0

我有一个这样的 txt 文件
$CHUNK_QTY = "50000";
$CLIENT = "大家好";
$CLIENT_CODE = "NMB";
$COMPOSER = "对话";
$CONTROL_FILE_NAME = "NMBM725.XML";
$COPY_BASE = "01";
$CSITE = "NSH";
$DATA_TYPE = "MET";
$DIALOGUE_VERSION = "V7R0M624";
$禁用 = "N";
$DPI = "300";
$DP_BAR_START = "A";
$DP_BAR_STOP = "Z";
$双工 = "N";
$Dialogue_Version = "V7R0M624";
$EMAIL_ERROR = "Y";
$EMAIL_ON = "N";

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

谢谢

4

4 回答 4

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";

rdo就像但在安全的do环境中,特别是它可以捕捉到$X = qx {rm -rf /};建筑物。如果文件通过了rdo可能可以-ed。

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

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

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";
于 2012-07-10T18:49:13.950 回答
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;
于 2012-07-10T18:55:48.030 回答
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"
于 2012-07-10T18:59:46.223 回答
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示例和完整的详细信息。

于 2012-07-10T21:19:10.327 回答