"Chas. Owens" <[EMAIL PROTECTED]> writes:

>> I don't understand why that happens.
>>

[snip] my version of script

> It looks like your problem is that you are creating a named subroutine
> inside another.
>
> I think this is close to what you desire.

Yes,  and I see you were able to come very close to what I was after
even with my terribly jumbled script.

It looks like what you've done is pass an entire sub function into
another sub function.  So there ends up being 3 sub function in all.

I see it works but I'm still a little confused about `wanted()'.

Is it not considered a sub function?  I'm not clear on `named' as
against simple `sub {...}'

The only biggish difference between your effort and what I wanted is
that I wanted to print both the filename and the line containing the
hit on $rgx. But it would have been hard to divine that out of what I
posted since I miss used File::Find;dir repeatedly when I meant
File::Find::name.

I made a (very) few changes to the main body and added a test to make
sure a command line flag was actually passed in from cli and will be
able to incorporate this into the main program... thanks.

I've included the new version and a diff file below just in case you
wanted to see what I'd done with the test.

------- 8< snip -------- 
#!/usr/local/bin/perl

use strict;
use warnings;

use File::Find;
use File::MMagic;

my $flag;
if([EMAIL PROTECTED] || $ARGV[0] eq "help"){
   usage();
   exit;
}else{
  $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::Find::name\n";
                                                print "$_\n\n";
                                                #last; #don't read the rest <= 
whoops, wrong...
                         # We actually want to read all of it
                         # in case another instance of `(?:^|:)$use '
                         # shows up
                                        }
                                }
                        }
                },
                @finddir
        );
}

sub usage {
  print "
Purpose: blah
Usage:   blah
example: blah
"
}

------- 8< snip -------- 

9,15c9,10
< my $flag;
< if([EMAIL PROTECTED] || $ARGV[0] eq "help"){
<    usage();
<    exit;
< }else{
<   $flag = shift;
< }
---
> my $flag = shift;
> 
33,38c28,29
<                                               print "$File::Find::name\n";
<                                               print "$_\n\n";
<                                               #last; #don't read the rest <= 
whoops, wrong...
<                          # We actually want to read all of it
<                          # in case another instance of `(?:^|:)$use '
<                          # shows up
---
>                                               print "$file\n";
>                                               last; #don't read the rest
46,53d36
< 
< sub usage {
<   print "
< Purpose: blah
< Usage:   blah
< example: blah
< "
< }



-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
http://learn.perl.org/


Reply via email to