无法解析文本文件。我想写一个新的文件文本,从其他文件文本中取一些单词

时间:2016-01-14 09:28:04

标签: perl parsing

无法解析文本文件。我想写一个新的文件文本,从其他文件文本中取一些单词。它是用Perl编写的。 VSS_Report是原始文件的handel,SVN_Report是创建文件的句柄。

use 5.010;
use strict;
use warnings;
use FileHandle;


my $VSS_Report_Path = <STDIN>;

chomp $VSS_Report_Path;

my $SVN_Report_Path = 'C:\Users\Maurizio Ambroselli\Desktop\PerlEsercitazione\SVN_Users.ini';

if (open SVN_REPORT, "+>>", $SVN_Report_Path) {

     # Parsing del File Vss_Report

        foreach my $val (VSS_REPORT){

if ((substr my $val, 0, 11) eq 'VSS Path: $'){
                    my $Projects_temp = (substr $val, 11);
                    print SVN_REPORT my $Project_temp;
                    }

                if ((substr my $val, 0, 6) eq "User: "){
                    my $Users_temp = (substr $val, 6);
                    print SVN_REPORT "$Users_temp=";
                    }

                if ((substr my $val, 0, 13) eq "Permissions: "){
                    my $Permissions_temp = (substr $val, 13);
                    if ($Permissions_temp eq " ") {print SVN_REPORT " \n";}
                    if ($Permissions_temp eq "Read") {print SVN_REPORT "r\n";}
                    else {print SVN_REPORT "rw\n";}
                }                                                   
        }

    close SVN_REPORT;

}

我收到此错误:

C:\Users\Maurizio Ambroselli\Desktop\PerlEsercitazione>perl "new 1.pl" Global symbol "$Project_temp" requires explicit package name at new 1.pl line 58. Bareword "VSS_REPORT" not allowed while "strict subs" in use at new 1.pl line 55. Execution of new 1.pl aborted due to compilation errors.

我哪里错了?

3 个答案:

答案 0 :(得分:1)

这不是错误。它的警告。您在foreach循环中使用了VSS_REPORT,而文件句柄名称是SVN_REPORT

你不能在foreach循环中使用这样的文件句柄。将其保存在<>中或创建一个文件句柄数组并使用该数组。

foreach my $val (<SVN_REPORT>) {
    # do something
}

my @VSS_REPORT = <SVN_REPORT>;
foreach my $val (@VSS_REPORT) {
    # do something
}

我建议总是使用lexical filehandle $svn_report

答案 1 :(得分:1)

通过将所有句柄VSSREPORT和SVNREPORT放在逗号&#34;&#34;之间,无论它们出现在脚本中,都解决了这个问题。

答案 2 :(得分:1)

你的代码有点困惑。您打开$ SVN_Report_Path但从未打开VSS_REPORT。哪个文件是源,您希望写入哪个文件。您从STDIN获取文件路径的目的是什么?

<强> 更新

好的,现在我更清楚你在做什么。这是一个整理的脚本版本。

你能举一些你的VSS文件的例子吗?

use 5.010;
use strict;
use warnings;
use FileHandle;

print "Please enter VSS Report Path/Filename > ";
my $VSS_Report_Path = <STDIN>;

chomp $VSS_Report_Path;

open my $VSS_Report, '<', $VSS_Report_Path or die "Could not read from $VSS_Report_Path: $!\n";

my $SVN_Report_Path = 'C:\Users\Maurizio Ambroselli\Desktop\PerlEsercitazione\SVN_Users.ini';

if (open my $SVN_Report, "+>>", $SVN_Report_Path) { 
    # Parsing del File Vss_Report

    while ( my $val = <$VSS_Report> ) {

        if ((substr my $val, 0, 11) eq 'VSS Path: $'){
            my $Projects_temp = (substr $val, 11);
            print $SVN_Report my $Project_temp;
        }

        if ((substr my $val, 0, 6) eq "User: "){
            my $Users_temp = (substr $val, 6);
            print $SVN_Report "$Users_temp=";
        }

        if ((substr my $val, 0, 13) eq "Permissions: "){
            my $Permissions_temp = (substr $val, 13);
            if ($Permissions_temp eq " "   ) {print SVN_REPORT " \n";}
            if ($Permissions_temp eq "Read") {print SVN_REPORT "r\n";}
            else {print $SVN_Report "rw\n";}
        }                                                   
    }
}
else {
    say "openning $SVN_Report_Path failed: $!";
}