smist has asked for the wisdom of the Perl Monks concerning the following question:

Hello,

I'm trying to utilize the File::Set module found at http://search.cpan.org/~robm/File-Set-1.01/Set.pm. I'm having difficulties getting any output.

use File::Set; $FS = File::Set->new(); $FS->add('/directory'); $FS->list("", \$callback); $callback = sub { print "Test\n" };
In the example code in the module it says to call the list subroutine like so:
list($Context, $Callback, $ErrorHandler)

I do not know what to use for $Context or $Callback. I'm not certain the code is even traversing the directory I supply in the add() subroutine. I tried running the program with debugging enabled and stepping through it and it doesn't seem to be doing much at all.

Does anyone have a working example I can see to get me rolling with this? Thanks for any suggestions and help. :)

Replies are listed 'Best First'.
Re: How to use File::Set module
by akho (Hermit) on May 01, 2007 at 18:24 UTC
    use warnings; use strict; use Fatal qw/ open close /; use File::Set; sub cback { my ($context, $prefix, $fpath, $ftype, $stat) = @_; if ($ftype eq 'f') { open my $fh, '<', $fpath; my $first_line = <$fh>; print $first_line; } } my $fs = File::Set->new(); $fs->add('/some/dir'); $fs->list('',\&cback);

    prints the first line of every plain file in /some/dir and all its subdirs.

    A callback is a reference to a subroutine; the important parameter passed to it is the third — full path to file.

      Thanks guys! I was able to get it working with your help.

Re: How to use File::Set module
by jbert (Priest) on May 01, 2007 at 18:16 UTC
    In your code, you don't seem to be using use strict and use warnings.

    If you had, you'd get a warning about an undefined variable $callback when you call $FS->list.

    You probably want to try setting my $callback = sub { print "Test\n" }; before the call to ->list, but strictures would probably have helped you find this yourself.