in reply to Re: Listing Files
in thread Listing Files

If you're willing to scan the filenames before scanning the contents, there's a much cleaner-looking solution:
use File::Find; @ARGV = (); find sub { push @ARGV, $File::Find::name if /\.html\z/; }, qw(/home /v +ar/dir); while (<>) { next unless /\bExpired\b/; print "$ARGV\n"; # current filename close ARGV; # skip to next file now }

-- Randal L. Schwartz, Perl hacker

Replies are listed 'Best First'.
Re: &bull;Re: Re: Listing Files
by Anonymous Monk on Mar 01, 2002 at 16:15 UTC
    Thanks merlyn!
Re: Â&#8226;Re: Re: Listing Files
by lordsuess (Scribe) on Mar 02, 2002 at 01:43 UTC
    Using @ARGV as a global List is a rather interesting idea from the perl-point-of-view, but in my eyes, strat's solution is cleaner. If you really want to list the files first and then search them for Expired, I'd do it with a closure, e.g.
    use File::Find; my @htmlFiles = (); find sub { push (@htmlFiles, $File::Find::name) if /\.html\z/; }, qw(/home /var/dir); foreach (@htmlFiles){ ...
    @merlyn: btw: thanks for the wonderful Schwartzian Transform and so many good other tricks you showed me :-)
      But setting @ARGV is an acceptable way to prepare for a diamond walk. With your solution, you have to either copy @htmlFiles to @ARGV, or do your own open loop, thereby moving further and further from idiomatic Perl.

      Feel free to do what you want in the privacy of your own cubicle, but I'd definitely ding you with a yellow flag in a formal code review for not using @ARGV in some manner in that loop.

      -- Randal L. Schwartz, Perl hacker

        Dear merlyn,

        with your piece of code, I think there is the danger that people might use it without fully understanding what it is doing (see for example the cgi parameter "parser" by Matt's Script Archive, which may work in a certain script, but not in many others). And they will perhaps get into troubles when there is e.g. already something in @ARGV.

        From the perl-point-of-view it's great, but from the learner's-point-of-view it may cause more problems than necessary.

        I hope, this time I happened to explain my point of view in a better way.

        Best regards,

        ls

          A reply falls below the community's threshold of quality. You may see it by logging in.