[HoustonTx.pm] Golf (was Re:Perl/Unix)

houston at mail.pm.org houston at mail.pm.org
Thu Aug 7 12:02:26 CDT 2003


Wow. Good job, excellent explanation

-Mike


On Thu, Aug 07, 2003 at 11:57:09AM -0500, houston at mail.pm.org wrote:
> > 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
> ===================================
> 
> 
> _______________________________________________
> Houston mailing list
> Houston at mail.pm.org
> http://mail.pm.org/mailman/listinfo/houston
> 



More information about the Houston mailing list