邪门的File::Find, 请教

邪门的File::Find, 请教

新学perl请教各位:

下面这段代码使用File::Find模块找到*.ape文件,然后将其删除。但是奇怪的是如果是

#./delape.pl ./ 则能正常工作。

但是,如果跨目录,比如,
#./delape.pl ./temp/ 则能找到*.ape文件,但是无法删除。这是怎么回事?


#!/usr/bin/perl -w
#name: delape.pl

use strict;
use File::Find;

sub wanted {
     $_ = $File::Find::name;
     if ( /\.ape$/ ) {
        my $file =  $File::Find::name;
        print "Found ape file: $file\n";
        my $cnt = unlink $file;
        print "\$cnt is $cnt\n";
     }
}
my $dir = shift;
$dir ||=".";
find (\&wanted, $dir);
加了绝对路径后就能删除了
高手,还真是,但是好像unlink并不需要绝对路径呀,为什么?
要的,最终的操作都需要生成绝对路径来的。
这是在newsgroup中别人回答我的,希望对大家有启发。

Alitaia           
View profile
         More options Dec 3, 11:10 am
Newsgroups: comp.lang.perl.misc
From: Alitaia <walter.newsgr...@gmail.com>
Date: Sun, 2 Dec 2007 19:10:56 -0800 (PST)
Local: Mon, Dec 3 2007 11:10 am
Subject: File::Find make me mad
Reply | Reply to author | Forward | Print | Individual message | Show original | Remove | Report this message | Find messages by this author
I write a simple perl, del.pl, to use File::Find to find *.ape and
delete them. I can find  the *.ape but can not del outside the
directory, can del them within the directory, I dont know what's
wrong.

for example:
If directory structure like this:
---./layer1
         |
         /layer2
             |
             a.ape
             b.ape

with the following code, within layer2
#del.pl ./       a.ape and b.ape can be deleted

but if under ./layer1,
#del.pl ./layer2   a.ape, b.ape can be found but can not del

what is wrong?

#!/usr/bin/perl -w
#name: del.pl
use strict;
use File::Find;

sub wanted {
     $_ = $File::Find::name;
     if ( /\.ape$/ ) {
        my $file =  $File::Find::name;
        print "Found ape file: $file\n";
        #system "shntool", "conv", "-o", "flac", $File::Find::name;
        my $cnt = unlink $file;
        print "\$cnt is $cnt\n";
     }
}

my $dir = shift;
$dir ||=".";
find (\&wanted, $dir);

    Reply    Reply to author    Forward  
               
               
               
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
        
               
John W. Krahn           
View profile
         More options Dec 3, 3:00 pm
Newsgroups: comp.lang.perl.misc
From: "John W. Krahn" <kra...@telus.net>
Date: Mon, 03 Dec 2007 07:00:49 GMT
Local: Mon, Dec 3 2007 3:00 pm
Subject: Re: File::Find make me mad
Reply | Reply to author | Forward | Print | Individual message | Show original | Report this message | Find messages by this author

- Hide quoted text -
- Show quoted text -
Alitaia wrote:

> I write a simple perl, del.pl, to use File::Find to find *.ape and
> delete them. I can find  the *.ape but can not del outside the
> directory, can del them within the directory, I dont know what's
> wrong.

> for example:
> If directory structure like this:
> ---./layer1
>          |
>          /layer2
>              |
>              a.ape
>              b.ape

> with the following code, within layer2
> #del.pl ./       a.ape and b.ape can be deleted

> but if under ./layer1,
> #del.pl ./layer2   a.ape, b.ape can be found but can not del

> what is wrong?

> #!/usr/bin/perl -w
> #name: del.pl
> use strict;
> use File::Find;

perldoc File::Find
[ SNIP ]
   You are chdir()'d to $File::Find::dir when the function is called,
unless no_chdir was specified.

> sub wanted {

If './layer1' is passed from the command line and the current file is
'./layer1/layer2/a.ape' then $File::Find::dir contains './layer1/layer2'
and $_ contains 'a.ape' and $File::Find::name contains
'./layer1/layer2/a.ape'.

>      $_ = $File::Find::name;

Now both $_ and $File::Find::name contain './layer1/layer2/a.ape'.  Why
are you doing this?

>      if ( /\.ape$/ ) {
>         my $file =  $File::Find::name;
>         print "Found ape file: $file\n";
>         #system "shntool", "conv", "-o", "flac", $File::Find::name;
>         my $cnt = unlink $file;

The current directory is './layer1/layer2' so unlink is looking for the
file in './layer1/layer2/layer1/layer2/a.ape' instead of the current
directory.

>         print "\$cnt is $cnt\n";
>      }
> }
> my $dir = shift;
> $dir ||=".";
> find (\&wanted, $dir);

