John M. Dlugosz has asked for the wisdom of the Perl Monks concerning the following question:

What exactly is this called? It's kind of futile to grep for "v" in the perldocs. Where is it explained?

The only explaination I've seen is actually in perlvar, under $^V.

So, "use version" feature will now use this new literal form. But the version check on use for module imports does not! The code in Exporter::Heavy looks for a string beginning with a digit.

So, how does one check for the presence of a new-style version literal? Could check for a small chr, which won't be a printable string until v32, but that seems crude.

—John

Edit kudra, 2001-07-20 Changed title to include 'vector of ordinals' as suggested

Replies are listed 'Best First'.
(tye)Re: v1.2.3 syntax
by tye (Sage) on Jul 19, 2001 at 09:59 UTC

    Exporter::require_version almost never gets called. See (tye)Re: Overriding Exporter::import.

    I found it funny (and a little sad) that perldata.pod defines this new type of literal but avoids giving it a name. The 5.6.0 pods don't ever mention "v-string" but do eventually refer to these as "vectors of (ordinals|integers)".

    As best as I can tell, there is absolutely no way to distinguish v53.46.52 from "5.4" once perl has parsed it. You could cheat a little and assume that if the version scalar already has a numeric value cached inside of it, then it probably isn't a ordinal vector.

    You can use Devel::Peek to determine this or use the old hack:

    my $version= shift; if( "0" eq ( $version ^ $version ) ) { # $version already has a numeric value } else { # $version is _only_ a string }

    Checking the code in universal.c, I see that the default method does something similar:

    if (!SvNIOK(sv) && SvPOK(sv)) { # ... } /* if we get here, we're looking for a numeric comparison,

            - tye (but my friends call me "Tye")
      The perldata that comes with ActiveState's latest build does not contain the string "vector". Perhaps the bleeding edge docs have already been improved?

      I guess I'll just have to assume that you'll never import a symbol with an illegal identifier name, and nobody will ask for a major version greater than 32 when I'm supporting a 1 or 2.

      Also, I don't beleive that the existing Exporter handles a v-string in $VERSION.

      —John

Re: v1.2.3 syntax
by converter (Priest) on Jul 19, 2001 at 09:29 UTC

    The perldelta included with Perl 5.6.1 provides a little detail:

    Support for strings represented as a vector of ordinals

    Literals of the form "v1.2.3.4" are now parsed as a string composed of characters with the specified ordinals. This is an alternative, more readable way to construct (possibly Unicode) strings instead of interpolating characters, as in ""\x{1}\x{2}\x{3}\x{4}"". The leading "v" may be omitted if there are more than two ordinals, so "1.2.3" is parsed the same as "v1.2.3".

    Strings written in this form are also useful to represent version "numbers". It is easy to compare such version "num­bers" (which are really just plain strings) using any of the usual string comparison operators "eq", "ne", "lt", "gt", etc., or perform bitwise string operations on them using "|", "&", etc.

    As it turns out, 5.6.0 is pretty much just another way to write "\x5\x6\x0":

    $ perl -e 'printf "%02X ", ord for split //, 5.6.0; print "\n"'
    05 06 00
    $ perl -e 'printf "%02X ", ord for split //, "\x5\x6\x0"; print "\n"'
    05 06 00

    conv

Re: v1.2.3 syntax
by Zaxo (Archbishop) on Jul 19, 2001 at 08:11 UTC

    Camel 3, pp. 67-68. "V-String Literals". Grep on 'v-string' should do well.

    After Compline,
    Zaxo

Re: v1.2.3 syntax
by petral (Curate) on Jul 19, 2001 at 09:16 UTC
    perl 5.6.1:
    > perldoc -f sprintf
    v interpret string as a vector of integers, output as
      numbers separated either by dots, or by an arbitrary
      string received from the argument list when the flag
      is preceded by C<*>
     
    The "v" flag is useful for displaying ordinal values of
      characters in arbitrary strings:
    printf "version is v%vd\n", $^V; # Perl's version printf "address is %*vX\n", ":", $addr; # IPv6 address printf "bits are %*vb\n", " ", $bits; # random bitstring


      p

      printf v flag: cool!

      But, doesn't UTF-8 encoding get in the way of dumping arbitrary bitstrings that way? $bits, for example, may contain illegal UTF-8 encodings.

      Experiments indicate that it's respecting the char/byte discipline attached to the string. That's good. Now if only we had a good way to test and force string disciplines...

      —John