Re: code references - how to

[email protected] ("Jenda Krynicky")
Newsgroups perl.beginners
Message-ID <[email protected]>
From:           	Dermot <[email protected]>
> 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}) {

You never called the referenced subroutine. To do that you have to 
use

    if ($_->{$l}->()) {
or
    if (&{$_->{$l}}) {

The $_->{$l} is the reference and references are always true.

Jenda
===== [email protected] === http://Jenda.Krynicky.cz =====
When it comes to wine, women and song, wizards are allowed 
to get drunk and croon as much as they like.
	-- Terry Pratchett in Sourcery
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.