SPUG: itm 60 in effective perl programming

Tim Maher tim at consultix-inc.com
Thu Feb 24 09:14:23 PST 2005


On Thu, Feb 24, 2005 at 01:53:45AM -0800, John W. Krahn wrote:
> ><> is the "input operator"
>              ^^^^^^^^^^^^^^
>              readline operator

I follow Larry's lead; he calls it the "line input operator" on
p. 80 of the 3rd ed. Camel, but I omit the "line" part because it
doesn't necessarily read a line -- the setting of $/ actually
dictates how much data it will read.

> >-- it's like the UNIX shell's "read"
> >command, except it loads $_ with the next line of input
> >rather than some other variable.  
> 
> IIF it is used in a while loop conditional!

Not IIF, because in addition to the while case it also loads $_ for
	for ($i; <>; $i++)
and in the absence of some other specified loop variable, also for 
	"foreach (<>)" 
and of course, it also loads $_ when the implicit loop of -n/-p
is used. 

> >But IMHO, that's a pretty inscrutable way to join lines.  Here's a more
> >"scrutable" way, based on the implicit loop (-p), assuming you really
> >want a space between each pair of lines as shown above:
> >
> >perl -wpe 's/\n$/ /;' file	# replace newline with space

> Also, the end of line anchor ($) is superfluous as there is
> only one newline in every line (unless you change the Input
> Record Separator.)
> John

Yes, but IMHO it's often worth adding a "superfluous" character or two to
make a program more readable -- and the original poster was a confused
newbie, after all, so I was going for "added scrutability" 8-}

Tim
*--------------------------------------------------------------------------*
| Tim Maher, PhD     (206) 781-UNIX      (866) DOC-PERL     (866) DOC-UNIX |
| tim(AT)Consultix-Inc.Com  http://TeachMePerl.Com  http://TeachMeUnix.Com |
*+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-*
|      Watch for my upcoming book: "Minimal Perl for UNIX/Linux People"    |
*--------------------------------------------------------------------------*


More information about the spug-list mailing list