Odd behaviour with has key - wide character

Discussion in 'Perl Misc' started by Jon Combe, Oct 13, 2008.

  1. Jon Combe

    Jon Combe Guest

    Does the letter "v" have any significance when creating a hash key?
    The following code snippet does not behave as I expect it to:-

    #!/usr/bin/perl -w

    %H = (v365, 3);
    print keys %H;

    When run it outputs

    Wide character in print at wide.pl line 4.
    Å­

    When the key name is changed to "va365" it prints "va365" as I expect.
    What is special about just v followed by numbers?

    Jon.
     
    Jon Combe, Oct 13, 2008
    #1
    1. Advertising

  2. Jon Combe wrote:
    > Does the letter "v" have any significance when creating a hash key?
    > The following code snippet does not behave as I expect it to:-
    >
    > #!/usr/bin/perl -w
    >
    > %H = (v365, 3);
    > print keys %H;
    >
    > When run it outputs
    >
    > Wide character in print at wide.pl line 4.
    > Å­
    >
    > When the key name is changed to "va365" it prints "va365" as I expect.
    > What is special about just v followed by numbers?
    >


    Have you tried it with "use strict;"?


    --
    RGB
     
    RedGrittyBrick, Oct 13, 2008
    #2
    1. Advertising

  3. RedGrittyBrick wrote:
    >
    > Jon Combe wrote:
    >> Does the letter "v" have any significance when creating a hash key?
    >> The following code snippet does not behave as I expect it to:-
    >>
    >> #!/usr/bin/perl -w
    >>
    >> %H = (v365, 3);
    >> print keys %H;
    >>
    >> When run it outputs
    >>
    >> Wide character in print at wide.pl line 4.
    >> Å­
    >>
    >> When the key name is changed to "va365" it prints "va365" as I expect.
    >> What is special about just v followed by numbers?
    >>

    >
    > Have you tried it with "use strict;"?
    >
    >


    Have *I*? (oops)

    C:\>perl -Mstrict -e "my %H=(v365,3); print keys %H"
    Wide character in print at -e line 1.
    ŭ

    C:\>perl -Mstrict -e "my %H=('v365',3); print keys %H"
    v365

    Somehow I thought strict would catch that. :-(

    --
    RGB
     
    RedGrittyBrick, Oct 13, 2008
    #3
  4. Jon Combe <> writes:


    > The following code snippet does not behave as I expect it to:-
    >
    > #!/usr/bin/perl -w
    >
    > %H = (v365, 3);
    > print keys %H;


    No, it isn't the usage as a hash key that makes the v special. In
    general a bareword consisting of a v followed by some numbers
    seperated by dots is handled like this.

    Look it up in 'perldoc perldata' under the heading 'Version Strings'.

    If it was a normal string, 'use strict' would have warned you about
    the bareword not being allowed. But it really isn't a bareword because
    of the v-string handling.

    //Makholm
     
    Peter Makholm, Oct 13, 2008
    #4
  5. Jon Combe <> writes:

    > Does the letter "v" have any significance when creating a hash key?
    > The following code snippet does not behave as I expect it to:-
    >
    > #!/usr/bin/perl -w
    >
    > %H = (v365, 3);
    > print keys %H;


    you're running into version string. Since 5.8, perl handles
    vX.Y.Z... where X Y and Z are numbers specially (this is considered a
    failed experiment by many people).

    note that any other letter would have caused an error under "strict":

    use strict;
    %h = ( b123, 3);

    Global symbol "%h" requires explicit package name at - line 2.
    Bareword "b123" not allowed while "strict subs" in use at - line 2.
    Execution of - aborted due to compilation errors.

    you should either manually quote the keys of a hash, or use the
    auto-quoting => operator:

    use strict;
    my %h = ( v123 => 3 );

    Note that even using => will not work correctly at 5.8.0: you should
    really upgrade your perl if you've got that version, since it has lots
    of bugs.

    --
    Joost Diepenmaat | blog: http://joost.zeekat.nl/ | work: http://zeekat.nl/
     
    Joost Diepenmaat, Oct 13, 2008
    #5
  6. Jon Combe

    Jon Combe Guest

    > you're running into version string. Since 5.8, perl handles
    > vX.Y.Z... where X Y and Z are numbers specially (this is considered a
    > failed experiment by many people).
    >
    > note that any other letter would have caused an error under "strict":
    >
    > use strict;
    > %h = ( b123, 3);
    >
    > Global symbol "%h" requires explicit package name at - line 2.
    > Bareword "b123" not allowed while "strict subs" in use at - line 2.
    > Execution of - aborted due to compilation errors.
    >
    > you should either manually quote the keys of a hash, or use the
    > auto-quoting => operator:
    >
    > use strict;
    > my %h = ( v123 => 3 );
    >
    > Note that even using => will not work correctly at 5.8.0: you should
    > really upgrade your perl if you've got that version, since it has lots
    > of bugs.
    >


    Thank you Joost. I tried with the quoting operator (=>) as you
    suggested but it didn't work, but this is because I do have Perl
    5.8.0. Sadly I am not the administrator of the system so I don't think
    that I will be able to change it. I cannot find any mention of
    "Version Strings" in the perldata documentation. Is that the correct
    page or was it not documented in 5.8.0?

    Jon.
     
    Jon Combe, Oct 13, 2008
    #6
  7. Jon Combe

    Guest

    RedGrittyBrick <> wrote:
    > Jon Combe wrote:
    > > Does the letter "v" have any significance when creating a hash key?
    > > The following code snippet does not behave as I expect it to:-
    > >
    > > #!/usr/bin/perl -w
    > >
    > > %H = (v365, 3);
    > > print keys %H;
    > >
    > > When run it outputs
    > >
    > > Wide character in print at wide.pl line 4.
    > > Å­
    > >
    > > When the key name is changed to "va365" it prints "va365" as I expect.
    > > What is special about just v followed by numbers?
    > >

    >
    > Have you tried it with "use strict;"?


    <The original post didn't show up for me>

    And more importantly, read the section on Version Strings in perldoc
    perldata.

    Xho

    --
    -------------------- http://NewsReader.Com/ --------------------
    The costs of publication of this article were defrayed in part by the
    payment of page charges. This article must therefore be hereby marked
    advertisement in accordance with 18 U.S.C. Section 1734 solely to indicate
    this fact.
     
    , Oct 13, 2008
    #7
  8. Jon Combe

    Tim Greer Guest

    Jon Combe wrote:

    > Does the letter "v" have any significance when creating a hash key?
    > The following code snippet does not behave as I expect it to:-
    >
    > #!/usr/bin/perl -w
    >
    > %H = (v365, 3);
    > print keys %H;
    >
    > When run it outputs
    >
    > Wide character in print at wide.pl line 4.
    > Å­
    >
    > When the key name is changed to "va365" it prints "va365" as I expect.
    > What is special about just v followed by numbers?
    >
    > Jon.


    Are you asking what the difference between v and "v" is? the "va365"
    example is in double quotes. 365, 3 and v365, 3 are two different
    things, unless you meant "v365", 3. Did you mean v365 => 3, ? Looks
    like you have a bareword otherwise. Were your working and non working
    examples literal?
    --
    Tim Greer, CEO/Founder/CTO, BurlyHost.com, Inc.
    Shared Hosting, Reseller Hosting, Dedicated & Semi-Dedicated servers
    and Custom Hosting. 24/7 support, 30 day guarantee, secure servers.
    Industry's most experienced staff! -- Web Hosting With Muscle!
     
    Tim Greer, Oct 14, 2008
    #8
  9. On 2008-10-13 14:51, Jon Combe <> wrote:
    >> you're running into version string. Since 5.8, perl handles
    >> vX.Y.Z... where X Y and Z are numbers specially (this is considered a
    >> failed experiment by many people).

    [...]
    >> Note that even using => will not work correctly at 5.8.0: you should
    >> really upgrade your perl if you've got that version, since it has lots
    >> of bugs.

    >
    > Thank you Joost. I tried with the quoting operator (=>) as you
    > suggested but it didn't work, but this is because I do have Perl
    > 5.8.0.


    Redhat Enterprise Linux 3?

    > Sadly I am not the administrator of the system so I don't think
    > that I will be able to change it.


    If the system has a C compiler installed you could compile your own
    version of perl and install it in $HOME/bin. That may not be practical
    if other users are supposed to use your scripts, though. In this case
    you could ask the sysadmin to upgrade the system or let you install a
    newer version of perl in some publicly accessible place (like
    /opt/perl5.10.0 or /usr/local/perl5.10.0).

    > I cannot find any mention of "Version Strings" in the perldata
    > documentation. Is that the correct page or was it not documented in
    > 5.8.0?


    It is the correct page and it was documented in 5.8.0, but the feature
    wasn't called "version strings". Search for "v-strings" instead.

    hp
     
    Peter J. Holzer, Oct 18, 2008
    #9
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Web Developer

    char 8bit wide or 7bit wide in c++?

    Web Developer, Jul 31, 2003, in forum: C++
    Replies:
    2
    Views:
    595
    John Harrison
    Jul 31, 2003
  2. George2
    Replies:
    2
    Views:
    388
    James Kanze
    Jan 25, 2008
  3. Disc Magnet
    Replies:
    2
    Views:
    726
    Jukka K. Korpela
    May 15, 2010
  4. Disc Magnet
    Replies:
    2
    Views:
    800
    Neredbojias
    May 14, 2010
  5. Martin Rinehart

    80 columns wide? 132 columns wide?

    Martin Rinehart, Oct 31, 2008, in forum: Javascript
    Replies:
    16
    Views:
    184
    John W Kennedy
    Nov 13, 2008
Loading...

Share This Page