SPUG: Problem deleting files

Daniel Chetlin daniel at chetlin.com
Fri Dec 1 12:28:23 CST 2000


On Fri, Dec 01, 2000 at 05:09:22PM +0000, Tim Maher/CONSULTIX wrote:
> As I told Rick in a private communication, that's not at all what I
> said; my point was <$oldfile> looks like a filehandle reference, so use
> < $oldfile > instead.   But as it turns out, that wasn't the problem
> here anyway (see below).  

Why `< $oldfile >'? That causes strange other (unexpected) effects.

> > 	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.*
> 
> Like the shells, Perl *will* do variable expansion within the "globbing
> brackets", but any wildcards have to be visible when the innards of
> those brackets are first scanned (as opposed to arising out of variable
> interpolation).  Because an eval allows the interpolation to occur before
> the globbing operation is attempted, it allows the wildcard characters
> to be presented via variables.

[~] $ perl -we'$h{f}="foo.*";my at f=<$h{f}>;print map{">>$_<<\n"}@f'
>>foo.a<<
>>foo.b<<
>>foo.c<<

[~] $ perl -we'$f="foo.*";my at f=<${f}>;print map{">>$_<<\n"}@f'
>>foo.a<<
>>foo.b<<
>>foo.c<<

Wildcards certainly can arise out of variable interpolation.

> So this is the approach that is needed:
> 
> #! /usr/bin/perl -w
> my $oldfiles = 'foo.';
> my @filenames = eval "<$oldfiles*>";
> print map {">>$_<<\n"} @filenames;
> unlink @filenames;

Why do you think this version is different than Scott's, and why do you
think it's necessary to move the `*' from the variable to the glob?

The only gotcha in this thread is that `<>' is interpreted as readline()
instead of glob() when it sees a lone scalar. The most reasonable
approach to that is to use the glob() function directly. All of this
talk about interpolation is a red herring.

-dlc

 - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
     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