pulling apart a blessed hash

Discussion in 'Perl Misc' started by sbk, Jan 26, 2005.

  1. sbk

    sbk Guest

    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
     
    sbk, Jan 26, 2005
    #1
    1. Advertising

  2. 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.
     
    Brian McCauley, Jan 26, 2005
    #2
    1. Advertising

  3. sbk

    Guest

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


    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
     
    , Jan 26, 2005
    #3
  4. In article <>,
    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.
    >
    >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
     
    Gary E. Ansok, Jan 26, 2005
    #4
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Nick Marden
    Replies:
    0
    Views:
    1,074
    Nick Marden
    Jan 17, 2004
  2. rp
    Replies:
    1
    Views:
    556
    red floyd
    Nov 10, 2011
  3. Nick Marden
    Replies:
    1
    Views:
    142
    Ben Morrow
    Jan 20, 2004
  4. Vetle Roeim

    Threads and shared blessed references

    Vetle Roeim, Jun 9, 2004, in forum: Perl Misc
    Replies:
    0
    Views:
    169
    Vetle Roeim
    Jun 9, 2004
  5. A. Farber
    Replies:
    9
    Views:
    131
Loading...

Share This Page