parsing argument in perl

in bash:

LIST=`cat $1`

for i in $LIST
do
  ...
done

how will i do this in perl ?

$1 is my first arguement. I'm a newbie in perl and will appreciate much your help guys ...

while (<>) {
  chomp; split;
  foreach $i (@_) {
    ...
  }
}

If you want to get more familiar with perl, in any case it is recommendable to study the man pages (which in case of perl are very instructive), the most basic ones are:
perlrun, perlsyn, perldata, perlop, perlfunc (second level: perlsub, perlre, perlvar)

1 Like
perl -E 'say for @ARGV' 1 'a b' --
1
a b
--
1 Like

hi hfreyer,

do you mean its like this ?

while ($ARGV[0]) {   
chomp; split;   
foreach $i (@_) {
   ...   
   }
}

how will i call my arguement1 ?

If you want just parse your arguments:

for my $arg (@ARGV) {
  # no split, no chomp
  # do what you want with your $arg-s
}

If you want to do something with lines from the file from your first argument:

open my $fd, "<", "$ARGV[0]";
while (<$fd>) {
  # the lines go into $_
  # so do something with $_
  # you can remove the newline
  chomp;
  # split it in an array
  my @fields = split;  # shortcut for split(/\s+/, $_)
}
close $fd;