04. Getopt::Long::GetOptions

  • 代码块

  • 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);

  • 7
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值