Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl Monk, Perl Meditation
 
PerlMonks  

Perl Syntax

by kbfiles (Initiate)
on Oct 30, 2013 at 20:47 UTC ( #1060446=perlquestion: print w/replies, xml ) Need Help??

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

I have some basic perl syntax questions from this beginner. I'm declaring an anonymous array in line 1, but I don't understand what the "0+" is doing in line 2? Also, in line 3, how is the assignment occurring for both $a and $b? As a scalar or an array element?

my $new = [$a, $b] $myhash1{0+ $new} = $new; $myhash2{$_} = $new for $a, $b;

Replies are listed 'Best First'.
Re: Perl Syntax
by davido (Cardinal) on Oct 30, 2013 at 21:19 UTC

    0+ is a numeric operation; add 0 to $new. And $new contains an array reference, which stringifies as something like ARRAY(0x1427fc8), and numerifies as something like 28299208. So 0+ forces a numeric representation of the array reference held in $new.

    Far more than you ever wanted to know (but you did ask, so...) about stringification and numerification is detailed in perldata. Perl's "duck typing" means that if you treat something like a string, Perl will do what it can to make a string of it. If you treat something as a number, Perl will do what it can to make a number of it.


    Dave

      Thank you Dave. I thought it was doing something like as you explained. I just needed some confirmation from more experienced people like you. In regards to my other question, what is the syntax doing when you use:

       $hash{$_} = $new for $a, $b;

      Is this assigning the value of $a and $b as keys for $new as the value in the hash table?

        Inside a foreach loop (for is synonymous with foreach) the $_ variable is aliased to each element of the list over which you are iterating. Sometimes it's called the "topic" variable, because it is the topic of each loop iteration unless you explicitly specify another variable. The code you demonstrated is equivalent to this:

        foreach ( $a, $b ) { $hash{$_} = $new; }

        ...which is about the same as...

        foreach my $item_alias ( $a, $b ) { $hash{$item_alias} = $new; }

        ...which can be unrolled as...

        $hash{$a} = $new; $hash{$b} = $new;

        A foreach loop that contains a simple statement in its block can be inverted to $hash{$_} = $new foreach $a, $b;, and further shortened with for. This should be discussed in greater (and possibly more accurate) detail in perlsyn.


        Dave

        It is equivalent to:
        $hash{$a} = $new; $hash{$b} = $new;

                     When in doubt, mumble; when in trouble, delegate; when in charge, ponder. -- James H. Boren

Re: Perl Syntax
by SuicideJunkie (Vicar) on Oct 30, 2013 at 20:59 UTC

    The best way to find out is to print the results as you go:

    c:>  perl -e "my $new = [$a,$b];  print qq*new is: * .$new . qq*\n 0+new is:*. (0+$new). qq*\n*; which gives:
    new is: ARRAY(0x2491b4) 0+new is:2396596

    As for line three, note that there is a for in there which loops over ($a, $b).

      @Suicide - I'm not trying to find what the value is but what does it do? What does 0+ do to $new?

        Uggh ... that is one bad code smell ... basically in that context you're getting the memory address of the reference value and using it as the key for the hash.

        -derby
Re: Perl Syntax
by talexb (Chancellor) on Oct 31, 2013 at 15:06 UTC

    Since no one's said it yet, I'll mention that $a and $b aren't usually used as variable names in Perl, as they are special variables inside a sort block.

    And, as other people have mentioned, this code smells badly. Very badly.

    Alex / talexb / Toronto

    Thanks PJ. We owe you so much. Groklaw -- RIP -- 2003 to 2013.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://1060446]
Front-paged by Corion
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others making s'mores by the fire in the courtyard of the Monastery: (3)
As of 2023-02-03 00:17 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    I prefer not to run the latest version of Perl because:







    Results (24 votes). Check out past polls.

    Notices?