SPUG:Best One-Liners and Scripts for UNIX

SPUG-list-owner tim at consultix-inc.com
Tue Apr 29 12:25:26 CDT 2003


On Sat, Apr 26, 2003 at 10:49:17PM -0700, Yitzchak Scott-Thoennes wrote:
> On Sat, 19 Apr 2003 10:37:13 -0700, tim at consultix-inc.com wrote:
> >        ($fields = shift) =~ /^[\d,.]+$/g or
> 
> That trips a warning if the script is given no args.  Make it:
> $fields = shift and $fields =~ ... 

Good idea, but that's not the same, because they could ask for field
#0 only, and that would be treated the same as a missing argument due
to its false value. I can't see the original program I submitted at
the moment, but I would usually have had a "@ARGV > 0 or die" or
equivalent in the BEGIN block to handle this kind of argument checking.
 
> >	@fields = eval " ( $fields ) ";	# 5,2..4 => 5,2,3,4
> 
> The parens there aren't necessary.  You need parens for something

I wasn't writing for the benefit of the compiler, but for that of the
reader, who I think will find it easier to understand that a list is
being formed with the parens there (and they don't bother the compiler).

> like: @fields = (5,2..4) just for correct precedence ('=' is lower
> precedence than ','), but with the eval there, there is no precedence
> problem.  Parens almost never affect anything besides precedence.
> (Two major exceptions are around the left operands of '=' and 'x':
> there the behaviour of the operator completely changes.  I thought

But my parens *are* on the right side of an "=", so if one glosses
over the eval, it reads like @fields = ( 5,2..4 ).  That was my
intention, anyway; obviously, you know too much about Perl to appreciate
this illusion! 8-}

> there was a third important exception but can't remember what it was.)

You're probably thinking of eof() vs eof, and use Module() vs use Module,
in which the parens fundamentally change the obtained behavior.

-Tim
*------------------------------------------------------------*
|  Tim Maher (206) 781-UNIX  (866) DOC-PERL  (866) DOC-UNIX  |
|  CEO, JAWCAR ("Just Another White Camel Award Recipient")  |
|  tim at Consultix-Inc.Com  TeachMeUnix.Com  TeachMePerl.Com   |
*+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-*
|  Watch for my  Book: "Minimal Perl for Shell Programmers"  |
*------------------------------------------------------------*



More information about the spug-list mailing list