-
代码块
-
Getopt::Long::GetOptions 是 Perl 编程语言中的一个模块,用于处理命令行参数。通过使用这个模块,你可以轻松地从命令行中获取和解析用户提供的选项和参数。
-
具体而言,Getopt::Long::GetOptions 模块的作用是解析命令行参数,使得你的 Perl 脚本能够识别和处理用户在命令行中提供的选项和参数。它允许你定义和处理各种类型的命令行选项,包括标志(flag)、字符串、数字等。
-
“input_file|i=s” => $input_file,
-
其中input_file和i代表指令的名字,|代表或的意思。=s,代表获得options是个字符串,同时也要写入到字符串变量中,$input_file代表是引用$input_file,即传递指针。
-
在while读取问价句柄时,会定义一个变量去存储每次读取到的内容.
-
while( my $line = <$input_file>){}
-
这样$_就不会起作用,因为它是为定义的。
-
while( <$input_file>){}
-
这样$_就存储每次读取文件句柄的内容(和$line一样)。后续如果要进行模式匹配等操作,$_可以减少代码输入。
#!/usr/bin/perl -w
use strict;
use warnings;
use Getopt::Long;
my $input_file = "";
my $output_file = "";
GetOptions(
"input_file|i=s" => \$input_file,
"output_file|o=s" => \$output_file,
);
open(my $fh_input, "<$input_file") or die("[Error]: can't open $input_file");
open(my $fh_output, ">$output_file") or die("[Error]: can't open $output_file");
while(my $line = <$fh_input>) {
next if($line =~ /^-+/);
$line =~ s/\s//g;
$line =~ s/^\|//g; # delete line header |
$line =~ s/\|$//g; # delete line tail |
$line =~ s/\|([\w\d\.\-\+]+)/,$1/g;
printf($fh_output $line."\n");
}
close($fh_input);
close($fh_output);