Odd behaviour with has key - wide character

J

Jon Combe

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.
 
R

RedGrittyBrick

Jon said:
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;"?
 
R

RedGrittyBrick

RedGrittyBrick said:
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. :-(
 
P

Peter Makholm

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
 
J

Joost Diepenmaat

Jon Combe said:
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.
 
J

Jon Combe

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.
 
X

xhoster

RedGrittyBrick said:
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.
 
T

Tim Greer

Jon said:
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?
 
P

Peter J. Holzer

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
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top