in reply to Re: Initializing Hash Arrays in Perl
in thread Initializing Hash Arrays in Perl

Kenosis and Davido, you both hit the nail on the head. The purpose of it is precisely what Kenosis said as well. I understand the hash creation now but the ++ seems very odd to me. How can the $refr_url be initialized as ++? It has not been defined as anything yet. Is it something like: undef++ which evaluates to 1? I don't understand how a key's value can be incremented when it hasn't even been defined as anything yet.

Replies are listed 'Best First'.
Re^3: Initializing Hash Arrays in Perl
by Athanasius (Archbishop) on Feb 14, 2014 at 02:19 UTC
Re^3: Initializing Hash Arrays in Perl
by davido (Cardinal) on Feb 14, 2014 at 04:35 UTC

    You've pretty much got it. In Perl, all scalar containers (ie, a scalar variable, an element from an array, or an element from a hash) start out as "undef", which is different from C's notion of undefined. In C, undefined means "could be anything, but never anything useful". In Perl, undefined means "undef", the value. In numeric context, undef equates to zero, meaning if you treat undef as a number it acts like zero. Increment zero, and you get 1.

    You can explicitly detect definedness with defined:

    perl -E 'say "undefined" if ! defined($var);'

    ...or...

    perl -E '$var = 1; say "defined" if defined($var);'

    But numeric operations, including Boolean comparisons in numeric context cause undef to act like a "false" numeric value, which is to say, zero.

    perl -E 'say "Hello" if undef == 0;'

    ...or...

    my $var; # Currently $var is undef print $var + 5, "\n"; # 5 print ++$var, "\n"; # 1

    That's really the less amazing thing. The more amazing thing is that you can implicitly create references to anonymous data structures. Consider this:

    my %hash; $hash{foo} = { bar => 'baz' };

    The { ... } curly braces around  bar => 'baz' act as explicit anonymous hash constructors, so the resulting structure looks like this:

    %hash = ( foo => { # { is an anon-hash constructor bar => 'baz' } );

    That's the boring explicit way. But this will create the exact same structure, without explicitly constructing the inner hashref:

    my %hash; $hash{foo}{bar} = 'baz';

    The { bar => 'baz'} hash ref was constructed implicitly.


    Dave