On Sun, Nov 23, 2008 at 17:55, Harry Putnam <[EMAIL PROTECTED]> wrote:
> The program I'll post below is really only a test of a subroutine I
> want to use in a larger program. Trying to get the subroutine ironed
> out in this test script below so there is a little extra bumping
> around to get it executed as sub routine, but It fails with these
> errors:
>
> Variable "$rgx" will not stay shared at ./test line 30.
> Global symbol "$finddir" requires explicit package name at ./test line
> 19.
> Execution of ./test aborted due to compilation errors.
>
> I don't understand why that happens.
>
> ------- 8< snip --------
>
> #!/usr/local/bin/perl
>
> use strict;
> use warnings;
>
> my $flag = shift;
>
> checkfile($flag);
> sub checkfile {
> use File::Find;
> use File::MMagic;
> use FileHandle;
>
> my ($rgx,$use,@finddir);
> $use = shift @_;
> $rgx = qr/(^|:)$use /;
> @finddir = "/usr/portage/profiles";
>
> find(\&wanted, @finddir) or die " Failed to open
> finddir <$finddir>: $!";
> sub wanted {
> my ($mm,$res,$testfile);
> $testfile = $_;
> $mm = new File::MMagic; # use internal magic file
> $res = $mm->checktype_filename($testfile);
> if ($res =~ /^plain\/text/) {
> open(FILE,"<$File::Find::dir") or die "Can't open <$File::Find::dir>:
> $!";
> }
> while(<FILE>) {
> if (/$rgx/) {
> print " $File::Find::dir\n";
> print " $_\n";
> }
> }
> }
> }
It looks like your problem is that you are creating a named subroutine
inside another.
I think this is close to what you desire.
#!/usr/local/bin/perl
use strict;
use warnings;
use File::Find;
use File::MMagic;
my $flag = shift;
checkfile($flag);
sub checkfile {
my $use = shift;
my $rgx = qr/(?:^|:)$use /;
my @finddir = qw(/usr/portage/profiles);
my $mm = File::MMagic->new;
find(
sub {
my $file = $File::Find::name;
my $res = $mm->checktype_filename($file);
if ($res =~ m{^text/plain}) {
open my $fh,"<", $file
or die "Could not open $file: $!";
while (<$fh>) {
if (/$rgx/) {
print "$file\n";
last; #don't read the rest
}
}
}
},
@finddir
);
}
--
Chas. Owens
wonkden.net
The most important skill a programmer can have is the ability to read.
--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
http://learn.perl.org/