Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Odd behaviour with has key - wide character

0 views
Skip to first unread message

Jon Combe

unread,
Oct 13, 2008, 8:34:27 AM10/13/08
to
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.

RedGrittyBrick

unread,
Oct 13, 2008, 8:40:48 AM10/13/08
to

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

unread,
Oct 13, 2008, 8:43:34 AM10/13/08
to

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

Peter Makholm

unread,
Oct 13, 2008, 8:53:32 AM10/13/08
to
Jon Combe <jco...@gmail.com> 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

Joost Diepenmaat

unread,
Oct 13, 2008, 8:55:00 AM10/13/08
to
Jon Combe <jco...@gmail.com> 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/

Jon Combe

unread,
Oct 13, 2008, 10:51:57 AM10/13/08
to
> 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.

xho...@gmail.com

unread,
Oct 13, 2008, 11:42:48 AM10/13/08
to

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

Tim Greer

unread,
Oct 14, 2008, 5:09:32 PM10/14/08
to
Jon Combe wrote:

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!

Joe Smith

unread,
Oct 15, 2008, 5:05:11 AM10/15/08
to
Tim Greer 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?
>>
>> Jon.
>
> Are you asking what the difference between v and "v" is?

No, he has run across the "Surprise and Confusion" of version strings.

perl -le 'print v65'
A
perl -le 'print chr 65'
A
perldoc perldata

Version Strings

Note: Version Strings (v-strings) have been deprecated.
They will not be available after Perl 5.8. The marginal
benefits of v-strings were greatly outweighed by the
potential for Surprise and Confusion.

A literal of the form "v1.20.300.4000" is parsed as a string
composed of characters with the specified ordinals. This
form, known as v-strings, provides an alternative, more
readable way to construct strings, rather than use the
somewhat less readable interpolation form
"\x{1}\x{14}\x{12c}\x{fa0}". This is useful for
representing Unicode strings, and for comparing version
"numbers" using the string comparison operators, "cmp",
"gt", "lt" etc. If there are two or more dots in the
literal, the leading "v" may be omitted.

print v9786; # prints UTF-8 encoded SMILEY, "\x{263a}"
print v102.111.111; # prints "foo"
print 102.111.111; # same

Peter J. Holzer

unread,
Oct 18, 2008, 5:14:22 AM10/18/08
to
On 2008-10-13 14:51, Jon Combe <jco...@gmail.com> 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

0 new messages