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

This must be a trivial question, but I have been unable to solve it :-(. I wish to slurp up a file all at once, chomp off all the line separators and then concatenate it all together in a single string. I have tried all sorts of things to no avail. To my surprise even this failed (adapted from the Cookbook)
sub multiChomp { my @out = $_; for (@out) { chomp; } return wantarray ? @out : $out[0] } @array = <> ; print join('',multiChomp(@array));
I have also tried various hacks based around setting $/ to no avail. Can anyone show me enlightenment? Thanks Alec

Replies are listed 'Best First'.
Re: How to slurp files and join lines
by duff (Parson) on May 26, 2005 at 16:07 UTC
    chomp(@slurp = <>); print @slurp;
    Alternatively,
    $text = do { local $/; <>}; $text =~ s/\n//g; # for some \n print $text;
    Update: I guess I should add perldoc -f chomp since you obviously didn't know that chomp already works on a list of values.

      And a requisite, ill-advised and derivative one-liner:

      my $slomped = join "", do { local $/; <>} =~ m/([^\n]+)/g;

      It's slower, messier, and hard to read. On the plus-side, I'd like to coin the term 'slomped'.

        That's ok, if you allow me to claim to the term "chumped" ;-)


        Remember rule one...
Re: How to slurp files and join lines
by cees (Curate) on May 26, 2005 at 16:29 UTC

    I can never be bothered to remember how to slurp files. And I don't have to because File::Slurp remembers for me.

    use File::Slurp; my $data = File::Slurp::read_file('filename'); $data =~ s/\015?\012//g;

    Some people will wonder why I bother to use a module to handle a concept that can be written in only a couple lines of code, but I know that File::Slurp will always 'Do the Right Thing', whereas I have been known to make silly little mistakes (I always used to get $| confused with $/).

Re: How to slurp files and join lines
by Joost (Canon) on May 26, 2005 at 16:05 UTC
Re: How to slurp files and join lines
by jeffa (Bishop) on May 26, 2005 at 16:07 UTC
Re: How to slurp files and join lines
by tlm (Prior) on May 26, 2005 at 18:03 UTC

    The reason your multiChomp fails is because you are setting @out from $_, instead of from @_; it is the latter that holds the arguments to the function.

    Just a couple of observations. One is that you don't need the loop in multiChomp; you can achieve the exact same results with chomp @out. Also, I don't see the utility of chomping the entire array and then returning only the first chomped element, which is what multiChomp does in scalar context. A more useful thing would be to return the concatenation of the chomped elements. Taken all this together, I would rewrite multiChomp like this:

    sub multiChomp { my @out = @_; chomp @out; return wantarray ? @out : join '', @out; }

    Or you could do this:

    ( my $text = do { local $/, @ARGV = 'foo.txt'; <> } ) =~ s/\n+//g;

    the lowliest monk

Re: How to slurp files and join lines
by alecclews (Novice) on Aug 17, 2005 at 13:49 UTC
    Many belated thanks for the help on this. I was very short of time so in the end the simplest solution was this:
    while (<$messageHandle>) { chomp; $body .= $_; }
Re: How to slurp files and join lines
by TedPride (Priest) on May 26, 2005 at 19:39 UTC
    It's much easier to put the file into a string first and then remove the line separators, like so:
    use strict; use warnings; $_ = join '',<DATA>; s/\n//g; print; __DATA__ line 1 line 2 line 3