Beefy Boxes and Bandwidth Generously Provided by pair Networks
laziness, impatience, and hubris
 
PerlMonks  

perl -e hash assignment and range operator bug

by crazyinsomniac (Prior)
on Aug 26, 2001 at 20:18 UTC ( [id://107941] : perlquestion . print w/replies, xml ) Need Help??

crazyinsomniac has asked for the wisdom of the Perl Monks concerning the following question:

Hello. Some monks and I were discussing perl 6, and BooK said, why not just %elbow = 1..10, 11..20;
So I test it out, like i do everything, and i do: perl -e"%a=( 1..10, 11..20 );print keys %a" and I get no output.

I try all my activeperl installs, nada. I try all my indigoperl installs, same thing (btw this be 5.6.0 and 5.6.1 builds 618 and up)

Meanwhile, BooK says, yes it does, and spits Dumper in the cb: perl -MData::Dumper -e'print { 1..10, 11..20 } and sure enough, Dumper dumps a hash like:

$VAR1 = { '13' => 14, '7' => 8, '15' => 16, '9' => 10, '1' => 2, '17' => 18, '3' => 4, '19' => 20, '11' => 12, '5' => 6 };

So I ssh over to jcwrens machine and sure enough i get some keys (111313515717919).

So now I says to me, this must be a bug, and to confirm, i try: perl -MData::Dumper -e "%a= 1 .. 10, 2 .. 11 ;print Dumper \%a" which yields nada, and then perl -MData::Dumper -e "$a={ 1 .. 10, 11.. 20};print Dumper $a" which spits our (as expected)

$VAR1 = { '13' => 14, '7' => 8, '15' => 16, '9' => 10, '1' => 2, '17' => 18, '3' => 4, '19' => 20, '11' => 12, '5' => 6 };
So, I think this is a win32 bug. Can anyone confirm/deny? Anyone have any idea where it is, sourcecode wise? (like foo.c or something)

 
___crazyinsomniac_______________________________________
Disclaimer: Don't blame. It came from inside the void

perl -e "$q=$_;map({chr unpack qq;H*;,$_}split(q;;,q*H*));print;$q/$q;"

Replies are listed 'Best First'.
Re: perl -e hash assignment and range operator bug
by japhy (Canon) on Aug 26, 2001 at 20:29 UTC
    Windows uses % as its environment variable token. You must double it to get a single one.

    The code %a = (1,2,3,4); print %a is reduced to a because the commandline sees a = (1,2,3,4); print as the name of an environment variable.

    _____________________________________________________
    Jeff[japhy]Pinyan: Perl, regex, and perl hacker.
    s++=END;++y(;-P)}y js++=;shajsj<++y(p-q)}?print:??;

        No, the problem is you're using Windows.

        _____________________________________________________
        Jeff[japhy]Pinyan: Perl, regex, and perl hacker.
        s++=END;++y(;-P)}y js++=;shajsj<++y(p-q)}?print:??;

Re: perl -e hash assignment and range operator bug
by Rudif (Hermit) on Aug 27, 2001 at 01:31 UTC
    crazyinsomniac said

    >> So I test it out, like i do everything, and i do:  perl -e"%a=( 1..10, 11..20 );print keys %a" and I get no output.

    Well, when I test in on my one and only Perl (5.6.1 build 628 on Win2k), I get this

    H:\>perl -e"%a=( 1..10, 11..20 );print keys %a" 137159117319115
    or in a more readable version
    H:\>perl -e"%a=( 1..10, 11..20 );print qq{@{[keys %a]}}" 13 7 15 9 1 17 3 19 11 5
    both of which look good to me.

    crazyinsomniac continued

    >> So now I says to me, this must be a bug, and to confirm, i try:

    perl -MData::Dumper -e "%a= 1 .. 10, 2 .. 11 ;print Dumper \%a"
    which yields nada, and then ...

    Me again, when I test it, I get this - as expected.

    H:\>perl -MData::Dumper -e "%a= 1 .. 10, 2 .. 11 ;print Dumper \%a" $VAR1 = { '7' => 8, '9' => 10, '1' => 2, '3' => 4, '5' => 6 };
    Seems to work nicely. The % character that japhy mentioned is tucked inside the double quotes and is seen by Perl, not the cmd shell.

    So where is the problem? Did I miss something here?

    I am all for bashing the cmd shell (pun intended) when justified, but here I cannot confirm the problem.

    Can you help me out on this, crazyinsomniac ?

    Rudif

    Update Could somemonk please set me straight on the operator precedence in the second example above?

    I think that the assignment binds 1..10 to %a, whereas 2..11 has no effect other than being the return value from the comma operator, which is thrown away. Is this correct?

        Thank you for clarifying - I forgot about that possibility.

        Rudif