Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl: the Markov chain saw
 
PerlMonks  

Re^2: " - " in the syntax

by plobsing (Friar)
on Mar 06, 2009 at 05:14 UTC ( [id://748744]=note: print w/replies, xml ) Need Help??


in reply to Re: " - " in the syntax
in thread " - " in the syntax

This got me curious

$ perl -MO=Deparse -e 'print $q -> start_html(-title => "The Time", -b +gcolor => "#ffffff") ' print $q->start_html(-'title', 'The Time', -'bgcolor', '#ffffff'); -e syntax OK $ perl -MO=Deparse -e 'print -"asdf"' print '-asdf'; -e syntax OK $ perl -MO=Deparse -e 'print -"1asdf"' print -1; -e syntax OK $ perl -MO=Deparse -e 'print -"-1asdf"' print '+1asdf'; $ perl -MO=Deparse -e 'print -"+1asdf"' print '-1asdf'; -e syntax OK

What I glean from this is that if you use unary minus on any non-numish-looking string constant, the minus will get prepended. If you use an undecorated(+/-) numish string constant it gets numified, but if you use a decorated one it inverts the sign and doesn't numify?

Is this accurate?
Is this documented anywhere?
What is the rule on this?
And what is this useful for (beyond use given in the OP)

Replies are listed 'Best First'.
Re^3: " - " in the syntax
by puudeli (Pilgrim) on Mar 06, 2009 at 06:04 UTC

    Did you read Symbolic-Unary-Operators?

    $ perl -MO=Deparse -e 'print -"asdf"' print '-asdf'; -e syntax OK
    If the operand is an identifier, a string consisting of a minus sign concatenated with the identifier is returned.
    $ perl -MO=Deparse -e 'print -"1asdf"' print -1; -e syntax OK
    If, however, the string begins with a non-alphabetic character (excluding "+" or "-"), Perl will attempt to convert the string to a numeric and the arithmetic negation is performed.
    $ perl -MO=Deparse -e 'print -"-1asdf"' print '+1asdf'; $ perl -MO=Deparse -e 'print -"+1asdf"' print '-1asdf'; -e syntax OK
    Otherwise, if the string starts with a plus or minus, a string starting with the opposite sign is returned.
    --
    seek $her, $from, $everywhere if exists $true{love};

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others examining the Monastery: (4)
As of 2024-04-26 09:09 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found