Beefy Boxes and Bandwidth Generously Provided by pair Networks
There's more than one way to do things
 
PerlMonks  

output of Find::File into an array

by Limbic~Region (Chancellor)
on Aug 25, 2002 at 01:16 UTC ( [id://192611]=perlquestion: print w/replies, xml ) Need Help??

Limbic~Region has asked for the wisdom of the Perl Monks concerning the following question:

Background: I am trying to write a program for system administration that doesn't require code to be modified every time someone wants to add something new to it. Instead, the program reads a config file where people can just add "checks". One problem I have run into, is getting the output of File::Find into an array. One solution I have thought of is changing STDOUT to a file and then reading the file back in, but that seems like the wrong way. Any ideas would be appreciated.
EXAMPLE #!/usr/local/bin/perl -w use File::Find; use Config::IniFiles; use strict; my $cfg = new Config::IniFiles( -file => "config.ini" ); my $start = $cfg->val('FIND', 'start'); my $wanted = $cfg->val('FIND', 'wanted'); my @filesfound = File::Find::find( sub {eval $wanted} , "$start" ); foreach my $file (@filesfound) { print "$file is in the $start directory\n"; } contents of config.ini [FIND] start = /tmp wanted = print("$File::Find::name\n");
Now of course the foreach never gets run because what I am getting is the exit status of the File::Find and not its output.

Replies are listed 'Best First'.
Re: output of Find::File into an array
by Zaxo (Archbishop) on Aug 25, 2002 at 01:33 UTC

    I'm going to assume that the real code runs in taint mode, checking that config.ini contains no mischievious pranks. ( That is the case, right?...).

    File::Find doesn't work the way you seem to think. It does not return anything but a status. Here's how I'd populate @filesfound:

    my @filesfound; find( sub {eval( "$wanted") and push @filesfound, $File::Find::name }, "$start" );

    After Compline,
    Zaxo

      Thanks - this is what I was trying to accomplish and the way I was trying to accomplish it. I just couldn't seem to see past my nose!
Re: output of Find::File into an array
by blakem (Monsignor) on Aug 25, 2002 at 02:27 UTC
    Take a look at File::Find::Rule. It has an interface that might work well for you.

    From the docs:

    # find all the .pm files in @INC my @files = File::Find::Rule->file() ->name( '*.pm' ) ->in( @INC );

    -Blake

Re: output of Find::File into an array
by mkmcconn (Chaplain) on Aug 25, 2002 at 01:43 UTC
      Thanks mkmcconn, but the users that are going to be adding things to the config file aren't going to know how/what/why to push information to the array since it is in the code that shouldn't need to be modified. Zaxo came up with a viable solution that I am going to use. I did realize that what I was trying to do was the equivalent of expecting to see "foo bar" as a result to:
      my $var = print "foo bar"; print "$var\n"

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://192611]
Approved by talexb
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others lurking in the Monastery: (4)
As of 2024-03-29 06:44 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found