Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl Monk, Perl Meditation
 
PerlMonks  

Re: Code references as use() parameters

by MarkM (Curate)
on Apr 09, 2003 at 16:25 UTC ( #249287=note: print w/ replies, xml ) Need Help??


in reply to Code references as use() parameters

The real problem isn't that the subroutine reference is taken before it exists, as this actually works due to the magic of Perl:

package A; sub import { *b = $_[1] }; BEGIN { $INC{"A.pm"} = undef } # Stop 'use' from loading A.pm package main; use A \&c; print A::b(), "\n"; # prints "YO\n" sub c { "YO" }

I suspect your problem is that Alzabo::MethodMaker is trying to execute name_maker() before it is defined (otherwise, you wouldn't have this question, as the above code should 'work'). The only way around this is to define name_maker() first, or to delay execution of Alzabo::MethodMaker::import using one of the means suggested by other people.


Comment on Re: Code references as use() parameters
Download Code
Re: Re: Code references as use() parameters
by diotalevi (Canon) on Apr 09, 2003 at 16:55 UTC

    I'd like to be clear here that Alzabo::MethodMaker is only an example and that I intended to ask for ideas for handling this problem in the general case.

      I think I was clear that in the general case, as long as the subroutine is not invoked as part of the 'use', there is no problem. If the subroutine is invoked, then it needs to have already been defined. Only taking the reference of an as-yet-non-existent subroutine is not dangerous.

      Actually, there is one danger, and it has to do with prototypes. If the prototypes differ (when the function actually is defined), Perl will croak.

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://249287]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others about the Monastery: (15)
As of 2014-09-18 13:56 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    How do you remember the number of days in each month?











    Results (116 votes), past polls