Stories
Slash Boxes
Comments
NOTE: use Perl; is on undef hiatus. You can read content, but you can't post it. More info will be forthcoming forthcomingly.

All the Perl that's Practical to Extract and Report

The Fine Print: The following comments are owned by whoever posted them. We are not responsible for them in any way.
 Full
 Abbreviated
 Hidden
More | Login | Reply
Loading... please wait.
  • Looks Nice (Score:3, Insightful)

    Hmm ... seems like an awful lot of duplicated code for checking the file status. Five subs are identical, so I deleted them.

    package File::Finder::Steps;

    use constant OFFSET => qr/^(\+|-|)(.*)/;

    my %status = (
        links => '(stat(_))[3]',
        inum  => '(stat(_))[1]',
        atime => 'int(-A _)',
        mtime => 'int(-M _)',
        ctime => 'int(-C _)',
    );

    while (my ($sub,$op) = each %status) {
        eval <<"    END_SUB";

    • Heh. Earlier today, I read Schwern's paper on accessors and closures [guild.net]. Is it me, or is there more than some similarity in the problem statement?

      # And then eval() a new subroutine for each of them.
      foreach my $accessor (@Accessors) {
          eval qq{
              sub $accessor {
                  my(\$self) = shift;
                  if( \@_ ) {
                      my \$data = shift;
           

      • Yeah, this is a technique I use a lot, though really I should have done this:

        my %status = (
          links => sub { (stat(_))[3] },
          inum  => sub { (stat(_))[1] },
          atime => sub { int(-A _) },
          mtime => sub { int(-M _) },
          ctime => sub { int(-C _) },
        );

        while (my ($function,$op) = each %status) {
          no strict 'refs';
          *$function = sub {
            my $self = shift;
            my ($prefix, $n) = shift =~ OFFSET;

            return sub {
              _n($prefix, $n, $op->());
            };
          }
        }

        It has the advantage that it fits Randal's formatting and it matches Schwern's fix of using closures. Plus, I think it's a bit cleaner.