in reply to (Segfault) Re: is_constant() - a routine that detects constant subs
in thread is_constant() - a routine that detects constant subs

there are a few other ways of setting constants i've included. in fact, i *NEVER* use constant, i don't like the syntax. i don't think => looks like assignment (i know, use constant makes it pretty obvious what it is, but i still use sub foo(){} anyway.)

see what this gives you...

sub bar { 1 }; sub baz() { 1 }; *zip = sub{ 1 }; *zap = sub(){ 1 }; foreach ( qw/ FOO One::FOO Two::FOO bar baz zip zap / ) { if ( Two::is_constant( $_ ) ) { print "$_ Yes\n"; #$_ so i know which one i'm checking } else { print "$_ No\n"; } }
samtregar, i'm glad you got this code working. it stuck in my head yesterday when i saw it mentioned, but i was too involved in the release of star wars to do anything about it.

~Particle *accelerates*

Replies are listed 'Best First'.
Re: Re: (Segfault) Re: is_constant() - a routine that detects constant subs
by samtregar (Abbot) on May 19, 2002 at 21:32 UTC
    I don't think that "bar" and "zip" are constants. As far as I know constants must have a prototype of "()". My is_constant() uses that to avoid testing subs that can't possibly be constants, but maybe that logic is flawed and I need to test all subs?

    -sam

      i'm sorry, perhaps i wasn't clear. i was using bar/baz and zip/zap as non-constant/constant pairs. you are correct in that an empty prototype is a necessary condition for a subroutine to be optimized to a constant.

      and truly, thanks for this code. i've been working on a module (possibly named Devel::TrackSub,) which will wrap all subs in any namespaces you specify. it'll be posted as soon as i finish the pod--perhaps tomorrow.

      developing this code was my first attempt at walking the symbol table, and at first seemed a little over my head. i think i've found an elegant solution that can be quite instrumental to others. i'm excited to post it soon.

      i think you've done the same here.

      ~Particle *accelerates*

      As far as I know constants must have a prototype of "()".

      Right. So you can also see if no prototype is set. You can do return 0 unless defined $proto and not length $proto; to cover all alternatives. As it is now you test subroutines with no prototypes. Also, I'd prefer if $is_const was initialized to 0 since the return statements returns dito.

      Cheers,
      -Anomo