【转载】perl接受传递参数的方法

时间:2023-03-09 14:36:10
【转载】perl接受传递参数的方法

#! /usr/bin/perl

use Getopt::Std;
use warnings;
use strict;

sub read_from_sh($) {
my $file = shift;
my @files = ();
open F, $file or die "Could not open $file: $!";
while (<F>) {
next if /^\s*$/;
push @files, $_;
}
close F or die "Could not close $file: $!";
return @files;
}

my @files;
my %opts = ();
getopts("s:", \%opts);
if ($opts{'s'}) {
@files = read_from_sh($opts{'s'});
}else {
@files = @ARGV;
}
for my $file (@files) {
print "file: $file\n";
}

export.pl同级目录下: chage.csv chage2.csv txt.txt t.txt

>perl export.pl chage.csv chage2.csv txt.txt t.txt

OUTPUT:

file:export.pl

file:chage.csv

chage2.csv

file:txt.txt

file:t.txt