Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/perl/11.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/variables/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何从脚本中提取所有全局变量并在Perl中获取每种数据类型?_Perl_Variables_Reflection_Reference - Fatal编程技术网

如何从脚本中提取所有全局变量并在Perl中获取每种数据类型?

如何从脚本中提取所有全局变量并在Perl中获取每种数据类型?,perl,variables,reflection,reference,Perl,Variables,Reflection,Reference,我喜欢用Perl从外部Perl脚本捕获所有全局变量。目前我正在进行类型检测 如何确定正确的数据类型(“标量”、“散列”、“数组”、“代码”) 分析器脚本: my %allVariables = (); { do "scriptToBeParsed.pl"; foreach my $sym ( keys %main:: ) { # Get all normal variables and scalar/hash/array references:

我喜欢用Perl从外部Perl脚本捕获所有全局变量。目前我正在进行类型检测

如何确定正确的数据类型(“标量”、“散列”、“数组”、“代码”)

分析器脚本:

my %allVariables = ();
{
     do "scriptToBeParsed.pl";
     foreach my $sym ( keys %main:: ) {
         # Get all normal variables and scalar/hash/array references:
        if ( ref( *{"$sym"} ) =~ m/^(?:|SCALAR|HASH|ARRAY)$/ ) {
            $allVariables{"$sym"} = *{"$sym"};
        }
    }
}
要分析的脚本:

$someVariable1 = 'Yes, I like to be captured';
$otherVariable2 = \'And I also want to be captured';
%anotherVariable3 = ( 'Capture' => 'me' );
@lameVariable4 = ( 'Capture', 'me' );
$fooVariable5 = { 'Capture' => 'me' };
$barVariable6 = [ 'Capture', 'me' ];
$subVariable7 = sub { return "Don't capture me!" };
sub dontCaptureMe { return "Don't capture me!" }
在我的示例中,
ref(*“$sym”})
总是返回“GLOB”(当然)。

如您所说

ref(*“$sym”})总是返回“GLOB”(当然)

因为perl将符号表中的所有内容都存储在glob中,所以不可能判断某个数据类型。这是因为在perl中,数组、标量、散列或其他任何具有相同名称的内容都是完全有效的。。。因此,perl将所有内容存储在globs中以避免冲突。您可以做的是循环遍历symbol表中的所有符号,并针对每个glob可能是的所有可能的东西(集合不是太大)测试每个glob,然后查看设置了哪些

或者,一种更实用的方法可能是将perl脚本作为文本加载,并对
$
%
@
子类
打开
(文件句柄)进行解析,以查看所有内容的类型

就像你说的

ref(*“$sym”})总是返回“GLOB”(当然)

因为perl将符号表中的所有内容都存储在glob中,所以不可能判断某个数据类型。这是因为在perl中,数组、标量、散列或其他任何具有相同名称的内容都是完全有效的。。。因此,perl将所有内容存储在globs中以避免冲突。您可以做的是循环遍历symbol表中的所有符号,并针对每个glob可能是的所有可能的东西(集合不是太大)测试每个glob,然后查看设置了哪些


或者,一种更实用的方法可能是将perl脚本作为文本加载,并对
$
%
@
子类
打开
(文件句柄)进行解析,以查看所有内容的类型

另一种方法是使用类型glob的has-like访问,这在brian d foy的《精通Perl》第8章第131f页进行了解释

package test;
no strict;
no warnings; 

$someVariable1 = 'Yes, I like to be captured';
$otherVariable2 = \'And I also want to be captured';
%anotherVariable3 = ( 'Capture' => 'me' );
@lameVariable4 = ( 'Capture', 'me' );
$fooVariable5 = { 'Capture' => 'me' };
$barVariable6 = [ 'Capture', 'me' ];
$subVariable7 = sub { return "Don't capture me!" };
sub dontCaptureMe { return "Don't capture me!" }

say $dontCaptureMe;
my %allVariables = ();
{
  do "scriptToBecomeParsed.pl";
  foreach my $sym ( keys %test:: ) {
    for (qw( SCALAR HASH ARRAY CODE IO)) {
      if (*{"$sym"}{$_}) {
        $allVariables{$_}->{"$sym"} = *{"$sym"}{$_};
      }
    }
  }
}

print Data::Dumper::Dumper \%allVariables;
这将产生以下输出:

