Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl: the Markov chain saw
 
PerlMonks  

Re^4: OO-call bug uncovered & autovivified functions: defined? exists?

by ikegami (Patriarch)
on Oct 29, 2007 at 01:46 UTC ( [id://647772] : note . print w/replies, xml ) Need Help??


in reply to Re^3: OO-call bug uncovered & autovivified functions: defined? exists?
in thread OO-call bug uncovered & autovivified functions: defined? exists?

I see what you mean, but I don't see that as a requirement. The first reference taking could just as easily return undef. In the odd (strange and rare) case you need to take a reference to a function that hasn't been compiled yet, you could always declare it using a stub.

  • Comment on Re^4: OO-call bug uncovered & autovivified functions: defined? exists?

Replies are listed 'Best First'.
Re^5: OO-call bug uncovered & autovivified functions: defined? exists? (compat)
by tye (Sage) on Oct 29, 2007 at 02:03 UTC

    Perl has long allowed the taking of references to functions before they have been compiled or even declared. So support for that is indeed required until after the decision is made to no longer support that and after the deprecation cycle can happen.

    - tye        

      Of course. I'm not arguing for such a change. There already exists sufficient tools (defined) to check if a function exists. I'm just challenging the claim that autovivification through \& is necessary to call functions loaded dynamically.

Re^5: OO-call bug uncovered & autovivified functions: defined? exists?
by Somni (Friar) on Oct 29, 2007 at 02:08 UTC
    Strange and rare? A simple dispatch table illustrates taking a reference before a function is seen, as the table typically comes before subroutines.

    my %OPS = ( frob => \&frobnicate, knob => \&knobnicate, ); sub frobnicate { ... } sub knobnicate { ... }

    As for a reference evaluating to undef, the concept seems wrong. If a reference can't be taken it should be an error; if it can, you should get a reference. Are there any examples of this behavior in Perl now?

      No, it doesn't. The function gets created by sub before \& is executed.

      print "a:", exists(&frobnicate)?1:0, "\n"; # 1 my %OPS = ( frob => \&frobnicate, knob => \&knobnicate, ); BEGIN { print "b:", exists(&frobnicate)?1:0, "\n"; } # 0 sub frobnicate { '...' } sub knobnicate { '...' }
        Hm, so it does. The only non-contrived example I can think of is:

        BEGIN { print exists(&frobnicate)?1:0, "\n"; my %OPS = ( frob => \&frobnicate, knob => \&knobnicate, ); sub runops { print $_->(), "\n" for values %OPS } } sub frobnicate { 'frobnicate' } sub knobnicate { 'knobnicate' }

        I have done it in the past in order to keep the dispatch table visible only to the function using it, but I find the extra indentation rather ugly so I tend to avoid it. I don't know if I'd call it strange and rare, but it certainly isn't unexpected. The need for it is also removed in 5.10, but I mention this only so that someone else won't.

        Edit: I realize now this has gotten somewhat far afield of the original question. You asked me to substantiate that if you remove auto-vivification by reference then all subroutines would have to be defined before they can be referred to. It's true, they would need only be declared, not defined, before taking a reference.