in reply to Loading all .txt files within current directory
As they stand, both the one-liner and the emitted script iterate over the command-line arguments as they appear after file name expansion.
perldoc -v ARGV says that ARGV is a special filehandle that iterates over the file names in @ARGV when specified inside angle brackets, and that is where the iteration takes place.
Your attempted modification of the emitted script to always iterate over *.txt is almost correct. What I think is needed is to remove the "my"; that is, the line should simply be
@ARGV = glob( '*.txt' )The thing is, Perl has two completely different places for variables to live. Lexical variables are created using 'my', and are accessible only within the lexical scope of the 'my'. Global variables are created when 'my' is not specified, live in a name space ('main' by default), and are accessible from anywhere (including other name spaces if you fully-qualify the name). "Magic" variables like @ARGV are global, and in fact are typically forced into the 'main' name space.
Novice Perl programmers are encouraged to specify "my" because lexical variables minimize the chance for unplanned and unexpected interactions between different parts of the code. But in this case you want to modify the global @ARGV, because that is the one with the magic connection to ARGV. By specifying "my" you create a lexical @ARGV, which hides the global one and has no magic attached.
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^2: Loading all .txt files within current directory
by TJCooper (Beadle) on Jan 31, 2016 at 13:41 UTC | |
by poj (Abbot) on Jan 31, 2016 at 13:54 UTC | |
by TJCooper (Beadle) on Jan 31, 2016 at 15:34 UTC |