我是 Perl 新手,我有一个非常简单的问题,但在查阅我的 Perl 书籍时找不到答案。
打印结果时
Dumper($request);
我得到以下结果:
$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' );
如何访问“_method”(“GET”)或“host”(“localhost:8081”)的值。
我知道这是一个简单的问题,但 Perl 一开始就有点神秘。
最佳答案
Narthring 的暴力方法是正确的。嵌套哈希通过链接键来解决,如下所示:
$hash{top_key}{next_key}{another_key}; # for %hash
# OR
$hash_ref->{top_key}{next_key}{another_key}; # for refs.
但是,由于这两个“哈希”都是有福的对象。阅读 HTTP::Server::Simple::Dispatched::Request
可能会有所帮助,它可以告诉你这是一个 HTTP::Request
对象并查看 header
和 method
方法上的 HTTP::Request
部分,会告诉您以下方法可以解决问题:
my $method = $request->method();
my $host = $request->header( 'host' );
真的,我建议您获取 firefox search plugin called Perldoc Module::Name当您遇到 Dumper 输出显示“bless ... 'Some::Module::Name'”时,您只需将其复制并粘贴到搜索插件中并阅读 CPAN 上的文档即可。
关于perl - 如何访问嵌套 Perl 散列的值?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2748738/