Beefy Boxes and Bandwidth Generously Provided by pair Networks
Do you know where your variables are?
 
PerlMonks  

comment on

( [id://3333]=superdoc: print w/replies, xml ) Need Help??
This glob thing can be a problem. A long time ago I got tripped up with the 3 versions of glob that were in use at that time in the ActiveState version of Perl that I was using. I changed my code to use readdir() and that solved the problem.

Nowadays, Perl glob is a lot more uniform and well behaved. This prints all simple files, but skips directories.

my @files = glob ('*.*'); print "",join("\n",@files),"\n"
For what you want, I would consider File::Find.
Consider this code also.
use strict; use warnings; opendir (my $dir, ".") or die "unable to open current directory $_"; my @files; my @directories; foreach my $file (grep{ ($_ ne '.') and ($_ ne '..')} readdir $dir) { if (-f $file ) {push @files, $file;} elsif (-d $file) {push @directories, $file;} else { die "weird beast found! $file"} } print "@files\n"; print "@directories\n";
I think in Unix there can be special things that are not simple files or directories. I would use a file test to see what this name actually means.
Note that if this is not the current directory, you need to spec the full path name for file tests.

Update: File operations like "open file" or "open directory" are "expensive" in terms of performance. I would expect my code to run faster than the OP's code, but I did not benchmark this in any serious way. If the directories are small and this is not done that often, I don't think that will make any difference at all. Also be aware that there is a special variable for repeated file tests, "_". like  elsif (-d _) {do something{ That tests the structure returned by the previous file test operation for a different flag.

Overall, unless there is a performance or other problem (special kinds of files), I see no problem with the OP's code.


In reply to Re: glob() and dot files by Marshall
in thread glob() and dot files by perlancar

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



  • Are you posting in the right place? Check out Where do I post X? to know for sure.
  • Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
    <code> <a> <b> <big> <blockquote> <br /> <dd> <dl> <dt> <em> <font> <h1> <h2> <h3> <h4> <h5> <h6> <hr /> <i> <li> <nbsp> <ol> <p> <small> <strike> <strong> <sub> <sup> <table> <td> <th> <tr> <tt> <u> <ul>
  • Snippets of code should be wrapped in <code> tags not <pre> tags. In fact, <pre> tags should generally be avoided. If they must be used, extreme care should be taken to ensure that their contents do not have long lines (<70 chars), in order to prevent horizontal scrolling (and possible janitor intervention).
  • Want more info? How to link or How to display code and escape characters are good places to start.
Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others taking refuge in the Monastery: (12)
As of 2024-04-23 08:42 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found