For Programmers: Free Programming Magazines  


Home > Archive > PERL Miscellaneous > May 2005 > using -e with array of filenames









You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

 

Author using -e with array of filenames
Ninja67

2005-05-24, 8:56 pm

I've been using...
-e "myfile.txt"

to determine if a file exists and then I use...
unlink "myfile.txt"

to delete the file.

According to the documentation, I can use an array of filenames with
unlink so that I could delete multiple files at a time. Can I do
something similar with the -e?

In other words, can I check for the existance of multiple files without
writing a lengthy if statement such as the one below:

if (-e $done_dir."MMBTRVCF.txt" && -e $done_dir."MMBTRC2F.txt" && -e
$done_dir."MMBTRVLF.txt" && -e $done_dir."MMBTRL2F.txt" && -e
$done_dir."MMBTRVPF.txt" && -e $done_dir."MMBTRP2F.txt" && -e
$done_dir."CREATE_DATA.txt") {
....

Eric Schwartz

2005-05-24, 8:56 pm

"Ninja67" <Ninja67@gmail.com> writes:
> I've been using...
> -e "myfile.txt"
>
> to determine if a file exists and then I use...
> unlink "myfile.txt"
>
> to delete the file.
>
> According to the documentation, I can use an array of filenames with
> unlink so that I could delete multiple files at a time. Can I do
> something similar with the -e?


If you read the documentation ("perldoc -f -e" or "perldoc -f -X"),
you'd see:

This unary operator takes one argument, either a filename or a
file- handle, and tests the associated file to see if something
is true about it.

So no, you can't.

> In other words, can I check for the existance of multiple files without
> writing a lengthy if statement such as the one below:
>
> if (-e $done_dir."MMBTRVCF.txt" && -e $done_dir."MMBTRC2F.txt" && -e
> $done_dir."MMBTRVLF.txt" && -e $done_dir."MMBTRL2F.txt" && -e
> $done_dir."MMBTRVPF.txt" && -e $done_dir."MMBTRP2F.txt" && -e
> $done_dir."CREATE_DATA.txt") {
> ...


You can just build an array and delete it all at once:

my @filenames = qw/ .... /;
my @del_list;
for my $file (@filenames) {
push @del_list, $file if -e $file;
}
unlink @del_list;

Also read perlop about qw and friends for specifying a list in-place.
I generally find it easier to build up the list somewhere else, if
it's reasonably large.

-=Eric
--
Come to think of it, there are already a million monkeys on a million
typewriters, and Usenet is NOTHING like Shakespeare.
-- Blair Houghton.
xhoster@gmail.com

2005-05-24, 8:56 pm

"Ninja67" <Ninja67@gmail.com> wrote:

>
> According to the documentation, I can use an array of filenames with
> unlink so that I could delete multiple files at a time. Can I do
> something similar with the -e?


my @new_list = grep -e, @old_list;

Xho

--
-------------------- http://NewsReader.Com/ --------------------
Usenet Newsgroup Service $9.95/Month 30GB
A. Sinan Unur

2005-05-25, 3:58 am

"Ninja67" <Ninja67@gmail.com> wrote in news:1116969551.382114.325280
@g44g2000cwa.googlegroups.com:

> to determine if a file exists and then I use...
> unlink "myfile.txt"
>
> to delete the file.


There is no need to check for existence before attempting to delete.


--
A. Sinan Unur <1usa@llenroc.ude.invalid>
(reverse each component and remove .invalid for email address)

comp.lang.perl.misc guidelines on the WWW:
http://mail.augustmail.com/~tadmc/c...guidelines.html
Joe Smith

2005-05-25, 8:56 am

Ninja67 wrote:
> I've been using...
> -e "myfile.txt"
>
> to determine if a file exists and then I use...
> unlink "myfile.txt"
>
> to delete the file.
>
> According to the documentation, I can use an array of filenames with
> unlink so that I could delete multiple files at a time. Can I do
> something similar with the -e?


I would recommend against using unlink with an array, since
that interferes with outputting informative error messages.

foreach my $file (@files) {
if (-e $file) {
unlink $file or warn "Could not delete $file: $!\n";
} else {
print "File $file has already been deleted\n";
}
}

or

foreach my $file (@files) {
next unless -e $file; # Quietly skip deleted files
unlink $file or warn "Could not delete $file: $!\n";
}


-Joe
Anno Siegel

2005-05-25, 8:56 am

Eric Schwartz <emschwar@pobox.com> wrote in comp.lang.perl.misc:
> "Ninja67" <Ninja67@gmail.com> writes:

[...]
[color=darkred]
> You can just build an array and delete it all at once:
>
> my @filenames = qw/ .... /;
> my @del_list;
> for my $file (@filenames) {
> push @del_list, $file if -e $file;
> }
> unlink @del_list;


unlink grep -e, qw/ ... /;

But then, why even bother. "unlink" silently passes non-existent files:

unlink qw/ ... /;

Anno
Eric Schwartz

2005-05-25, 3:56 pm

anno4000@lublin.zrz.tu-berlin.de (Anno Siegel) writes:
> unlink grep -e, qw/ ... /;


For some reason, I keep forgetting about grep; I'm fine with map, I
use it all the time, but grep for some reason just doesn't always
stick with me. Part of it, I think, is the project I'm on has several
people who are very uncomfortable with "weird" (i.e., not like ksh)
aspects of Perl, so I try to keep the syntax minimal.

> But then, why even bother. "unlink" silently passes non-existent files:
>
> unlink qw/ ... /;


One advantage to grep is it's easier to debug; if you have the grep'd
filenames in an array, you can print them out to verify they're the
complete set, or more easily add other operations-- for instance,
logging the deletes in a database or logfile somewhere.

-=Eric
--
Come to think of it, there are already a million monkeys on a million
typewriters, and Usenet is NOTHING like Shakespeare.
-- Blair Houghton.
Anno Siegel

2005-05-25, 3:57 pm

Eric Schwartz <emschwar@pobox.com> wrote in comp.lang.perl.misc:
> anno4000@lublin.zrz.tu-berlin.de (Anno Siegel) writes:
>
> For some reason, I keep forgetting about grep; I'm fine with map, I
> use it all the time, but grep for some reason just doesn't always
> stick with me. Part of it, I think, is the project I'm on has several
> people who are very uncomfortable with "weird" (i.e., not like ksh)
> aspects of Perl, so I try to keep the syntax minimal.


I can see where you're standing, but it's a pity, really. The grep
and map functions, along with slices and the occasional splice are
the functions that treat arrays or lists, as the case may be, as units.
They shift the attention to the aggregate instead of to its elements.
In many situations, that is a good thing.

I bet you have to be stingy with refs too.

>
> One advantage to grep is it's easier to debug; if you have the grep'd
> filenames in an array, you can print them out to verify they're the
> complete set, or more easily add other operations-- for instance,
> logging the deletes in a database or logfile somewhere.


Oh, sure, including a quick check if it's done its duty (untested):

my @goners = grep -f ...;
@goners == unlink @goners or warn "some remain...";

....or even

@$_ == unlink @$_ or warn "some remain..." for [ grep -f ...];

Anno
Sponsored Links







Also available: Server administration forum archive | Web Design forum archive | Software forum archive | Hardware reviews archive

Copyright 2009 codecomments.com