in reply to A bizarre way to get a list of filenames

The answers to most "how does this operator work?" questions can be found in perlop. In this case it's described in the IO Operators section of that document, where this is explained:

If what's within the angle brackets is neither a filehandle nor a simple scalar variable containing a filehandle name, typeglob, or typeglob reference, it is interpreted as a filename pattern to be globbed, and either a list of filenames or the next filename in the list is returned, depending on context. This distinction is determined on syntactic grounds alone. That means <$x> is always a readline() from an indirect handle, but <$hash{key}> is always a glob(). That's because $x is a simple scalar variable, but $hash{key} is not--it's a hash element. Even <$x > (note the extra space) is treated as glob("$x ") , not readline($x).

The document continues for a couple more paragraphs explaining in better detail what globbing is, and how it works with the diamond operator. And of course now we have the "how does the glob() function work?" question, which is answered by perldoc -f glob.

The original question of "how do I add text files to this list?" can be answered by doing two globs:

my @list = (<*.xml>, <*.txt>);

Or by pushing elements from list two onto the array, or by using traditional shell file expansion mechanisms, described in File::Glob:

my @list = <*.{xml,txt}>

Dave

Replies are listed 'Best First'.
Re^2: A bizarre way to get a list of filenames
by haukex (Archbishop) on Aug 05, 2019 at 15:49 UTC
    or by using traditional shell file expansion mechanisms

    I'm glad you didn't suggest <*.xml *.txt> :-) Splitting its arguments on whitespace by default is IMHO one of the caveats of glob.

      Truth be known, I tested the splitting on whitespace approach while composing the response and deemed it to work in this case, but to be too fragile to bother introducing as another option.


      Dave