Beefy Boxes and Bandwidth Generously Provided by pair Networks
go ahead... be a heretic
 
PerlMonks  

Re: reaching into a perl script from a package

by jimt (Chaplain)
on Oct 02, 2006 at 19:18 UTC ( #575929=note: print w/ replies, xml ) Need Help??


in reply to reaching into a perl script from a package

But, I would like to modify my package and have it reach into the script to see if a function exists.

If it's possible, I'd recommend going the other way. Have the executing script tell your package that the function exists, instead of having the package try to probe it.

With the suggested approaches here, you're hardwiring some function in your script into your package. What if the function moves? What if the function gets renamed? What if it gets deleted?

This is clearly a side effect. Somebody goes into the script and modifies the function, they then need to go into the separate package and modify the link to the function. Nothing makes it explicitly clear that anything else should care, its not like you're talking about modifying a library function it's clear that other calls may be affected.

So I think it'd be better to have the script tell the package instead. I'm thinking of something along these lines:

package FooBar; sub set_function_ref { my $function = shift; #do someting interesting with it. } in script.pl: sub interesting_fuction { # interesting things... } FooBar::set_function_ref(\&interesting_function);

And you're decoupled. Your package can now safely work anywhere else, and not depend upon a particular function existing in a particular package. Anything that wants to use FooBar just calls set_function_ref with the value it wants it to operate on.


Comment on Re: reaching into a perl script from a package
Download Code

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others cooling their heels in the Monastery: (10)
As of 2014-11-24 22:51 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    My preferred Perl binaries come from:














    Results (147 votes), past polls