|
Anonymous Monk has asked for the
wisdom of the Perl Monks concerning the following question:
Why does -localtime force scalar context but not +localtime?
print -localtime, $/;
print +localtime, $/;
print +gmtime, $/;
print -+gmtime, $/;
__END__
-Fri Nov 6 01:10:44 2009
4417161010953090
4417961010953090
-Fri Nov 6 09:10:44 2009
Re: -localtime +gmtime by moritz (Chancellor) on Nov 06, 2009 at 09:58 UTC |
B::Deparse to the help:
$ perl -MO=Deparse -e 'print +localtime'
print localtime;
-e syntax OK
$ perl -MO=Deparse -e 'print -localtime'
print -localtime;
-e syntax OK
The + can be used to tell the parser that something isn't part of a function call, it's not a way to tell perl this is a number. We've just discussed this in Re: Split returning one element.
In Perl 6 it is a bit different: foo(..), $x is always parsed a function call, followed by a comma and $x, whereas foo (...), $x is parsed a function call which receives both the parenthesis group and $x. That leaves the unary + free to force numeric context.
Perl 6 - links to (nearly) everything that is Perl 6.
| [reply] [d/l] [select] |
Re: -localtime +gmtime by ikegami (Saint) on Nov 06, 2009 at 16:05 UTC |
The no-op operator (unary-plus) doesn't even affect context. It is used to disambiguate parser decisions.
Negation requires a number, so it forces numerical context. (Some convenient behaviour was added for when the argument isn't a number.)
| [reply] |
Back to
Seekers of Perl Wisdom
|