Sponsor

Saturday, October 6, 2007

Modul Recursive Directory

Dulu sebelum nemuin modul ini, untuk baca atau pengecekan suatu file yang ada dalam sub-sub direktory aku menggunakan fungsi recursive yang lumayan agak panjang. Tapi untung nya setelah nemuin nih modul, untuk baca file yang ada didalam sub-sub direktory terasa begitu mudah. Berikur contoh scriptnya :

#!/usr/bin/perl
use strict;
#--Modul Recursive Directory--
use File::Find;

my($Group, $Text) = @ARGV;
my $Spool = "/var/spool"; # or wherever your newsspool lives
$| = 1; # so we can see it run
find(\&Kibo, "$Spool/$Group");

sub Kibo
{
-d and print "$_\n";
-f and /^\d+$/ or return;
print "$_\r";

open(ARTICLE, $_) or return;
my @lines = ();

for my $line (@lines)
{
$line =~ /$Text/o and print $line;
}
}

No comments: