in reply to version string oddities

Perl has a feature where barewords preceeded by a minus need not be quoted. This allows people to do function -optiona -optionb;.

>perl -wle "print abc;" Unquoted string "abc" may clash with future reserved word at -e line 1 +. Name "main::abc" used only once: possible typo at -e line 1. print() on unopened filehandle abc at -e line 1. >perl -wle "print -abc;" -abc

When your v-string gets packed into something which starts with a letter, you see this autoquoting in effect.

>perl -wle "print -90.0.0.1" -Z^@^@^A >perl -wle "print -91.0.0.1" Argument "[\0\0^A" isn't numeric in negation (-) at -e line 1. 0

Apparently, it didn't occured to the implementors of these features to test them used together. What you are expecting from the negation of a string?

Update: If you want to work with 90.0.0.1 as a number, unpack it.

$uint32_ip = unpack 'N', 90.0.0.1;

Once unpacked, you'll be able to manipulate it using numerical operators (including -, <<, >>, ^, | and &).

Update: Better yet, use one of the existing modules (such as Net::IP or NetAddr::IP).

Replies are listed 'Best First'.
Re^2: version string oddities (-bug)
by tye (Sage) on Sep 24, 2006 at 04:57 UTC

    Note that v-strings aren't required to demonstrate this oddity.

    > perl -wle 'print -"abc"' -abc > perl -wle 'print -"/abc";' Argument "/abc" isn't numeric in negation (-) at -e line 1. -0

    Which I first considered a bug in unary minus (thinking it should only act this way on bare words), but I think it is a reasonable choice if it is intentional and documented (it just isn't how I had the feature explained to me).

    - tye        

      Hmm, check this out:
      % perl -le'print -"-aaa"' +aaa % perl -le'print -"+aaa"' -aaa
      That's highly unexpected to me.
        Looks like you don't even need the quotes:
        $ perl -le 'print -aaa' -aaa $ perl -le 'print -+-aaa' +aaa
        You can't put "--aaa" because Perl thinks it's a decrement.
        "If you go on with this nuclear arms race, all you are going to do is make the rubble bounce" -- Winston Churchill
Re^2: version string oddities
by chrisdolan (Beadle) on Sep 24, 2006 at 04:47 UTC

    I'm hacking on PPI::Token::Number and was confused by the version strings. I really haven't used them before and was under the mistaken impression that they were actually numbers and not strings. So, in an attempt to look for PPI bugs, I've been writing regression tests negating anything I can think of.

    Duh, I can't believe I forgot to try it with -w.

    Still, the varied behavior is unexpected.