mab has asked for the wisdom of the Perl Monks concerning the following question:

I found this comment regarding this question on the site:

http://www.perlmonks.org/?node_id=226165

But for some reason, though, I can't get the suggestion to work. Here's my test case:
sub hello; *fn = $::{'hello'}; print "declared\n" if *fn{CODE}; print "not defined\n" if ! defined &fn; print "exists\n" if exists &fn; print "glob exists\n" if exists $::{'hello'};
For me, this test correctly prints "not defined" and "glob exists" but shouldn't it also print "declared" and "exists?"

Thanks for any insight you can give me on this.
  • Comment on How can I tell if a function has been forward declared but not yet defined?
  • Download Code

Replies are listed 'Best First'.
Re: How can I tell if a function has been forward declared but not yet defined?
by dragonchild (Archbishop) on Mar 30, 2005 at 20:17 UTC
    The scalar $hello doesn't exist. The subrouine &hello exists.
    sub hello; $fn = \&hello; # ...

    Alternately:

    sub hello; *fn = *hello; # ...

    But, either way, your check of $::{'hello'} will fail.

    Being right, does not endow the right to be rude; politeness costs nothing.
    Being unknowing, is not the same as being stupid.
    Expressing a contrary opinion, whether to the individual or the group, is more often a sign of deeper thought than of cantankerous belligerence.
    Do not mistake your goals as the only goals; your opinion as the only opinion; your confidence as correctness. Saying you know better is not the same as explaining you know better.

      The subroutine &hello doesn't exist. In fact $::{hello} is &hello's prototype (as a plain string). There's no CV or CV-ish value around yet--just a plain string being the prototype. When you do *hello, \&hello or $hello you create the glob at comile-time and so $::{hello} returns a glob. This means that

      sub hello; *fh = $::{hello}; *hello;
      works. See Re^3: Grabbing Variable Names for an elaboration.

      ihb

      See perltoc if you don't know which perldoc to read!

      %:: is the namespace for the main package and what I'm doing with $::{'hello'} is to get the glob of things pointed at by the name 'hello.' For example, do this and you'll see what I mean:
      sub hello { }; # declared and defined *fn = $::{'hello'}; print "declared\n" if *fn{CODE}; print "defined\n" if defined &fn;
      So I know the technique works - I just need a way to differentiate functions that are only forward declared from ones that are actually defined.
      Hey, I see what you're saying now. Sorry for being slow. I am still curious why the namespace approach doesn't work but thanks for this suggestion.
Re: How can I tell if a function has been forward declared but not yet defined?
by ihb (Deacon) on Mar 30, 2005 at 21:31 UTC

    If you would've waited two hours for me to answer instead of reposting the exact same question as a SoPW after just one hour, you would've seen my elaborate answer.

    ihb

    See perltoc if you don't know which perldoc to read!

      my apologies for not waiting - when i realized that the thread i posted against was a few years old, i reposted it. in any case, thank you for the in depth answer - very excellent.