Beefy Boxes and Bandwidth Generously Provided by pair Networks
good chemistry is complicated,
and a little bit messy -LW
 
PerlMonks  

checking from a module if a sub exists in main::

by tune (Curate)
on Nov 20, 2001 at 01:44 UTC ( #126395=perlquestion: print w/replies, xml ) Need Help??

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

Dear M0nX!

Recently I had to touch an old module, which is a general purpose database table row viewer/inserter/updater/deleter.
The behaviour of this module can be set up by exported hashes, and variables. It has a delete function which is deleting a row from the DB table. I wanted to add some functionality to its behaviour, but only for one table. So i added a new subroutine to the script that uses the module, and set up another exported variable to tell the module it can call the sub from the script.
But I feel it is very weak. First of all there is already too much exported variables, I don't want to follow the original authors stupid practice, and I would like to have elegant code. I would like to check from the module if the subroutine is existing in the caller namespace. Or at least in main namespace.
Is that possible? Thanks for any ideas.

--
tune

  • Comment on checking from a module if a sub exists in main::

Replies are listed 'Best First'.
Re: checking from a module if a sub exists in main::
by dragonchild (Archbishop) on Nov 20, 2001 at 01:54 UTC
    my $package = 'main'; my $function = 'foo'; { no strict 'refs'; if (defined &{$package.'::'$function}) { print "$function is defined in $package\n"; } }

    ------
    We are the carpenters and bricklayers of the Information Age.

    Don't go borrowing trouble. For programmers, this means Worry only about what you need to implement.

(Ovid) Re: checking from a module if a sub exists in main::
by Ovid (Cardinal) on Nov 20, 2001 at 01:57 UTC
Re: checking from a module if a sub exists in main::
by premchai21 (Curate) on Nov 20, 2001 at 02:55 UTC
    A better idea would be to do something like this:
    package Foo; use vars qw/$delHook/; $delHook = undef;
    Export $delHook if you like. Then, in the main script:
    $Foo::delHook = \&somehook; # or just $delHook if exported
    will set $delHook; in the module, when you need to call the sub:
    $delHook->(any_arguments_here) if defined $delHook;
    That way, rather than being restricted to calling a sub with a particular name, you call the subroutine by having a reference to it (which is what the \ does; the & is just to specify that it's the method and not something else). Note that the call will not occur if no delHook was set.

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others romping around the Monastery: (4)
As of 2020-07-03 20:55 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found

    Notices?