gnu@perl has asked for the wisdom of the Perl Monks concerning the following question:

I am writing a program to maintain log files. I know there are a ton of things out there to do this, but I am kind of a roll your own guy. I learn more when I write it than when I use anothers.

Anyway, I have a config file like the following:

/opt/gvc/gvc_dtfr/port*/done/*gz 20 /opt/gvc/gvc_dtfr/log 30

The first part is the path glob and the second is the maximum mtime to keep. As you can see, the first line of the config file would not parse correctly in an opendir/readdir combination.

I would like to be able to retain this globbing ability in the config file, otherwise the top line would turn into over 100 lines if I had to enter each one seperatly.

Any Ideas on how I can use this glob quickly and efficiently in a perl program (*nix environment).

TIA, Chad

Replies are listed 'Best First'.
Re: parsing directory glob in config file
by fglock (Vicar) on Sep 13, 2002 at 19:08 UTC
      I had been trying that like this:
      my @files; open (CFG,"<$ENV{HOME}/bin/$configFile") or die "Cannot Open Config Fi +le: $!\n"; while (<CFG>){ chomp; s/ \d+$//; @files = < $_ >; } close(CFG); print join ("\n", @files);

      But I get "glob failed (child exited with status 1) at ./old_file_scrub line XX". If I trim the entry in the config file to the point that there is only 1 "*" in it, it works fine, but not with both.

        You'll need to split the path at each wildcard, expand each level, concatenate the next piece to each result, and so forth. That's not particularly fun, but hey, you'll be learning!

      Also, this is a 5.005 box, File::Glob is not on it.
Re: parsing directory glob in config file
by mothra (Hermit) on Sep 13, 2002 at 21:17 UTC

    perldoc -f glob for starters.

    It'll just do what you think it should.

    e.g.

    globfoo ======= #!/usr/bin/perl -w my @files = glob("foo*/ba*/"); print join "\n", @files, "\n"; mothra@penderel:~/src/Perl$ ls -d foo*/ba*/ foo1/bar1/ foo1/bar2/ foo1/bar3/ foo2/baz2/ foo2/baz3/ mothra@penderel:~/src/Perl$ ./globfoo foo1/bar1/ foo1/bar2/ foo1/bar3/ foo2/baz2/ foo2/baz3/

    etc.

    It's probably a bad idea to roll your own logfile implementation (i.e. you could be learning lots of *other* things besides trying to make a wheel more round), but alas, to each their own.

Re: Parsing directory glob in config file.
by Zaxo (Archbishop) on Sep 13, 2002 at 20:44 UTC

    There is perl's glob, or else there is File::Find.

    With glob,

    open CONF, '/path/to/.config' or die $!; while (<CONF>) { my ($fglub, %mto) = split; for (glob $fglub) { -M < $mto and next; # do stuff to an out of date file } } close CONF or die $!;

    After Compline,
    Zaxo