Consider the following silly Perl program:
$firstarg = $ARGV[0];
print $firstarg;
$input = <>;
print $input;
I run it from a terminal like:
perl myprog.pl sample_argument
And get this error:
Can't open sample_argument: No such file or directory at myprog.pl line 5.
Any ideas why this is? When it gets to the <> is it trying to read from the (non-existent) file, “sample_argument” or something? And why?
<>is shorthand for “read from the files specified in@ARGV, or if@ARGVis empty, then read fromSTDIN“. In your program,@ARGVcontains the value("sample_argument"), and so Perl tries to read from that file when you use the<>operator.You can fix it by clearing
@ARGVbefore you get to the<>line: