in reply to Accessing values of a Hash object

  1. use strictures (use strict; use warnings;).
  2. $self->{i}={[(110,FB),(100,F)]}; does not make sense. What do you intend?
  3. Where are you looking to return a hash value?

The following reworked sample code may give you some hints:

use strict; use warnings; package ngetlogstdin; sub new { my $self={}; $self->{o}="ngeterr.log"; $self->{a}=undef; $self->{i}={110 => 'FB', 100 => 'F'}; $self->{F}=undef; $self->{B}='http://wibble.wobble.com'; bless $self; return $self; } sub access { my $self=shift; my $arg=shift; print $arg; # Note that "result" of print is the return value } 1; package main; my $obj=new ngetlogstdin; for my $key(keys %$obj){print "$key\n"}; my $arg='tomatoes'; my $value=$obj->access($arg); print $value;

Prints:

F a B i o tomatoes1

DWIM is Perl's answer to Gödel

Replies are listed 'Best First'.
Re^2: Accessing values of a Hash object
by dReKurCe (Scribe) on Mar 13, 2007 at 21:21 UTC
    Ok, Well I guess what I'm asking is how to get 'at' the hash from the accessor. I want to prompt for the key then print the value.

      so you want something like:

      sub access { my ($self, $arg) = @_; return $self->{$arg}; } ... my $arg='B';

      Prints (using previous sample code):

      F a B i o http://wibble.wobble.com

      DWIM is Perl's answer to Gödel