Reputation: 2331
I want to do something like
for i in (1..100)
do
./perlScript.pl
done
where perlScript.pl will open a file handle like
#!/usr/bin/perl -w
use strict;
my $file = 'info${i}.txt';
my @lines = do {
open my $fh, '<', $file or die "Can't open $file -- $!";
<$fh>;
};
I would just like some advice one how to access that letter I from inside the script. Even if I could pass in the txt file as a parameter and then access it like $1 or something
Thank-you
Upvotes: 1
Views: 195
Reputation: 6592
You can pass command line arguments to perl, they will show up in the special array @ARGV
.
Basic Command Line Argument Passing
# In bash
./perlScript.pl 123
# In perl
my ($num) = $ARGV[0]; # The first command-line parameter [ 123 ]
Many Positional Command Line Arguments
# In bash
./perlScript.pl 123 456 789 foo bar
# In perl
my ($n1,$n2,$n3,$str1,$str2) = @ARGV; # First 5 command line arguments will be captured into variables
Many Command Line Flags
# In bash
./perlScript.pl --min=123 --mid=456 --max=789 --infile=foo --outfile=bar
# In perl
use Getopt::Long;
my ($min,$mid,$max,$infile,$outfile,$verbose);
GetOptions(
"min=i" => \$min, # numeric
"mid=i" => \$mid, # numeric
"max=i" => \$mix, # numeric
"infile=s" => \$infile, # string
"outfile=s" => \$outfile, # string
"verbose" => \$verbose, # flag
) or die("Error in command line arguments\n");
Environmental Variables
# In bash
FOO=123 BAR=456 ./perlScript.pl 789
# In perl
my ($foo) = $ENV{ FOO } || 0;
my ($bar) = $ENV{ BAR } || 0;
my ($baz) = $ARGV[0] || 0;
perldoc perlvar - details about @ARGV
and %ENV
Upvotes: 5