John
--
use Perl;
program
fulfillment

    Reply    Reply to author    Forward       Rate this post: Text for clearing space
Cancel
               
               
Send  Discard
               
The group you are posting to is a Usenet group. Messages posted to this group will make your email address visible to anyone on the Internet.
               
From:         
Alitaia <walter.newsgroup@gmail.com>
Newsgroups:         
Cc:         
Followup To:         
        
Add Cc | Add Followup-to | Edit Subject         
Subject:         
               
Send  Discard
        
               
               
Your post was successful
               
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
        
               
Todd           
View profile
         More options Dec 3, 5:36 pm
Newsgroups: comp.lang.perl.misc
From: Todd <[url=mailto%3Cimg%20src=]ueweizh...@gmail.com">xueweizh...@gmail.com[/url]>
Date: Mon, 3 Dec 2007 01:36:24 -0800 (PST)
Local: Mon, Dec 3 2007 5:36 pm
Subject: Re: File::Find make me mad
Reply | Reply to author | Forward | Print | Individual message | Show original | Report this message | Find messages by this author
Hi,

I got the simplest form for this questions

perl -MFile::Find -e '
find sub { unlink if /\.ape$/} , shift || "."
'

-Todd

    Reply    Reply to author    Forward       Rate this post: Text for clearing space
               
               
               
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
        
               
Randal L. Schwartz           
View profile
         Hide options Dec 4, 12:15 am
Newsgroups: comp.lang.perl.misc
From: mer...@stonehenge.com (Randal L. Schwartz)
Date: Mon, 03 Dec 2007 08:15:12 -0800
Local: Tues, Dec 4 2007 12:15 am
Subject: Re: File::Find make me mad
Reply | Reply to author | Forward | Print | Individual message | Show original | Report this message | Find messages by this author

>>>>> "Alitaia" == Alitaia  <walter.newsgr...@gmail.com> writes:

Alitaia> I write a simple perl, del.pl, to use File::Find to find *.ape and
Alitaia> delete them. I can find  the *.ape but can not del outside the
Alitaia> directory, can del them within the directory, I dont know what's
Alitaia> wrong.

In addition to the other answers, consider this, using my File::Finder
from the CPAN:

use File::Finder;
File::Finder->name('*.ape')->eval(sub { unlink })->in('.');

There.  Done.

--
Randal L. Schwartz - Stonehenge Consulting Services, Inc. - +1 503 777 0095
<mer...@stonehenge.com> <URL:http://www.stonehenge.com/merlyn/>;
Perl/Unix/security consulting, Technical writing, Comedy, etc. etc.
See PerlTraining.Stonehenge.com for onsite and open-enrollment Perl training!

    Reply    Reply to author    Forward       Rate this post:
               
               
               
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
        
               
Dr.Ruud           
View profile
         More options Dec 4, 7:55 am
Newsgroups: comp.lang.perl.misc
From: "Dr.Ruud" <rvtol+n...@isolution.nl>
Date: Tue, 4 Dec 2007 00:55:02 +0100
Local: Tues, Dec 4 2007 7:55 am
Subject: Re: File::Find make me mad
Reply | Reply to author | Forward | Print | Individual message | Show original | Report this message | Find messages by this author
Todd schreef:

> I got the simplest form for this questions

> perl -MFile::Find -e '
>  find sub { unlink if /\.ape$/} , shift || "."
>  '

perl -wle'
  $p = shift || ".";
  print for <$p/*.ape>
' optional/path

(replace "print" by "unlink", or even "print and unlink"

--
Affijn, Ruud

"Gewoon is een tijger.

请教

[Copy to clipboard] [ - ]
CODE:
$p = shift || ".";

这个是什么意思?
我知道最终的结果是将在程序名后的输入取出,但是这里shift || ".";具体是什么意思呢?
明白了,shift取出参数,如果参数为空则返回.,即当前目录
这个module能支持查找多少天前修改的文件么?