pulling apart a blessed hash

S

sbk

hi,

i'm using a module which returns a reference to a blessed hash, and i'm
having trouble figuring out how to pull apart this data structure.

guru% cat test
#!/usr/bin/perl
use strict;
use warnings;
use Data::Dumper;
use Net::NBName;
my $ip = "10.1.2.3";
my $nb = Net::NBName->new;
my $ns = $nb->node_status($ip);
print Dumper($ns);

guru% ./test
$VAR1 = bless( {
'mac_address' => '8C-6B-20-5C-00-00',
[...]


but of course, i don't want use Dumper in my real program ... i want to
extract the values in this blessed hash and do useful things with them.

i haven't figured out the syntax i would need to do this.

the following seemed reasonable to be ... but they didn't work ... so
much for my sense of reasonableness:

my $temp = $ns->mac_address;
print "$temp\n";

or

my $temp = $ns->{'mac_address'};
print "$temp\n";


would anyone be willing to offer me alternate syntax?
--sk

stuart kendrick
fhcrc
 
B

Brian McCauley

sbk said:
hi,

i'm using a module which returns a reference to a blessed hash, and i'm
having trouble figuring out how to pull apart this data structure.

Blessing does not change how you can get at the internals.

However, unless the module documentation says you are allowed to access
the internals of the object directly you should not do so as subsequent
realease of the module may change the internals.
print Dumper($ns);
$VAR1 = bless( {
'mac_address' => '8C-6B-20-5C-00-00',
[...]
the following seemed reasonable to be ... but they didn't work ...
my $temp = $ns->{'mac_address'};

That should work. Please genereate a minimal but complete script to
illustrate your claim that it does not.
would anyone be willing to offer me alternate syntax?

Well, if you insist.

$$ns{mac_address}

But I still think the syntax with -> looks better.
 
X

xhoster

sbk said:
hi,

i'm using a module which returns a reference to a blessed hash, and i'm
having trouble figuring out how to pull apart this data structure.

You aren't supposed to pull apart those data structures. If you are
getting an object, you should almost certainly use it like an object.

my $nb = Net::NBName->new;
my $ns = $nb->node_status($ip);
but of course, i don't want use Dumper in my real program ... i want to
extract the values in this blessed hash and do useful things with them.

Why not use the object methods provided? Any decent module will provide
you with the methods you need to access the data. And I try not to use
indecent modules in the first place.
the following seemed reasonable to be ... but they didn't work ... so
much for my sense of reasonableness:
....
my $temp = $ns->{'mac_address'};
print "$temp\n";

What, exactly, do you mean by "didn't work"?

Xho
 
G

Gary E. Ansok

hi,

i'm using a module which returns a reference to a blessed hash, and i'm
having trouble figuring out how to pull apart this data structure.

i haven't figured out the syntax i would need to do this.

the following seemed reasonable to be ... but they didn't work ... so
much for my sense of reasonableness:

my $temp = $ns->mac_address;
print "$temp\n";

This should work, assuming that $ns is a reference to a Net::NBName::NodeStatus
object. I'm not familiar with the Net::NBName module, though.

This would generally be the preferred syntax, although personally I'd find
$ns->mac_address() a little clearer.
my $temp = $ns->{'mac_address'};
print "$temp\n";

This should also work, although you're more likely to have potential
problems caused by future changes to the module. The quotes around
mac_address aren't needed in this example.

When you say "didn't work", what happened? Did you get an error message
or warning? What did the message say? Did you get output different
than what you expected? What did you get, and what did you expect?

Did you check to make sure that both $nb and $ns are defined?
would anyone be willing to offer me alternate syntax?

foreach my $key (keys %$ns) {
print "$key => '$ns->{$key}'\n";
}

Gary Ansok
 

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

Members online

Forum statistics

Threads
473,755
Messages
2,569,535
Members
45,007
Latest member
obedient dusk

Latest Threads

Top