如何在perl中访问变量的值,其中我有多个变量及其值并以;结尾;

时间:2012-07-10 18:34:01

标签: perl variables mod-perl perl-data-structures

我有一个像这样的txt文件  $ CHUNK_QTY =" 50000";
 $ CLIENT ="大家好";
  $ CLIENT_CODE =" NMB";
  $ COMPOSER =" DIALOGUE";
  $ CONTROL_FILE_NAME =" NMBM725.XML";
  $ COPY_BASE =" 01";
  $ CSITE =" NSH";
  $ DATA_TYPE =" MET";
  $ DIALOGUE_VERSION =" V7R0M624";
  $ DISABLE =" N";
  $ DPI =" 300";
  $ DP_BAR_START =" A&#34 ;;
  $ DP_BAR_STOP =" Z&#34 ;;
  $ DUPLEX =" N";
  $ Dialogue_Version =" V7R0M624&#34 ;;
  $ EMAIL_ERROR =" Y";
  $ EMAIL_ON =" N&#34 ;;

我有很多变量,最多500个。我想访问相应变量的值。例如,如果我想访问$ DPI,它应该打印300。我如何在perl中做到这一点。任何帮助将不胜感激。我想要一些与正则表达式不同的东西。

由于

4 个答案:

答案 0 :(得分:2)

错误,错误,错误和危险的方式:

eval qx{cat filename.txt};
print "$DPI\n";

do "filename.txt";
print "$DPI\n";

所以不要这样做。

使用正则表达式解析和解开文件会更好...

如果不想要基于正则表达式的解决方案,至少可以使用Safe.pm模块:

use Safe;
my $sandbox = new Safe;
$sandbox->rdo( "filename.txt"  ) or die "safe problem $@";

#more safe now
do "filename.txt";
print "$DPI\n";

rdodo类似,但在安全的环境中,尤其是它可以捕获$X = qx {rm -rf /};构造。如果文件通过rdo可能可以执行 -ed。

当然,上面的内容也是错误的,因为TLP已经告诉你不能使用use strict;。最好的方法是解析文件。

对于基于正则表达式的解决方案,您可以使用:

use strict;
use warnings;
my $re = qr /^\s*\$(\w+)\s*=\s*"(.*)"\s*;\s*$/o;
my %conf = map { m/$re/;($1,$2) } grep {$re} <DATA>;
__END__
$CHUNK_QTY = "50000";
$CLIENT = "hi all";
$CLIENT_CODE = "NMB";
$COMPOSER = "DIALOGUE";
$CONTROL_FILE_NAME = "NMBM725.XML";
$COPY_BASE = "01";
$CSITE = "NSH";
$DATA_TYPE = "MET";
$DIALOGUE_VERSION = "V7R0M624";
$DISABLE = "N";
$DPI = "300";
$DP_BAR_START = "A";
$DP_BAR_STOP = "Z";
$DUPLEX = "N";
$Dialogue_Version = "V7R0M624";
$EMAIL_ERROR = "Y";
$EMAIL_ON = "N";

答案 1 :(得分:1)

您可以使用do来运行文件:

do "yourfile";
print $DPI;

但是,如果您在use strict下运行,则需要首先使用our声明变量:

use strict;
use warnings;

our $DPI; # plus any other variables you want to use
do "yourfile";
print $DPI;

答案 2 :(得分:1)

您可以使用hashes,这样您就可以将名称和值写入哈希列表并根据其名称检索它们。

这是一种读取文件内容并将内容放入哈希结构的方法:

my $hash = ();
open FILE, "<", "stuff.txt" or die $!;

while(<FILE>)
{
    my @attr  = split(/=/);
    #this is actually a regexp, but you can read the data in any way you want

    my $key   = $attr[0];
    my $value = $attr[1];
    #only splitting up so that it becomes easier to read

    $hash{$key} = $value;#insert key and value  
}
close (FILE);
print 'Content of $CLIENT:'.$hash{'$CLIENT'};
print 'Content of $CHUNK_QTY:'.$hash{'$CHUNK_QTY'};

文件“stuff.txt”的内容

$CLIENT="hi all"
$CHUNK_QTY="50000"

答案 3 :(得分:0)

这是一个快速示例,展示了如何以上面给出的格式获取行并将它们放入哈希:

#!/usr/bin/env perl    

use strict;
use warnings;

my %vars;
while (my $line = <DATA>) {
  chomp $line;          # remove linebreak
  $line =~ s/^\$//;     # Optional: remove $ from start of variable name

  my ($key, $value) = $line =~ /^(\w+)\s*=\s*(.*)$/;
  $value =~ s/;$//;     # Remove trailing semicolon
  $value =~ s/^"//;     # Remove leading double-quotes
  $value =~ s/"$//;     # Remove trailing double-quotes
  $vars{$key} = $value;
} 

for my $key (sort keys %vars) {
  print "$key has value $vars{$key}\n";
}

print "CLIENT says $vars{CLIENT}\n";

__DATA__
$CHUNK_QTY = "50000";
$CLIENT = "hi all";
$CLIENT_CODE = "NMB";
$COMPOSER = "DIALOGUE";
$CONTROL_FILE_NAME = "NMBM725.XML";
$COPY_BASE = "01";
$CSITE = "NSH";
$DATA_TYPE = "MET";
$DIALOGUE_VERSION = "V7R0M624";
$DISABLE = "N";
$DPI = "300";
$DP_BAR_START = "A";
$DP_BAR_STOP = "Z";
$DUPLEX = "N";
$Dialogue_Version = "V7R0M624";
$EMAIL_ERROR = "Y";
$EMAIL_ON = "N";

这里应该有足够的内容让你开始,但你需要了解如何打开一个实际的文件(而不是像我在这里一样使用__DATA__部分)。我建议您查看perldoc open示例和详细信息。