If the string(sic --> should be 'thing') inside the angle brackets is anything other than a filehandle name or a scalar variable (even if there are just extra spaces), it is interpreted as a filename pattern to be 'globbed'. (Programmig Perl, p. 83)

You have a scalar variable inside the angle brackets, so it is *not* interpreted as a filename pattern to be globbed.

You could trick perl like this:

my @fnames = <${path}>

But since the angle operator calls glob() implicitly when you don't provide it with a file handle or scalar variable inside the brackets, you might as well just call glob() explicitly yourself. Note that the argument to glob() is a string, so you need quotes.

You can also use opendir() and readdir():

use strict; use warnings; use 5.010; my $path = '/var/log'; opendir(my $TARGET_DIR, $path) or die "Couldn't open directory $path: $!"; my @all_files = readdir $TARGET_DIR; closedir $TARGET_DIR;

There are a couple of differences, though: 1) glob() will ignore invisible files, 2) glob() tacks the file name onto the specified path, where readdir() just returns the filenames.


In reply to Re: Reading a directory into an array using <> by 7stud
in thread Reading a directory into an array using <> by Bignut_Squirrel

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.