in reply to Re: Function reference undef when extract from hash?
in thread Function reference undef when extract from hash?

So that I need to declare a subroutine prototype if I declare the hash outside of the function?
  • Comment on Re^2: Function reference undef when extract from hash?

Replies are listed 'Best First'.
Re^3: Function reference undef when extract from hash?
by chromatic (Archbishop) on Mar 04, 2008 at 06:51 UTC

    No.

    You need to declare the hash before you try to use it.

    This will work:

    # hash declaration # sub declarations # call some sub that uses the hash

    This will not work:

    # call some sub that uses the hash # hash declaration # sub declarations

    If you want to experiment with this on your own, move all of the code into individual subroutines and call them from main():

    # declare this as a file-scoped lexical my %Op; main(); # subs follow sub main { assign_to_Op(); do_something_else(); } sub assign_to_Op { %Op = ( ... ); } sub do_something_else { my $code = $Op{ ... }; $code->(); }