Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I access a value of a nested Perl hash?

I am new to Perl and I have a problem that's very simple but I cannot find the answer when consulting my Perl book.

When printing the result of

Dumper($request);

I get the following result:

$VAR1 = bless( {
             '_protocol' => 'HTTP/1.1',
             '_content' => '',
             '_uri' => bless( do{\(my $o = 'http://myawesomeserver.org:8081/counter/')}, 'URI::http' ),
             '_headers' => bless( {
                                    'user-agent' => 'Mozilla/5.0 (X11; U; Linux i686; en; rv:1.9.0.4) Gecko/20080528 Epiphany/2.22 Firefox/3.0',
                                    'connection' => 'keep-alive',
                                    'cache-control' => 'max-age=0',
                                    'keep-alive' => '300',
                                    'accept' => 'text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8',
                                    'accept-language' => 'en-us,en;q=0.5',
                                    'accept-encoding' => 'gzip,deflate',
                                    'host' => 'localhost:8081',
                                    'accept-charset' => 'ISO-8859-1,utf-8;q=0.7,*;q=0.7'
                                  }, 'HTTP::Headers' ),
             '_method' => 'GET',
             '_handle' => bless( \*Symbol::GEN0, 'FileHandle' )
           }, 'HTTP::Server::Simple::Dispatched::Request' );

How can I access the values of '_method' ('GET') or of 'host' ('localhost:8081').

I know that's an easy question, but Perl is somewhat cryptic at the beginning.

like image 229
st. Avatar asked May 01 '10 03:05

st.


People also ask

How do I access nested hash?

Accessing a specific element in a nested hash is very similar to a nested array. It is as simple as calling hash[:x][:y] , where :x is the key of the hash and :y is the key of the nested hash.

How can we access hash elements in Perl?

A hash is a set of key/value pairs. Hash variables are preceded by a percent (%) sign. To refer to a single element of a hash, you will use the hash variable name preceded by a "$" sign and followed by the "key" associated with the value in curly brackets..

How get key from hash value in Perl?

Extracting Keys and Values from a Hash variable The list of all the keys from a hash is provided by the keys function, in the syntax: keys %hashname . The list of all the values from a hash is provided by the values function, in the syntax: values %hashname . Both the keys and values function return an array.

How do I read a hash file in Perl?

use Data::Dumper; use File::Slurp; my %hash = read_file("output.pl"); print Dumper(keys(%hash));


1 Answers

Narthring has it right as far as the brute force method. Nested hashes are addressed by chaining the keys like so:

$hash{top_key}{next_key}{another_key}; # for %hash
# OR
$hash_ref->{top_key}{next_key}{another_key}; # for refs.

However since both of these "hashes" are blessed objects. It might help reading up on HTTP::Server::Simple::Dispatched::Request, which can tell you that it's a HTTP::Request object and looking at HTTP::Request section on the header and method methods, tells you that the following do the trick:

my $method = $request->method();
my $host   = $request->header( 'host' );

Really, I recommend you get the firefox search plugin called Perldoc Module::Name and when you encounter Dumper output that says "bless ... 'Some::Module::Name'" you can just copy and paste it into the search plugin and read the documentation on CPAN.

like image 150
Axeman Avatar answered Nov 04 '22 13:11

Axeman