Phoenix.pm: Code problem

Kevin Buettner kev at primenet.com
Tue Oct 12 18:52:06 CDT 1999


On Oct 12,  7:32pm, Beaves at aol.com wrote:

> OK, OK!!  I have to fess up that the $size assignment in Ron's code is 
> totally and completely greek to me...
> 
> $size = `wc -w $out_file | tr -s " " | cut -f1 -d " "` ;
> 
> Can someone go through that assignment statement,  character by character and 
> decipher it for me (and I'm sure others)?

The backquotes cause the enclosed shell command to be executed and the
assigned to $size.

wc -w $outfile

    causes the wc command to be run on outfile.  The -w switch causes
    it to only return the number of words.

tr -s " "

    causes all the space characters to be squeezed together

cut -f1 -d " "

    causes the first field to be removed where fields are delimited
    with space characters.

Phaedrus <phaedrus at endless.org> correctly pointed out that for correct
operation, the -f1 should've been a -f2.  You get a null field as the
first field.

The | characters (vertical bars) are pipes.  The output of the wc
command is piped to the tr command which in turn pipes its output
to the cut command.  The output of this entire pipe is then given
back to perl and put in $size.

BTW, all of these problems could've been avoided by doing the
following:

    $size = 0 + `wc -w`;
    print "Unix word count = $size\n";

It also has the advantage of running two fewer processes per iteration.
(I still like split better though for counting the number of words.)

Kevin

-- 
Kevin Buettner
kev at primenet.com, kevinb at cygnus.com



More information about the Phoenix-pm mailing list