如何获取在Perl子例程中经过硬编码的值?

时间:2018-06-20 03:36:51

标签: perl

我有一个像这样的perl代码:

use constant OPERATING_MODE_MAIN_ADMIN  => 'super_admin';
use constant OPERATING_MODE_ADMIN       => 'admin';
use constant OPERATING_MODE_USER        => 'user';
sub system_details
{
    return {
        operating_modes => {
            values => [OPERATING_MODE_MAIN_ADMIN, OPERATING_MODE_ADMIN, OPERATING_MODE_USER],
            help   => {
                'super_admin'  => 'The system displays the settings for super admin',
                'admin' => 'The system displays settings for normal admin',
                'user' => 'No settings are displayed. Only user level pages.'
            }
        },
        log_level => {
            values => [qw(FATAL ERROR WARN INFO DEBUG TRACE)],
            help   => "http://search.cpan.org/~mschilli/Log-Log4perl-1.49/lib/Log/Log4perl.pm#Log_Levels"
        },
    };
}

如何从另一个子例程访问每个键的“值”字段和“帮助”字段?假设我要单独使用operating_mode或log_level的值?

2 个答案:

答案 0 :(得分:2)

system_details()返回一个hashref,该hashref具有两个值为hashrefs的键。因此,您可以取消引用子项的返回值并将其分配给哈希,然后提取所需的内容

my %sys = %{ system_details() };

my @loglevel_vals = @{ $sys{log_level}->{values} };

my $help_msg = $sys{log_level}->{help};

@loglevel_vals数组包含FATALERROR等,而$help_msg具有消息字符串。

这会产生一个额外的散列副本,而哈希表可以与引用一起使用,如doimen's answer

my $sys = system_details();
my @loglevel_vals = @{ $sys->{log_level}->{values} };

但是,由于目的是在另一个子目录中查询数据,因此使用本地副本也很有意义,这通常比较安全(以防意外更改调用者中的数据)。

有些模块通过显示来帮助解密复杂的数据结构。这有助于设计处理数据的方法。经常引用Data::Dumper,它不仅可以显示数据。其他一些仅用于显示数据。几个不错的是Data::DumpData::Printer

答案 1 :(得分:2)

  {
 "Messages" : {
   "pranavk28" : {
     "sunilk26" : {
       "-LFRGvG86BpKNYBONRKX" : {
         "msg" : "hii",
         "time" : 1529482819019,
         "type" : 1
       },
       "-LFRH3nTMYv-QcqXkFFb" : {
         "msg" : "how are you?",
         "time" : 1529482858064,
         "type" : 1
       },
       "-LFRIWJvmACC61niuN07" : {
         "msg" : "How was your day?",
         "time" : 1529483237050,
         "type" : 1
       }
     }
   },
   "sunilk26" : {
     "pranavk28" : {
       "-LFRGvG9JJcpprSKz8YB" : {
         "msg" : "hii",
         "time" : 1529482819020,
         "type" : 2
       },
       "-LFRH3nTMYv-QcqXkFFc" : {
         "msg" : "how are you?",
         "time" : 1529482858064,
         "type" : 2
       },
       "-LFRIWJwEJ_ypA1zeBXt" : {
         "msg" : "How was your day?",
         "time" : 1529483237051,
         "type" : 2
       }
     }
   }
 },
 "Users" : {
   "pranavk28" : {
     "email" : "pranavk28@gmail.com",
     "name" : "Pranav Bhardwaj",
     "password" : "priyamkr",
     "username" : "pranavk28"
   },
   "rockykbc" : {
      "email" : "rocky6@hotmail.com",
      "name" : "Ravi Kumar",
      "password" : "neetukbc",
      "username" : "rockykbc"
    },
    "sunilk26" : {
      "email" : "thakur@gmail.com",
      "name" : "D K Thakur",
      "password" : "pass1234",
      "username" : "sunilk26"
    }
  }
}

如果您需要自省存储在帮助中的结构类型(例如my $sys = system_details; my $log_level = $sys->{'log_level'}; my @values = @{ $log_level->{'values'} }; my $help = $log_level->{'help'}; 中的help是一个哈希,而在operating_mode中则是一个字符串),请使用{{ 3}}内置函数。