PERL BEGINNERS 18 RE HELP WITH UNLINK PLEASE
Date: Thu, 7 Aug 2003 23:00:07 -0400 (EDT)

Subject: Re: Help with Unlink please
From: perlwannabe@no-spam (Perlwannabe)

> Steve Grazzini wrote at Wed, 06 Aug 2003 23:38:00 -0400:
>
>> On Wed, Aug 06, 2003 at 11:49:20PM -0400, perlwannabe wrote:
>>> I have made the script as simple as possible and cannot get >>> unlink to work.
>>>
>>> unlink ("c:\testdir\*030977*.*") || die "unlink failed: $!";
>>
>> You'd need to expand the wildcards yourself:
>>
>> my $pat = 'c:\testdir\*030977*.*';
>> foreach (glob($pat)) {
>> unlink or warn "Couldn't unlink '$_': $!";
>> }
>
> Or if you want to write it as a one liner,
> you can exploit that unlink also takes a list as its arguments:
>
> unlink glob "c:/testdir/*030977*.*" or die "No files unlinked: $!";

Neither of these worked. I am beginning to think that there is something wrong with ActivePerl and WinXP.

Try this. Make a sample text file named "testfile.txt" and put it in directory "c:\testdir\"

then try this script...don't add anything like use File::glob and see if it works:

my $test = ("c:\\testdir\\testfile.txt");
unlink glob ($test) || die "unlink failed: $!";

This simple little script does not delete testfile.txt from my machine. Why?


Date: Thu, 7 Aug 2003 23:31:47 -0400

Subject: Re: Help with Unlink please
From: grazz@no-spam (Steve Grazzini)
On Thu, Aug 07, 2003 at 11:00:07PM -0400, perlwannabe wrote:
> > Steve Grazzini wrote at Wed, 06 Aug 2003 23:38:00 -0400:
> >> my $pat = 'c:\testdir\*030977*.*';
^

That looks like trouble. Using forward slashes, as Janek has done below, would have been smarter.

> >> foreach (glob($pat)) {
> >> unlink or warn "Couldn't unlink '$_': $!";
> >> }
> >
> > Or if you want to write it as a one liner,
> > you can exploit that unlink also takes a list as its arguments:
> >
> > unlink glob "c:/testdir/*030977*.*" or die "No files unlinked: $!";

That's not very good error-reporting. Maybe unlink failed ninety-nine times and succeeded once...
> Neither of these worked.

Did they give any error messages?

> my $test = ("c:\\testdir\\testfile.txt");
> unlink glob ($test) || die "unlink failed: $!";
^^

And that's the wrong "or". If you use the alphabetical "or", you'll at least get an error message.

unlink $test or die "unlink: $test: $!";

-- Steve