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

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)

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'


Comment on Re: The "<" in the grep block
Select or Download Code

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others browsing the Monastery: (9)
As of 2015-07-04 15:25 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    The top three priorities of my open tasks are (in descending order of likelihood to be worked on) ...









    Results (60 votes), past polls