http://qs1969.pair.com?node_id=517772

jeanluca has asked for the wisdom of the Perl Monks concerning the following question:

Dear Monks,

I would like to do something describe in the example below
open IN, "<myFile" ; $str .= <IN> close IN ;
To me it looks very logic, but it doesn't work. Is there a way simular that does work ?

Thanks in advance
Luca

Replies are listed 'Best First'.
Re: put file content into a string
by Roy Johnson (Monsignor) on Dec 19, 2005 at 16:14 UTC
Re: put file content into a string
by Fletch (Bishop) on Dec 19, 2005 at 16:15 UTC

    The idiomatic construct would be:

    open( IN, "<", $file ) or die "Can't open: $file: $!\n"; my $str = do { local $/; <IN> }; close( IN );

    See perlvar for more info on $/.

Re: put file content into a string
by ptum (Priest) on Dec 19, 2005 at 16:14 UTC

    First, you don't seem to check to see if the open works. This seems a critical flaw. Try something like this:

    open IN, "<myFile" or die "Could not open myFile: $!";

    Second, you will only get the first line of the file, unless you muck with the record separator. You may want to append to your $str variable inside a while loop.

    Hope that helps. :)


    No good deed goes unpunished. -- (attributed to) Oscar Wilde
Re: put file content into a string
by prasadbabu (Prior) on Dec 19, 2005 at 16:13 UTC

    jeanluca, this will work

    open (IN, "<", "myFile") or die "Cannot open: myFile: $!\n"; ; { local $/; $str = <IN>; print "$str\n"; } close IN ;

    Also there is a module Slurp to do this.

    Prasad

Re: put file content into a string
by Hue-Bond (Priest) on Dec 19, 2005 at 16:19 UTC

    Of course, more than one. Try:

    { local $/ = undef; open my $fd, '<', 'file' or die "open: $!"; my $txt = <$fd>; close $fd; }

    Or:

    open my $fd, '<', '/home/hue/f1' or die "open: $!"; my $txt = join '', <$fd>; close $fd;

    Be sure to take a look at perldoc perlop for insights on this and many other subjects.

    --
    David Serrano

Re: put file content into a string
by tirwhan (Abbot) on Dec 19, 2005 at 16:19 UTC

    TIMTOWDI and here's how you can do it with Perl6::Slurp:

    use Perl6::Slurp; my $content = slurp 'myFile';

    Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it. -- Brian W. Kernighan
Re: put file content into a string
by jeanluca (Deacon) on Dec 19, 2005 at 16:19 UTC
    Well, my dear monks, the reason I thought this might work is because of this
    open IN,"<myFile" or die "Hmmm Error" ; @str = <IN> ; close IN ;

    Thanks
    Luca

      You got bitten by yet another facet of Perl which is sensitive to the calling context. In LIST context the diamond operator (<FH>) does indeed return a list of all the lines in the file (separated by the current value of $/). However in SCALAR context it just returns the next line (again, separated by $/). This mjd USENET posting may clear things up if you're still fuzzy. If you ever see "strange" behavior like this check the docs and make sure it's not context sensitive.

Re: put file content into a string
by Spidy (Chaplain) on Dec 19, 2005 at 17:26 UTC
    I have a method that does something like that, and it looks like this:
    open IN, "<myFile"; while(<IN>) { # read the file in, line by line $str .= $_; } close IN;
Re: put file content into a string
by l3v3l (Monk) on Dec 19, 2005 at 17:31 UTC
    You could also use the "-0777" command line switch if you were writing a quick solution to do something like parsing the entire input file as a single line in order to alter the newlines that otherwise require a range operator to deal with, ie:
    perl -0777 -pe 's/\n+/\n/g' myFile
    The value 0777 will cause Perl to slurp files whole because there is no legal byte with that value.