code references - how to

[email protected] (Dermot)
Newsgroups perl.beginners
Message-ID <[email protected]>
Hi All,

I was have to create a script to search and find files. The files will
end in 'a.txt', 'b.txt', 'c.txt', so a record could have 123a.txt,
123b.txt, 123c.txt.

There may be lots of ways to achieve my goal but I got curious about
how to create a structure that, for each record would store the
associated files. Below is what I started out with. The hash tries 3
ways to either return a code reference or a true/false value if the
file exists. I have tried putting junk values in the hash values - sub
{ return -e "$File::Find::dir/${num}Z.txt"} - but it always ouputs
with a YES.

So how do you create a code reference in this context, should I create
a code reference or use something else and what should I be test the
hash values for?
Thanx in advance.
Dp.



#!/usr/bin/perl

use strict;
use warnings;
use File::Find;

my $root = shift;
my @records;

find(\&wanted, $root);

for (@records) {
 print $_->{name},"\t";
 foreach my $l (qw/a b c j/) {
        if ($_->{$l}) {
                print "YES\t";
        }
        else {
                print "No\t";
        }
 }
 print "\n";
}


sub wanted {

 my ($num,$let) = ($_ =~ /(\d+)([\w{1}])\.txt/);
 print $_, "  $num Let=$let\n" if ($let !~ /i|j|k/) ;

 my @sizes = qw(a b c d e f h i j k);

 my %file = (
        name    => "f001/$num",
        a       => sub { return -e "$File::Find::dir/${num}a.txt"},
        b       => sub { return -e "$File::Find::dir/${num}b.txt"},
        c       => sub { return does_exist("$File::Find::dir/${num}c.txt") },
        j       => \&does_exist("$File::Find::dir/${num}c.txt"),
        );
 push(@records, \%file);

}

sub does_exist {
 my $file = shift;
 return sub { return -e $file };
}
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.