Beefy Boxes and Bandwidth Generously Provided by pair Networks
Welcome to the Monastery
 
PerlMonks  

Re: Is this the most elegant way to code directory lookup?

by shmem (Chancellor)
on Sep 29, 2006 at 17:15 UTC ( #575576=note: print w/replies, xml ) Need Help??


in reply to Is this the most elegant way to code directory lookup?

- unless (-d $srcdir && -d $destdir ) {die "Error: $!";} + for($srcdir,$destdir) { ! -d $_ and die "Directory '$_' - $!\n" }

You wouldn't know which directory is missing.

- chdir $srcdir; - opendir(INDIR,$srcdir) || die "Can't open directory: $!"; + chdir $srcdir or die "Can't chdir to '$srcdir': $!\n"; + open my $indir, '.' or die "Can't read '.' in $srcdir: $!\n";

Point is, you are already in $srcdir, so you are here->.

Instead of using the ugly and hard to grok

unless (-d || $_ eq "." || $_ eq "..") { # . and .. are always -d

in the loop, after having lumped all directory entries (files, directories, symlinks, sockets, device files) into @files (are you really looking for files only? and making tar.gz's containing a single file each?) it would be more elegant

my @files = grep { -f } readdir $indir;

to stuff only the interesting files into @files.

`tar -cz $_ -f $_.tar.gz`; `mv *.gz $destdir`;

Really? what if your directory contains *.gz files? Your asking tar to complain. And use system, not backticks.

I would pack all files into one tar file:

chdir $srcdir or die "Can't chdir to '$srcdir': $!\n"; open my $indir, '.' or die "Can't read '.' in $srcdir: $!\n"; (my $tarfile = $srcdir) =~ s|.*/||; $tarfile = "$destdir/$tarfile-" . time . '.tar'; my @files = grep { -f and -M <= 1 } readdir $indir; # packing all files at once doesn't work with large directories # (limit of command line length) while(my $file = shift @files) { system ('tar', 'uf', $tarfile, $file) and die "Can't pack '$file' into '$tarfile' (exitcode $?)\n"; } system ('gzip', $tarfile) and die "Couldn't gzip $tarfile (exitcode $?)\n";

--shmem

_($_=" "x(1<<5)."?\n".q·/)Oo.  G°\        /
                              /\_¯/(q    /
----------------------------  \__(m.====·.(_("always off the crowd"))."·
");sub _{s./.($e="'Itrs `mnsgdq Gdbj O`qkdq")=~y/"-y/#-z/;$e.e && print}

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://575576]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others musing on the Monastery: (4)
As of 2021-06-13 09:33 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    What does the "s" stand for in "perls"? (Whence perls)












    Results (54 votes). Check out past polls.

    Notices?