I agree with ww. This glob stuff can get you into trouble. There are a number of versions of glob(). There is even a DOS glob, one more variant to take into account when dealing with a MS platform. On one program I was able to coerce a module into using Posix glob, but couldn't make the same thing happen in main() which caused problems for file names with embedded spaces.

Using readdir and grep{} will work on any version of Perl and on any platform. Important to know is that readdir only returns the file names in the directory - not the full path name. So when you do the tests in grep{} to filter out non-directories, you have to use the full path on the -d test. Since you can use a regex inside the grep{}, you can get exactly what you want even if it is much more complex than the example below.

In general, I would advise against using cwd to change your working directory. Use full pathnames or relative pathnames from where your program is running. That way, you won't get confused about "where you are", ie where your program is running.And later in the program if you need to do something like delete some file (or perhaps make a sub-directory), you know exactly how to get a valid path name which will allow that. Plus see how much "cleaner" the code looks.

I use the following "formula"...

#!/usr/bin/perl use warnings; use strict; #list the Timepoint directories my $dir = "."; opendir (DIR, $dir) || die "couldn't open directory $dir\n"; my @TP_dirs = grep{-d "$dir/$_" && /^T/ }readdir DIR; foreach (@TP_dirs) { print "relative path of TP: $dir/$_\n"; } __END__ in my dir, prints: relative path of TP: ./TESTING

In reply to Re: chdir problem by Marshall
in thread chdir problem by PerlBeginNew

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.