如何访问嵌套 Perl 散列的值?

发布于 2024-08-30 14:17:43 字数 1521 浏览 4 评论 0原文

我是 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' );

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

我知道这是一个简单的问题,但 Perl 一开始有点神秘。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

执手闯天涯 2024-09-06 14:17:43

Narthring 的暴力方法是正确的。嵌套散列通过像这样链接键来寻址:

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

但是,因为这两个“散列”都是blessed对象。它可能有助于阅读 HTTP::Server ::Simple::Dispatched::Request,它可以告诉你这是一个 HTTP::Request 对象并查看 headermethod< 上的 HTTP::Request 部分/code> 方法,告诉您以下方法可以解决问题:

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

真的,我建议您获取 firefox 搜索插件名为 Perldoc Module::Name,当您遇到 Dumper 输出显示“bless ... 'Some::Module::Name'”时,您只需将其复制并粘贴到搜索插件中即可阅读 CPAN 上的文档。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文