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

Can i assign a whole file's data to a string. Is there any length restriction. i.e while<> { $str = $str." ".$_; } Thanks

Replies are listed 'Best First'.
Re: Data length in strings
by ChOas (Curate) on Jul 24, 2001 at 14:58 UTC
    It might be easier to undef $/ (The input record separator,
    which is normaly "\n") it has the same effect...

    e.g.:
    ... open, etc... my $File { local $/; $File=<FILE>; } ...

    Limitation is you memory....

    check: perldoc perlvar

    p.s. Thank you Tye

    GreetZ!,
      ChOas

    print "profeth still\n" if /bird|devil/;
      Once I encountered a problem concerning an exported peagasus mailfile. It was several hundreds of megabytes big. My oportunity was to split it into parts, each containing exactly one mail. As the file had a nice seperator, it seemed quite easy to me - but the first attempt blew up my memory. This was the first time I came to use the $_ variable instead of an own:
      open(FH,"<$source") || die "cant open $source: $!\n"; while (<FH>){ push @file, $_; if ($_ =~ /^-- End --/) { $zaehler++; open(GUN,">$destiny") || die "can write to $destinyfile$zaehle +r:$!"; foreach $zeile (@file) { print GUN $zeile; }#endforeach undef @file; close(GUN); print "$zaehler:\n"; }#endif }#endwhile close(FH);

      easy code, sent in the hope it helps you

      there are no silly questions
      killerhippy

      Edit ar0n -- 2001-07-24 fixed excessive whitespace

Re: Data length in strings
by arturo (Vicar) on Jul 24, 2001 at 15:01 UTC

    There are no built-in length restrictions on Perl's strings; in practice, the limitation is how much of your system's memory is available to Perl (this is something your system administrator can set, so check local listings).

    The most efficient ways of reading a whole file into a scalar involve unsetting the input record separator $/ and then reading in the file (by unsetting the separator, nothing counts as a break between lines, so to speak). A bit of code that does that is:

    my $str; { local $/; # sets $/ to undef only WITHIN the braces open FILE, $filename or die "Can't open $filename: $!\n"; $str = <FILE>; close FILE: }

    HTH.

    perl -e 'print "How sweet does a rose smell? "; chomp ($n = <STDIN>); +$rose = "smells sweet to degree $n"; *other_name = *rose; print "$oth +er_name\n"'
Re: Data length in strings
by ariels (Curate) on Jul 24, 2001 at 15:03 UTC
    More succinctly: just use the diamond in list context:
    $str = join ' ',<>;

    If you just want to slurp all the lines, concatenated (you usually don't want those spaces we just added):

    { local $/; # Temporarily set $/=undef: "total slurp mode" $str = <>; # Slurp it all in one go! }
    Note the use of local (not my!) to set the special variable $/.

    You're still working on your computer, though! So you cannot (or should not) read huge files into memory. Reading huge files into memory is grounds for LARTing on many systems!

    How large is `huge' depends on your computer; it's almost certainly well below 2GB, though!


    UPDATE

    Thanks to ar0n for pointing out I meant diamond not spaceship.

      How large is `huge' depends on your computer; it's almost certainly well below 2GB, though!
      You have seen my computer, haven't you?

      scnr ;-)))

      -- Hofmator

Re: Data length in strings
by grinder (Bishop) on Jul 24, 2001 at 16:25 UTC
    Setting $/ to undef is good, but it won't exactly what the person asked for, namely, concatenate a space between each record. Only the join ' ', <> idiom will do that.

    Be that as it may, I think using <> in array context is going to build up a huge AV temporary (Perl's internal representation of an array). Also, I have a sneaking suspicion the person would also like to get rid of the newlines of each record, in which case one would do something like:

    my $foo; while( <> ) { chomp; # maybe, maybe not, depending on the circumstances if( defined( $foo )) { $foo .= " $_"; } else { $foo = $_; } }
    --
    g r i n d e r
Re: Data length in strings
by CharlesClarkson (Curate) on Jul 24, 2001 at 15:48 UTC
    Instead of using $/, try reading the file.

    open my $fh, $file_name or die "Cannot open $file_name $!"; read $fh, my $str, -s $fh;

    HTH,
    Charles K. Clarkson