P is for Practical | |
PerlMonks |
Re: The "<" in the grep blockby tobyink (Canon) |
on Sep 23, 2012 at 15:12 UTC ( [id://995209]=note: print w/replies, xml ) | Need Help?? |
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:
or
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:
or to place parentheses like this:
or include an explicit $_:
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'
In Section
Seekers of Perl Wisdom
|
|