http://qs321.pair.com?node_id=647768


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

Consider, you take a reference to \&foo in one part of your code. No symbol table entry is created, but somehow taking the reference Just Works. Later in the same code another reference is taken, \&foo. If no symbol table entry is created, how can these two references refer to the same subroutine?

There has to be some agreement on what underlying subroutine these two references refer to. This has to agree with the subroutine body that, supposedly, is eventually created.

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

Replies are listed 'Best First'.
Re^4: OO-call bug uncovered & autovivified functions: defined? exists?
by ikegami (Patriarch) on Oct 29, 2007 at 01:46 UTC

    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.

      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.

      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 { '...' }