At 18:48 +0200 2002.04.08, Louis Pouzin wrote:
>I want to read several text files into a single string.
>
>use strict;
>@ARGV=('fil1','fil2','fil3');
>my $fil = do{local $/; <>};
>print $fil;
>__END__
>
>This prints only the contents of fil1.

You would have to loop over it, and at that point, may not want to use the
implicit <>.  I'd just be more explicit:

use strict;
@ARGV=('fil1','fil2','fil3');
my $fil = join '', <>;
print $fil;
__END__


Or, perhaps a bit faster, as you let Perl give you one string per file
instead of joining many strings per file:

use strict;
@ARGV=('fil1','fil2','fil3');
my $fil;
{ local $/;
  $fil = join '', <>;
}
print $fil;
__END__



>If I use an array:
>
>use strict;
>@ARGV=('fil1','fil2','fil3');
>my @fil = do{local $/; <>};
>print @fil;
>__END__
>
>All 3 files are printed, but there are 3 strings in @fil.

Yep.  That do{} block returns three strings in either context, with $fil or
@fil, one string for each file.



At 09:56 -0700 2002.04.08, Andrew O. Mellinger wrote:
>You have made a local of $/,
>but haven't changed the value.  You probably want to undef it.

No, local() creates a new value; if you don't provide one, then it defaults
to undef (or an empty list, in the case of an array or hash).  So
"local($/)" does undef $/ until the current block exits (or a new value is
given to it).

-- 
Chris Nandor                      [EMAIL PROTECTED]    http://pudge.net/
Open Source Development Network    [EMAIL PROTECTED]     http://osdn.com/

Reply via email to