Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Perl > Perl Misc > pulling apart a blessed hash

Reply
Thread Tools

pulling apart a blessed hash

 
 
sbk
Guest
Posts: n/a
 
      01-26-2005
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:umper;
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

 
Reply With Quote
 
 
 
 
Brian McCauley
Guest
Posts: n/a
 
      01-26-2005


sbk wrote:

> 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.

 
Reply With Quote
 
 
 
 
xhoster@gmail.com
Guest
Posts: n/a
 
      01-26-2005
"sbk" <(E-Mail Removed)> wrote:
> 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

--
-------------------- http://NewsReader.Com/ --------------------
Usenet Newsgroup Service $9.95/Month 30GB
 
Reply With Quote
 
Gary E. Ansok
Guest
Posts: n/a
 
      01-26-2005
In article <(E-Mail Removed) .com>,
sbk <(E-Mail Removed)> wrote:
>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
--
Any attempt to brew coffee with a teapot should result in the error code
"418 I'm a teapot". The resulting entity body MAY be short and stout.
-- RFC 2324, Hyper Text Coffee Pot Control Protocol (HTCPCP)/1.0
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
hash of hash of hash of hash in c++ rp C++ 1 11-10-2011 04:45 PM
Being more restrictive when using blessed hash ref for OOP A. Farber Perl Misc 9 04-01-2009 02:16 PM
Pulling a Canon G3 apart Frank Digital Photography 5 01-21-2004 07:31 AM
Question about threads::shared and blessed references Nick Marden Perl 0 01-17-2004 05:16 AM



Advertisments