in reply to Re^2: Using File::DosGlob::glob in loop only works first time
in thread Using File::DosGlob::glob in loop only works first time

I'm using glob in case the absolute path names contain a space character somewhere.

How does glob help you with path names that have spaces? glob accepts a file spec (i.e. something with *) and returns all matching files. It's used to allow the user to specify multiple files at once. However, since you're only interested in one file, it makes no sense to accept file specs or to use glob.

-f works with spaces, whether you've passed the value through glob or not.

The thing that's bothering me about this is "Why does glob give me a valid answer for the first item in my list, even when I'm using it in scalar context, and not the second?"

When glob is called in a scalar context, it still returns all the values it would in a list context. It just returns one by one. Somehow, it has to tell you when it's done returning all the values. It does that by returning undef.

The first time you call glob, it returns the first filename that matches the file spec.

The second time you call glob, it returns undef since there are are no files that match the file spec passed to the first call to glob.

Replies are listed 'Best First'.
Re^4: Using File::DosGlob::glob in loop only works first time
by ff (Hermit) on Feb 24, 2006 at 22:21 UTC
    As anticipated above by japhy, there is an evil windows reason at work. I was getting error messages like:

    File check, cannot access C:/Program\ Files/DirDictate/soap_form/soap_ +final.dot, No such file or directory
    I tried preprocessing my $template_file variable with:

    $template_file =~ s#/#\\\\#g;
    but that didn't help and so scanning around the ActiveState archives I found:

    perl-win32-users Re: using glob with filenames with spaces by Edward G. Orton, Jan 31 2002 7:08AM my @html_files = glob qq{"$input_dir/*.html"}; ego Edward G. Orton, GWN Consultants Inc. Phone: 613-764-3186, Fax: 613-764-1721
    Now in this case, the glob is being processed in list context, but I didn't think of "context" when I twisted this example for my needs. The file names that I wanted to test would be several absolute path names that wouldn't all have the same directory portion. So, faced with a series of files to check for filedom, I thought that having glob take its crack at creating/passing along the filename would get me its special powers of handling pathnames that include spaces in a Windows world.

    It appears that glob is acting like an iterator in which the original call to glob hangs around and gets fired with each visit to the glob line rather than new calls to glob being fired when the line repeats with the loop. I thought that perhaps wrapping an eval around my glob line would do what I wanted, but I'm rather clumsy with that command and the slice/[0] approach works quite nicely. :-)

      The error is the "\" in front of the space in "C:/Program\ Files/". It shouldn't be there.
        Well, yes, the '\' didn't help, but leaving it blank in the Windows world wasn't helping either; the string wouldn't hang together as a single argument and so I needed to find some kind of glue to submit the file name in toto to the -f test. Perhaps it's overkill, but the current solution of using globbing with a slice does what I want. :-)