How to convert from a filehandle to a string?

How to convert from a filehandle to a string?

am 06.09.2007 00:12:37 von Bryan

Hi,

I have a filehandle, $file.

I can do something like this:
while (<$file>) {
print $_;
# or
$string .= $_;
}

But is there a way to stuff the whole file to a string in one shot? I
tried:
$string = <$file>;

But no dice.

Thanks,
B

Re: How to convert from a filehandle to a string?

am 06.09.2007 00:17:06 von Michele Dondi

On Wed, 05 Sep 2007 15:12:37 -0700, Bryan wrote:

>Subject: How to convert from a filehandle to a string?

This doesn't make sense.

>I have a filehandle, $file.
>
>I can do something like this:
>while (<$file>) {
> print $_;
> # or
> $string .= $_;
>}
>
>But is there a way to stuff the whole file to a string in one shot? I
>tried:
>$string = <$file>;

Read about $/ in

perldoc perlvar

or resort to Uri's File::Slurp.


Michele
--
{$_=pack'B8'x25,unpack'A8'x32,$a^=sub{pop^pop}->(map substr
(($a||=join'',map--$|x$_,(unpack'w',unpack'u','G^ ..'KYU;*EVH[.FHF2W+#"\Z*5TI/ER 256),7,249);s/[^\w,]/ /g;$ \=/^J/?$/:"\r";print,redo}#JAPH,

Re: How to convert from a filehandle to a string?

am 06.09.2007 01:11:56 von charley

On Sep 5, 6:12 pm, Bryan wrote:
> Hi,
>
> I have a filehandle, $file.
>
> I can do something like this:
> while (<$file>) {
> print $_;
> # or
> $string .= $_;
>
> }
>
> But is there a way to stuff the whole file to a string in one shot? I
> tried:
> $string = <$file>;
>
> But no dice.
>
> Thanks,
> B

In addition to the info you'll find in perlvar, you can also read
about what you want to do in:
http://perldoc.perl.org/perlfaq5.html#How-can-I-read-in-an-e ntire-file-all-at-once%3f-slurp-file%2c-slurping

Chris