PLEASE HELP! Perl script does not work!

G

Gandu

Could some Perl guru please help me? I have a very simple script to
remove
contents of the mozilla cache. It runs, but does nothing, the cache
remains
full as before. Could someone please point what I am doing wrong? The
file
is below:

#!/usr/bin/perl

$directory_name = '.mozilla/gandu/uar30qwk.slt/Cache';

print "Opening directory ...: $directory_name\n";

opendir(the_dir, $directory_name) || die("Directory could not be
opened");

@filelist = readdir(the_dir);

foreach $f (@filelist){
unless(($f eq ".") || ($f eq "..")){
printf("Removing ... $f\n");
unlink($f);
}
}

closedir(the_dir);

Thanks in advance for your help.
 
A

A. Sinan Unur

(e-mail address removed) (Gandu) wrote in
Could some Perl guru please help me? I have a very simple script to
remove contents of the mozilla cache.

Please put the subject of your message in the subject line of your message.
Also please read the posting guidelines for this group

http://mail.augustmail.com/~tadmc/clpmisc/clpmisc_guidelines.text
It runs, but does nothing, the cache remains full as before. Could
someone please point what I am doing wrong?

You did not read the documentation for the functions you are using.

The file is below:
#!/usr/bin/perl

use strict;
use warnings;
$directory_name = '.mozilla/gandu/uar30qwk.slt/Cache';

$directory_name = '.mozilla/gandu/uar30qwk.slt/Cache';
print "Opening directory ...: $directory_name\n";

opendir(the_dir, $directory_name) || die("Directory could not be
opened");

Why ignore "why" opendir failed?

The convention is either to use all upper case variables for directory
handles.

opendir THE_DIR, $directory_name
or die "Directory could not be opened: $!";
@filelist = readdir(the_dir);

my @filelist = readdir(THE_DIR);

If the number of files is large this very inefficient. It is generally
better to process files one by one by calling readdir in scalar context.

Please read:

perldoc -f opendir
perldoc -f readdir
foreach $f (@filelist){
unless(($f eq ".") || ($f eq "..")){
printf("Removing ... $f\n");
unlink($f);

You are not checking if unlink succeeded. This code also does not check if
the file is actually a directory. Please read:

perldoc -f unlink
perldoc -f -x
}
}

closedir(the_dir);

closedir THE_DIR;

Sinan
 
T

Tad McClellan

Gandu said:
Subject: PLEASE HELP! Perl script does not work!


PLEASE HELP! Put the subject of your article in the Subject of your article!

Have you see the Posting Guidelines that are posted here frequently?

Could some Perl guru please help me?


You do not need the help of a Perl guru.

You don't even need the help of a Perl programmer of any level.

You only need the help of any programmer, as any of them ought
to know that

You should read the documentation for the functions that you use.

Are you a programmer?

It runs, but does nothing,


If you want to verify that the unlink() unlinked, then you
should check its return value:

unlink($f) or warn "could not unlink '$f' $!";

Could someone please point what I am doing wrong?


You are doing the programming equivalent of signing a contract
without reading it, namely calling a function without have read
the documentation for that function.

#!/usr/bin/perl


use warnings;
use strict;

Ask for all the help you can get!

$directory_name = '.mozilla/gandu/uar30qwk.slt/Cache';
opendir(the_dir, $directory_name) || die("Directory could not be
opened");

@filelist = readdir(the_dir);

foreach $f (@filelist){
unless(($f eq ".") || ($f eq "..")){
printf("Removing ... $f\n");
unlink($f);


perldoc -f readdir

If you're planning to filetest the return values out of a
"readdir", you'd better prepend the directory in question.
Otherwise, because we didn't "chdir" there, it would have been
testing the wrong file.


You are trying to remove the files from the *current directory*
rather than the directory where they really are:

unlink( "$directory_name/$f" ) or warn...
 
A

A. Sinan Unur

Andrew Tkachenko said:
Gandu wrote on 20 ÐоÑбрь 2004 17:24:


@filelist contains relative paths. To fix it, prepend $f with
$directory_name:

That is one problem.
foreach $f (map {"$directory_name/$_"} grep {!/^\./} @filelist){

First: You are also reading in all the files first. Depending on your cache
settings, the directory may contain thousands of files. Do you really want
to create an array that size?

Second: Do you want to skip hidden files on *nix systems?

perldoc -f -X
printf("Removing ... $f\n");
unlink($f);
}

I am not sure if there is a rule that says the Firefox cache directory
cannot contain subdirecotries. See perldoc -f unlink and note that the
potential problem can be avoided by using appropriate file test operator.

By the way, I am a little baffled as to why a simple rm from the command
line is not an acceptable alternative to writing this script.

The script below skips subdirectories. I only tested it on my Windows 98
system:

#! /usr/bin/perl

use strict;
use warnings;

use constant BASE_DIR => 'd:/home';

use File::Spec::Functions qw(rel2abs catfile);

my $dir = rel2abs(shift @ARGV, BASE_DIR);

chdir $dir or die "Cannot change to $dir: $!";

opendir my $DIR, '.' or die "Cannot open $dir: $!";

while(my $f = readdir $DIR) {
next unless -f $f;
print "Removing @{[ catfile($dir, $f) ]}\n";
unless( unlink $f ) {
warn "Cannot remove $f: $!";
}
}
__END__
 
A

Andrew Tkachenko

Gandu wrote on 20 ÐоÑбрь 2004 17:24:
foreach $f (@filelist){
unless(($f eq ".") || ($f eq "..")){
printf("Removing ... $f\n");
unlink($f);
}
}

@filelist contains relative paths. To fix it, prepend $f with
$directory_name:

foreach $f (map {"$directory_name/$_"} grep {!/^\./} @filelist){
printf("Removing ... $f\n");
unlink($f);
}
 
A

A. Sinan Unur

What is that correction showing?

I expect you forgot to put the my() in there...


my $directory_name = '.mozilla/gandu/uar30qwk.slt/Cache';

Thanks for catching that.

Sinan
 
G

Gandu

Dear Sir,
You are indeed the Perl guru. It has been a pleasure
to hear these words of profound knowledge.
 
T

Tore Aursand

#!/usr/bin/perl

$directory_name = '.mozilla/gandu/uar30qwk.slt/Cache';

print "Opening directory ...: $directory_name\n";

opendir(the_dir, $directory_name) || die("Directory could not be
opened");

@filelist = readdir(the_dir);

foreach $f (@filelist){
unless(($f eq ".") || ($f eq "..")){
printf("Removing ... $f\n");
unlink($f);
}
}

closedir(the_dir);

Use the 'File::Find::Rule' module from CPAN, and check the return value of
the 'unlink' function;

#!/usr/bin/perl
#
use strict;
use warnings;
use File::Find::Rule;

my $dir = '.mozilla/gandu/uar30qwk.slt/Cache';
my @files = File::Find::Rule->file()->in( $dir );

foreach ( @files ) {
unlink or warn "Couldn't delete '$_'; $!\n";
}
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads


Members online

No members online now.

Forum statistics

Threads
473,995
Messages
2,570,236
Members
46,825
Latest member
VernonQuy6

Latest Threads

Top