Hash vs. Hash ref

R

Rg

Saluton,

Please, take a look at the following Perl program:

-----------------------
#!/usr/bin/perl

use strict;

my $hash_ref = {
List => ()
};

$hash_ref->{List}[0]->{Something} = "Boo";
$hash_ref->{List}[1]->{Something_else} = "Not boo";

print "Something is $hash_ref->{List}[0]{Something}\n";
print "Something else is $hash_ref->{List}[1]{Something_else}\n";
-----------------------

The output for this program is

"Something is Boo
Something else is Not boo"

I wonder why. How comes Perl treat $hash_ref->{List}[0] both as a hash
reference and a hash? Am I missing something here? Is there "undefined
behavior" involved?
 
X

Xicheng Jia

Saluton,

Please, take a look at the following Perl program:

-----------------------
#!/usr/bin/perl

use strict;

my $hash_ref = {
List => ()

ITYM:
List => [ ],
};

$hash_ref->{List}[0]->{Something} = "Boo";
$hash_ref->{List}[1]->{Something_else} = "Not boo";

print "Something is $hash_ref->{List}[0]{Something}\n";
print "Something else is $hash_ref->{List}[1]{Something_else}\n";
-----------------------

The output for this program is

"Something is Boo
Something else is Not boo"

I wonder why. How comes Perl treat $hash_ref->{List}[0] both as a hash
reference and a hash? Am I missing something here? Is there "undefined
behavior" involved?

no, they are both references, check what's the difference between:

$hash_ref->{List}[0]{something}
$hash_ref->{List}[0]->{something}

Read "Intermediate Perl" section 4.7

Regards,
Xicheng
 
P

Paul Lalli

Saluton,

Please, take a look at the following Perl program:

-----------------------
#!/usr/bin/perl

use strict;

my $hash_ref = {
List => ()

This doesn't do what you think it does. ALWAYS enable warnings when
writing Perl code!!
};

$hash_ref->{List}[0]->{Something} = "Boo";
$hash_ref->{List}[1]->{Something_else} = "Not boo";

print "Something is $hash_ref->{List}[0]{Something}\n";
print "Something else is $hash_ref->{List}[1]{Something_else}\n";
-----------------------

The output for this program is

"Something is Boo
Something else is Not boo"

I wonder why. How comes Perl treat $hash_ref->{List}[0] both as a hash
reference and a hash?

What gives you the impression anything in the above is indicating
that? The second arrow and lack thereof?
Am I missing something here?

Yes. The "arrow rule". You should read up on how multidimensional
structures are accessed in Perl. Take a look at :
perldoc perlreftut
perldoc perllol
perldoc perldsc.
Is there "undefined behavior" involved?

Not at all. It's perfectly well defined. It's your assumption that
is in error.

Paul Lalli
 

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

Similar Threads


Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,581
Members
45,056
Latest member
GlycogenSupporthealth

Latest Threads

Top