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

  2. 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.
    That should work. Please genereate a minimal but complete script to
    illustrate your claim that it does not.
    Well, if you insist.

    $$ns{mac_address}

    But I still think the syntax with -> looks better.
     
    Brian McCauley, Jan 26, 2005
    #2
    1. Advertisements

  3. sbk

    xhoster Guest

    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.

    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.
    What, exactly, do you mean by "didn't work"?

    Xho
     
    xhoster, Jan 26, 2005
    #3
  4. 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.
    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?
    foreach my $key (keys %$ns) {
    print "$key => '$ns->{$key}'\n";
    }

    Gary Ansok
     
    Gary E. Ansok, Jan 26, 2005
    #4
    1. Advertisements

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 (here). After that, you can post your question and our members will help you out.