Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Perl > Perl Misc > How to convert from a filehandle to a string?

Reply
Thread Tools

How to convert from a filehandle to a string?

 
 
Bryan
Guest
Posts: n/a
 
      09-05-2007
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
 
Reply With Quote
 
 
 
 
Michele Dondi
Guest
Posts: n/a
 
      09-05-2007
On Wed, 05 Sep 2007 15:12:37 -0700, Bryan <(E-Mail Removed)> 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^<R<Y]*YB='
..'KYU;*EVH[.FHF2W+#"\Z*5TI/ER<Z`S(G.DZZ9OX0Z')=~/./g)x2,$_,
256),7,249);s/[^\w,]/ /g;$ \=/^J/?$/:"\r";print,redo}#JAPH,
 
Reply With Quote
 
 
 
 
charley@pulsenet.com
Guest
Posts: n/a
 
      09-05-2007
On Sep 5, 6:12 pm, Bryan <(E-Mail Removed)> 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.htm...le%2c-slurping

Chris

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Re: How include a large array? Edward A. Falk C Programming 1 04-04-2013 08:07 PM
key as filehandle error Bill Perl 2 09-01-2004 12:22 PM
$v = shift OR$_[0] on a filehandle, whats teh difereence Eduard W. Lohmann Perl 1 12-02-2003 06:30 PM
Behaviour of filehandle.readlines() for images on Windows Jorge Godoy Python 2 10-14-2003 08:35 PM
HELP! using filehandle.get("filename") writes to file when it reads!? Christopher Reeve C++ 1 09-14-2003 08:18 PM



Advertisments