develooper Front page | perl.perl5.porters | Postings from August 2001

Re: How to redefine sub that has prototypes without warning at runtim e?

Thread Previous | Thread Next
From:
Michael G Schwern
Date:
August 17, 2001 23:21
Subject:
Re: How to redefine sub that has prototypes without warning at runtim e?
Message ID:
20010818022141.E641@blackrider
On Fri, Aug 17, 2001 at 10:56:36PM -0700, Kurt D. Starsinic wrote:
>         #define foo "foo"               ->      sub foo () { "foo"; }
>         #undef  foo                     ->      undef &foo;

This outright doesn't work.  Why?  Inlining.

    sub foo () { "foo" }
    undef &foo;
    print foo();

At compile time, foo() is inlined to "foo".  C<undef &foo> produces a
warning "Constant subroutine foo() undefined" because it doesn't have
the desired effect.  C<print foo()> has already been changed to
C<print "foo">.


>     . . . and given the following input . . .
> 
>         #define foo "foo"
>         #undef  foo
>         #define foo <something complex>
> 
>     . . . is there an alternate mapping that wouldn't trigger a warning?

A few options.  First, drop the () prototype.  That will kill the
inlining and it will work as expected.  I don't think micro-efficiency
is the issue here.

Second, map it to this:

    #define foo "foo"       sub foo { "foo" }
    #undef  foo             { no warnings 'redefine';  undef &foo }
    #define foo <stuff>     { no warnings 'redefine';  *foo = sub {<stuff>} }

Also, would it make more sense to say C<*foo = sub { undef }> rather
than C<undef &foo>?


-- 

Michael G. Schwern   <schwern@pobox.com>    http://www.pobox.com/~schwern/
Perl6 Quality Assurance     <perl-qa@perl.org>	     Kwalitee Is Job One
If you have to shoot, shoot!  Don't talk.
		-- Tuco, "The Good, The Bad And The Ugly"

Thread Previous | Thread Next


nntp.perl.org: Perl Programming lists via nntp and http.
Comments to Ask Bjørn Hansen at ask@perl.org | Group listing | About