我如何访问嵌套的Perl哈希值? [英] How do I access a value of a nested Perl hash?
问题描述
我是Perl的新手,我有一个非常简单的问题,但在咨询我的Perl书籍时找不到答案。
打印
p> 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,恩; 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'=>祝福(\ * Symbol :: GEN0,'FileHandle')
},'HTTP :: Server :: Simple :: Dispatched :: Request');
如何访问'_method'('GET')或'host'的值( 'localhost:8081')。
我知道这是一个简单的问题,但Perl在开始的时候有些神秘。 h2_lin>解决方案
Narthring拥有和 brute force 方法一样的权利。嵌套哈希通过链接如下的键来解决: #for%hash
#或
$ hash_ref-> {top_key} {next_key} {another_key}; #供参考。
然而,因为这两个哈希都是 对象。它可能有助于阅读 HTTP :: Server :: Simple :: Dispatched :: Request
,它可以告诉你它是一个 HTTP :: Request
对象并查看 HTTP :: Request
标题和方法
方法的
my $ method = $ request-> method();
my $ host = $ request-> header('host');
真的,我建议您获取 firefox搜索插件,称为Perldoc Module :: Name ,当您遇到Dumper输出时,显示bless ...某些:: Module ::名字''你可以复制并粘贴到搜索插件并阅读CPAN上的文档。
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.
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.
这篇关于我如何访问嵌套的Perl哈希值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!