[HoustonTx.pm] Golf (was Re:Perl/Unix)
houston at mail.pm.org
houston at mail.pm.org
Thu Aug 7 11:57:09 CDT 2003
> perl -ne 'local$/;$x.=<>'
Ok, last time, I promise... Heh, I really should think it all the way
through before replying... Here's my absolute shortest solution (which
works, tested by adding extra code), and clearest + without bugs, as well:
perl -ne '$x.=$_'
There, that reads the contents of any number of files into a single scalar,
without the need for a do, assignment to @ARGV (already done), or modifying
the IRS.
(it's more clear, and correct, than the previous example, as lines may have
been lost by saying $x .= <> -- as the script already called <> and
assigned it to $_ from -n [see below]. When the IRS is set to undef in the
previous example, the very first line is lost, as, imagine what happens when
you say:
while(<>) {
local($/) = undef;
$contents = <>;
}
$contents is missing the first line. I imagine that all the talk about
'<>' originally got me lost, and forgetting that I needed to be using $_,
instead =)
For what it does:
-n argument assumes : 'while (<>) { [your script] }'
-e argument, well, we know what that is (code follows)
$x .= $_
append the results from the code evaluated by -n into $x, e.g.:
while (<>) {
$x .= $_;
}
Is what that code expands to, which is equivalent to:
while(<ARGV>) {
$x .= $_;
}
!chris
===================================
Discreet Packaging: www.dronecolony.com
C. Church
===================================
More information about the Houston
mailing list