Beefy Boxes and Bandwidth Generously Provided by pair Networks
P is for Practical
 
PerlMonks  

Re: The "<" in the grep block

by tobyink (Canon)
on Sep 23, 2012 at 15:12 UTC ( [id://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)

or

(-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?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://995209]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others meditating upon the Monastery: (7)
As of 2024-04-19 08:07 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found