Re: A bizarre way to get a list of filenames
by holli (Abbot) on Aug 05, 2019 at 14:56 UTC
|
| [reply] [d/l] |
Re: A bizarre way to get a list of filenames
by LanX (Saint) on Aug 05, 2019 at 14:55 UTC
|
It's called file-glob and is using the same diamond operator like readline
And yes I agree it's confusing °, and due to the scripting ("Bash on steroids") heritage of Perl4.
°) even the parser is sometimes confused, because it decides based on the inner pattern which operator is meant. | [reply] |
|
even the parser is sometimes confused
Nitpick: Unlike other cases, such as map or //, I think the rules for when <...> is readline vs glob are pretty clearly spelled out in I/O Operators.
kiz: Note that using glob has several caveats, described in To glob or not to glob. In this particular example that you showed in your post, it's probably fine.
| [reply] [d/l] [select] |
|
> pretty clearly spelled out in I/O Operators
here I agree with the docs
These days, it's considered cleaner to call the internal function directly as glob($foo), which is probably the right way to have done it in the first place .)
I passively remember very hairy edge cases here, and sometimes people try to overload <> for their own iterator use and fail because of edge cases. It's our main iterator operator after all.
| [reply] [d/l] |
|
|
|
Re: A bizarre way to get a list of filenames
by davido (Cardinal) on Aug 05, 2019 at 15:43 UTC
|
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}>
| [reply] [d/l] [select] |
|
| [reply] [d/l] [select] |
|
| [reply] |
Re:A bizarre way to get a list of filenames
by hippo (Bishop) on Aug 05, 2019 at 15:03 UTC
|
perldoc -f glob gives examples of using the angle brackets in this manner. It also refers to I/O-Operators in perlop which has some more on this towards the end of that section. That should cover most of it but if there's something you're still not sure about please ask further.
| [reply] |
Re: A bizarre way to get a list of filenames
by Your Mother (Archbishop) on Aug 05, 2019 at 15:11 UTC
|
Sidenote based on this–
("how do I add text files to this list?"... answer: make 2 lists & join them.)
–join is a proper operator as well as a concept and it’s definitely not what you want here. push @files, @xml_files, @text_files; or my @files = ( @xml_files, @text_files ); or some variety along those lines is more what you’ll want. You might already know this, I only mention it because of “join them.”
| [reply] [d/l] [select] |
Re: A bizarre way to get a list of filenames
by Lotus1 (Vicar) on Aug 05, 2019 at 15:08 UTC
|
(His question was "how do I add text files to this list?"... answer: make 2 lists & join them.. )
There is also the push function if you would like to push items or a list onto the end of the array. Unshift will add to the beginning.
| [reply] |
Re: A new to me way to get a list of filenames
by Anonymous Monk on Aug 06, 2019 at 02:11 UTC
|
$ perl -MO=Deparse -e " my @xml_files = <*.xml> "
use File::Glob ();
my(@xml_files) = <*.xml>;
-e syntax OK
File::Glob... | [reply] [d/l] |