SPUG: Problem deleting files

Scott Blachowicz scott at sabmail.rresearch.com
Fri Dec 1 10:34:00 CST 2000


On Thu, Nov 30, 2000 at 09:48:56PM -0800, Matt Tucker wrote:
> -- rick.croote at philips.com spake thusly:
> 
> > To backup what Tim said, more so, don't use unlink <$oldfile>, use
> > unlink $oldfile or unlink ($oldfile).
> 
> This wouldn't work, because he's trying to delete files using globbing. 
> The following would work:
> 
>     my $oldfiles = 'foo.*';
>     unlink < $oldfiles >;

Yes, unlink takes a LIST of files and the <$oldfiles> should return a
list of filenames matching the glob pattern contained in the $oldfiles
var.

> ...
> As for why the code isn't working, does it have something to do with mixing
> up shell variables and perl variables?
> ...

You might try sticking some debugging prints in there...

	my $oldfiles = 'foo.*';
	my @filenames = <$oldfiles>;
	print map {">>$_<<\n"} @filenames;
	unlink @filenames;

Hmmmm...that's odd...I guess the globbing thing is kinda special...

	gator:/tmp% ls -l foo.*
	-rw-rw-r--   1 scott    scott           0 Dec  1 08:26 foo.a
	-rw-rw-r--   1 scott    scott           0 Dec  1 08:26 foo.b
	-rw-rw-r--   1 scott    scott           0 Dec  1 08:26 foo.c
	gator:/tmp% cat foodel.pl
	#! /usr/bin/perl
	my $oldfiles = 'foo.*';
	my @filenames = <$oldfiles>;
	print map {">>$_<<\n"} @filenames;
	unlink @filenames;
	gator:/tmp% perl foodel.pl

No output...but if I change the script a little...

	gator:/tmp% cat foodel.pl
	#! /usr/bin/perl
	my $oldfiles = 'foo.*';
	my @filenames = eval "<$oldfiles>";
	print map {">>$_<<\n"} @filenames;
	unlink @filenames;
	gator:/tmp% perl foodel.pl
	>>foo.a<<
	>>foo.b<<
	>>foo.c<<
	gator:/tmp% ls -l foo.*
	zsh: no matches found: foo.*

It must not to variable expansion inside the globbing brackets, so you
have to the expansion yourself by using "eval".

-- 
Scott Blachowicz

 - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
     POST TO: spug-list at pm.org       PROBLEMS: owner-spug-list at pm.org
      Subscriptions; Email to majordomo at pm.org:  ACTION  LIST  EMAIL
  Replace ACTION by subscribe or unsubscribe, EMAIL by your Email-address
 For daily traffic, use spug-list for LIST ;  for weekly, spug-list-digest
  Seattle Perl Users Group (SPUG) Home Page: http://www.halcyon.com/spug/





More information about the spug-list mailing list