On Wed, 24 Jul 2002, McCormick, Rob E wrote:
>
> I read (ok, skimmed...) the results from perldoc -m cwd. I don't
> understand cwd (current working directory). My questions apply to this
> general scenario:
>
> In practice, I'd like to work with my .pl or .plx files in say, /myhome/bin/
>
> Suppose I want to do some looping on some files in /data/path/files, or even
> on NT
You can do this by using the glob operator (perldoc -f glob), opendir,
readdir, closedir (perldoc -f opendir, perldoc -f readdir, perldoc -f
closedir). If you want to traverse /data/path/files recursively you can
use File::Find (perldoc File::Find)
$dir is the directory that you want to loop through. Run both the glob and
opendir, readdir, closedir examples, you will notice the different form of
output.
# glob
while (<$dir/*>) {
print "$_\n";
}
# opendir, readdir, closedir
opendir(DIRHNDL, $dir) or
die "Cannot open $dir : $!\n";
while (my $file = readdir(DIRHNDL)) {
print "$file\n";
}
closedir(DIRHNDL);
# File::Find
find (sub { print "$File::Find::name\n" }, $dir);
>
> \\hostname\d$\inetpub\wwwroot\virtualdir
>
> Would use Cwd; even be necessary?
For just looping through the files in a directory no, but depends
on what you are trying to do.
> What are some scenarios you've used Cwd.pm in the past?
> Why?
When you are changing directories (perldoc -f chdir) in your program a
lot, Cwd.pm can be used to get your current working directory. There
was a thread in this list about pushd and popd that had an example of
Cwd's use.
>
> If all files to be looped over are in, say, /data/path/files is it used to
> declare in pseudocode:
>
> <<define working directory as /data/path/files >>
>
> The docs note: It is recommended that cwd (or another *cwd() function) is
> used in all code to ensure portability.
>
> Rob
> --
> Rob McCormick
>
>
>
>
>
>
--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]