[Date Prev][Date Next][Thread Prev][Thread Next] [Search] [Date Index] [Thread Index]

Re: [FWP] Stumper



Michael G Schwern wrote:
> 
> I'm stumped.  I want to know how many times a certain function is
> called inside a lexical block without actually running that block of
> code.  For example:
> 
>         foo {
>             bar();
>             bar();
>             bar();
>         };
> 
> Assuming foo() is prototyped (&), how could it know that bar() is
> called three times in that block without actually running it?
> 
> I'd like to avoid a filter.  Maybe reading the opcodes with B?

I *think* this should work.

    use B::Deparse;
    sub foo (&) {
        my $code = shift;
        my $deparse = B::Deparse->new("-p");
        scalar(() = $deparse->coderef2text($code) =~ /\bbar\(/g);
    }

This would confuse random_package::bar with package_you_want::bar but
that shouldn't be too hard to take care of.  

I can't think of any other ambiguities offhand that Deparse doesn't
already disambiguate.

-- 
Rick Delaney
rick.delaney@home.com

==== Want to unsubscribe from Fun With Perl?  Well, if you insist...
==== Send email to <fwp-request@technofile.org> with message _body_
====   unsubscribe