10

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.

Axeman
  • 29,660
  • 2
  • 47
  • 102
st.
  • 156
  • 1
  • 6
  • If you aren't sure how to use an object, you should be reading the documentation for the object you are using *before* reading for Data::Dumper. Please heed Axeman's answer. – Ether May 01 '10 at 16:34

1 Answers1

13

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.

Ether
  • 53,118
  • 13
  • 86
  • 159
Axeman
  • 29,660
  • 2
  • 47
  • 102
  • 2
    +1. I have often wished to ban the use of Data::Dumper by beginner Perl programmers, as it opens up every object to prying eyes and it becomes easier to simply reach into the object and grab the desired value from the hash rather than *reading the documentation* and using the proper accessor methods. What will you do when the field representation changes in the next revision? It is a Perl convention that **private values begin with underscore** and **should never be accessed directly**. – Ether May 01 '10 at 16:33
  • 1
    Data::Dumper is very useful, but particularly invaluable to beginners. – Pedro Silva May 02 '10 at 00:46
  • 1
    @Pedro Silva: I agree. It's a trade-off really. I've seen some coders bang up against the wall. *If only they could see what the DBI call was returning*. The guy I showed DD to, was so grateful, and it greatly sped his project. It's definitely better than feeling around a dark room. Since finding [Smart::Comments](http://search.cpan.org/perldoc?Smart::Comments), I've been recommending that to fellow developers, as a close-to-effortless way of being able to see what your code is really doing. However, showing the bones of what are supposed to be "objects" has its drawbacks, too. – Axeman May 02 '10 at 01:43