SPUG: Problem deleting files
Tim Maher/CONSULTIX
tim at consultix-inc.com
Fri Dec 1 11:09:22 CST 2000
On Fri, Dec 01, 2000 at 08:34:00AM -0800, Scott Blachowicz wrote:
> 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).
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).
> 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 do variable expansion inside the globbing brackets, so you
> have to the expansion yourself by using "eval".
>
> --
> Scott Blachowicz
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.
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;
And it works! 8-}
Sorry I didn't pick up on this before as I should have; we actually have
a lab exercise that illustrates this very point in my basic Perl Programming
class, so I'm familiar with this situation, but I was concentrating on
other aspects of the sample code earlier.
>
> - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
> 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/
>
>
--
*========================================================================*
| Dr. Tim Maher, CEO, Consultix (206) 781-UNIX/8649; ask for FAX# |
| Email: tim at consultix-inc.com Web: http://www.consultix-inc.com |
|Training- TIM MAHER: Unix, Perl DAMIAN CONWAY: Adv. Perl, OOP, Parsing |
|12/12: UNIX 1/15: Perl/DBI 2/20: Data Munging;Perl 2/22: Adv. OO Perl|
*========================================================================*
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
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