$VAR1 = {
          'CODE' => {
                      'dontCaptureMe' => sub { "DUMMY" }
                    },
          'ARRAY' => {
                       'lameVariable4' => [
                                            'Capture',
                                            'me'
                                          ]
                     },
          'HASH' => {
                      'anotherVariable3' => {
                                              'Capture' => 'me'
                                            }
                    },
          'SCALAR' => {
                        'someVariable1' => \'Yes, I like to be captured',
                        '__ANON__' => \undef,
                        'subVariable7' => \sub { "DUMMY" },
                        'dontCaptureMe' => \undef,
                        'otherVariable2' => \\'And I also want to be captured',
                        'BEGIN' => \undef,
                        'barVariable6' => \[
                                              'Capture',
                                              'me'
                                            ],
                        'anotherVariable3' => \undef,
                        'lameVariable4' => \undef,
                        'fooVariable5' => \{
                                              'Capture' => 'me'
                                            }
                      }
        };

另一种方法是使用类型glob的has-like访问,这在第131f页brian d foy的精通Perl的第8章中进行了解释

package test;
no strict;
no warnings; 

$someVariable1 = 'Yes, I like to be captured';
$otherVariable2 = \'And I also want to be captured';
%anotherVariable3 = ( 'Capture' => 'me' );
@lameVariable4 = ( 'Capture', 'me' );
$fooVariable5 = { 'Capture' => 'me' };
$barVariable6 = [ 'Capture', 'me' ];
$subVariable7 = sub { return "Don't capture me!" };
sub dontCaptureMe { return "Don't capture me!" }

say $dontCaptureMe;
my %allVariables = ();
{
  do "scriptToBecomeParsed.pl";
  foreach my $sym ( keys %test:: ) {
    for (qw( SCALAR HASH ARRAY CODE IO)) {
      if (*{"$sym"}{$_}) {
        $allVariables{$_}->{"$sym"} = *{"$sym"}{$_};
      }
    }
  }
}

print Data::Dumper::Dumper \%allVariables;
这将产生以下输出:

$VAR1 = {
          'CODE' => {
                      'dontCaptureMe' => sub { "DUMMY" }
                    },
          'ARRAY' => {
                       'lameVariable4' => [
                                            'Capture',
                                            'me'
                                          ]
                     },
          'HASH' => {
                      'anotherVariable3' => {
                                              'Capture' => 'me'
                                            }
                    },
          'SCALAR' => {
                        'someVariable1' => \'Yes, I like to be captured',
                        '__ANON__' => \undef,
                        'subVariable7' => \sub { "DUMMY" },
                        'dontCaptureMe' => \undef,
                        'otherVariable2' => \\'And I also want to be captured',
                        'BEGIN' => \undef,
                        'barVariable6' => \[
                                              'Capture',
                                              'me'
                                            ],
                        'anotherVariable3' => \undef,
                        'lameVariable4' => \undef,
                        'fooVariable5' => \{
                                              'Capture' => 'me'
                                            }
                      }
        };

脚本中唯一需要解析的“全局”是
sub dontCaptureMe
;“其他的都是词汇……你说得对,@pavel。我的例子不正确。编辑它。脚本中唯一要分析的“全局”是
sub dontCaptureMe
;“其他的都是词汇……你说得对,@pavel。我的例子不正确。我想你的意思是“交替”而不是“头韵”。至少,在你的最后一段中,我没有看到一个令人敬畏的头韵答案,至少我从中学到了一些东西。我甚至不知道“头韵”是一个词。我几乎想把剩下的答案变成头韵,而不是把它改成“另类”,但我觉得那样会毁了它的质量answer@John我认为这根本不可能。我向你挑战,让你做这件事,并把它张贴在:Dit是一个形容词,用来描述头韵,它是一个声音在紧密排列在一起的单词中的重复。就像“深陷黑暗凝视,我长时间站在那里,疑惑、恐惧、怀疑,做着以前任何人都不敢做的梦。”重复“D”使它成为头韵。我想你的意思是“交替”而不是“头韵”。至少,在你的最后一段中,我没有看到一个令人敬畏的头韵答案,至少我从中学到了一些东西。我甚至不知道“头韵”是一个词。我几乎想把剩下的答案变成头韵,而不是把它改成“另类”,但我觉得那样会毁了它的质量answer@John我认为这根本不可能。我向你挑战,让你做这件事,并把它张贴在:Dit是一个形容词,用来描述头韵,它是一个声音在紧密排列在一起的单词中的重复。就像“深陷黑暗凝视,我长时间站在那里,疑惑、恐惧、怀疑,做着以前没人敢做的梦。”重复“D”使它成为头韵。