Beefy Boxes and Bandwidth Generously Provided by pair Networks
"be consistent"

Re: The "<" in the grep block

by tobyink (Abbot)
on Sep 23, 2012 at 15:12 UTC ( #995209=note: print w/replies, xml ) Need Help??

in reply to The "<" in the grep block

Certain Perl built-ins, including -s can take an argument or operate on the default variable $_. For example, defined $var versus just defined.

In certain circumstances, perl needs a little help to disambiguate. In this case, it's not sure whether you mean an ill-formed glob:



(-s) < 1000

perl could theoretically look ahead searching for a ">" to disambiguate, but IIRC it only ever looks ahead one token.

Other ways to disambiguate would be to use a different operator which suffers from fewer ambiguities:

grep { 1000 > -s } @ARGV; # or grep { -s <= 999 } @ARGV;

or to place parentheses like this:

-s() < 1000

or include an explicit $_:

-s $_ < 1000

This is not Windows specific. It could theoretically change between Perl releases, but the oldest and newest versions of Perl I have available on this computer (5.8.9 and 5.16.0) both behave the same with regard to this particular example. So I don't know why your book contains the bad example; perhaps the writer never actually tested it?

perl -E'sub Monkey::do{say$_,for@_,do{($monkey=[caller(0)]->[3])=~s{::}{ }and$monkey}}"Monkey say"->Monkey::do'

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://995209]
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others having an uproarious good time at the Monastery: (4)
As of 2018-04-20 09:15 GMT
Find Nodes?
    Voting Booth?