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.


In reply to Re: Loading all .txt files within current directory by Anonymous Monk
in thread Loading all .txt files within current directory by TJCooper

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.