passing $fd as a reference

passing $fd as a reference

am 05.10.2007 21:06:31 von Larry

Hi everybody,

below is a few lines from http::daemon pod:

--
$c->send_file( $filename )
$c->send_file( $fd )

Copy the file to the client. The file can be a string (which will be
interpreted as a filename) or a reference to an IO::Handle or glob.
--

I have to admit I have never read file contents by IO::Handle

I usually go about it by:

my $song = 'song.mp3';
open my $fh1, '<', $song or die "open(): $!\n";
binmode $fh1;
close($fh1);

how can I tie the code above to $c->send_file( $fd ) ???

--

btw, when reading a binary file in order to move cursor position, do you
think I should use "seek" or "sysseek" and the should I use read the
file by "read" or "sysread" ??

thanks ever so much!

Re: passing $fd as a reference

am 05.10.2007 22:02:38 von Jim Gibson

In article , Larry
wrote:

> Hi everybody,
>
> below is a few lines from http::daemon pod:
>
> --
> $c->send_file( $filename )
> $c->send_file( $fd )
>
> Copy the file to the client. The file can be a string (which will be
> interpreted as a filename) or a reference to an IO::Handle or glob.
> --
>
> I have to admit I have never read file contents by IO::Handle
>
> I usually go about it by:
>
> my $song = 'song.mp3';
> open my $fh1, '<', $song or die "open(): $!\n";
> binmode $fh1;
> close($fh1);
>
> how can I tie the code above to $c->send_file( $fd ) ???

Use IO::File (untested):

my $fh1 = new IO::File $song;
die "open: $!" unless defined $fh1;
$fh1->binmode;
$c->send_file($fh1);
$fh1->close;

>
> --
>
> btw, when reading a binary file in order to move cursor position, do you
> think I should use "seek" or "sysseek" and the should I use read the
> file by "read" or "sysread" ??

Use sysseek if you are using sysread to read the file, seek otherwise.

--
Jim Gibson

Posted Via Usenet.com Premium Usenet Newsgroup Services
----------------------------------------------------------
** SPEED ** RETENTION ** COMPLETION ** ANONYMITY **
----------------------------------------------------------
http://www.usenet.com

Re: passing $fd as a reference

am 06.10.2007 08:56:03 von Larry

In article <051020071302388074%jgibson@mail.arc.nasa.gov>,
Jim Gibson wrote:

> Use IO::File (untested):
>
> my $fh1 = new IO::File $song;
> die "open: $!" unless defined $fh1;
> $fh1->binmode;
> $c->send_file($fh1);
> $fh1->close;

thanks it worked great!!

yet, now I'd like to pass a socket filedesc:

my $fh1 = IO::Socket::INET->new(PeerAddr => '127.0.0.1',PeerPort =>
'65000',Proto => 'tcp');
syswrite $fh1, "GET /song/$itemnum.mp3 HTTP/1.1".$CRLF.$CRLF;
$c->send_file($fh1);
$fh1->close;

But it won't work...why ??

Re: passing $fd as a reference

am 06.10.2007 12:24:06 von nobull67

On Oct 6, 7:56 am, Larry wrote:
> In article <051020071302388074%jgib...@mail.arc.nasa.gov>,
> Jim Gibson wrote:
>
> > Use IO::File (untested):
>
> > my $fh1 = new IO::File $song;
> > die "open: $!" unless defined $fh1;
> > $fh1->binmode;
> > $c->send_file($fh1);
> > $fh1->close;
>
> thanks it worked great!!
>
> yet, now I'd like to pass a socket filedesc:
>
> my $fh1 = IO::Socket::INET->new(PeerAddr => '127.0.0.1',PeerPort =>
> '65000',Proto => 'tcp');
> syswrite $fh1, "GET /song/$itemnum.mp3 HTTP/1.1".$CRLF.$CRLF;
> $c->send_file($fh1);
> $fh1->close;
>
> But it won't work...why ??

In what way does it not work?

BTW, this has nothing to do with your question (although may be
related to your real problem) but is looks like you are trying to
implement an HTTP/1.1 client.

This is a far from simple exercise. For example HTTP/1.1 clients MUST
support "chunked". Does yours?

Your HTTP request is invalid - it has a 1.1 protocol version number
but lacks the required "Host:" header.

If you want an HTTP client, use LWP (or an external binary).

Re: passing $fd as a reference

am 07.10.2007 03:49:21 von Larry

In article <1191666246.122474.313690@50g2000hsm.googlegroups.com>,
Brian McCauley wrote:

> In what way does it not work?

it was my fault, now it's working great...

Re: passing $fd as a reference

am 07.10.2007 23:06:48 von Ben Morrow

Quoth Jim Gibson :
> In article , Larry
> wrote:
>
> > Hi everybody,
> >
> > below is a few lines from http::daemon pod:
> >
> > --
> > $c->send_file( $filename )
> > $c->send_file( $fd )
> >
> > Copy the file to the client. The file can be a string (which will be
> > interpreted as a filename) or a reference to an IO::Handle or glob.
> > --
> >
> > I have to admit I have never read file contents by IO::Handle
> >
> > I usually go about it by:
> >
> > my $song = 'song.mp3';
> > open my $fh1, '<', $song or die "open(): $!\n";
> > binmode $fh1;
> > close($fh1);
> >
> > how can I tie the code above to $c->send_file( $fd ) ???
>
> Use IO::File (untested):
>
> my $fh1 = new IO::File $song;

IO::File is obsolete. Lexical filehandles are globrefs, and as such can
be passed directly to HTTP::Daemon->send_file.

> > btw, when reading a binary file in order to move cursor position, do you
> > think I should use "seek" or "sysseek" and the should I use read the
> > file by "read" or "sysread" ??
>
> Use sysseek if you are using sysread to read the file, seek otherwise.

IMHO sys* are obsolete as well; I would use

binmode $FH, ':unix';

and then use the ordinary functions. But that's just a point of view :).

Ben