0

Hi I'm trying to use the code written in this answer https://stackoverflow.com/a/1712480/1740992 :

foreach (@ARGV){
   print "file: $_\n";
 # open your file here...
   #..do something
 # close your file
}

I don't know how to refer to the argument. When my scipt was just running on one file I open it by running:

$kml = "adair.kml";
open INPUT, "<$kml";

what do I replace my filename with? I've tried $ARGV[n]

thanks

Community
  • 1
  • 1
whatahitson
  • 355
  • 1
  • 9
  • 26

3 Answers3

3

You're already using it, it is $_.

You can use a named variable instead with:

foreach my $foo (@ARGV){
Quentin
  • 914,110
  • 126
  • 1,211
  • 1,335
2
for my $arg (@ARGV) {
    open my $fh, '<', $arg or die "Cannot open '$arg': $!";
    # ...
    close $fh;
 }
Sinan Ünür
  • 116,958
  • 15
  • 196
  • 339
0

Your code block:

$kml = "adair.kml";
open INPUT, "<$kml";

should be replaced with:

use strict;
use warnings;

my $kml = $ARGV[0];
open my $input_fh, '<', $kml or die "Couldn't open $kml $!";

if your code is only going to run with one argument.

dms
  • 797
  • 1
  • 5
  • 15