Beefy Boxes and Bandwidth Generously Provided by pair Networks
There's more than one way to do things
 
PerlMonks  

Re^2: Passing a variable recursively

by tobyink (Abbot)
on Nov 06, 2012 at 09:30 UTC ( #1002442=note: print w/ replies, xml ) Need Help??


in reply to Re: Passing a variable recursively
in thread Passing a variable recursively

Precisely. You should give functions and objects everything they'll need to perform their duties. Loggers, database handles, etc should be provided to objects as part of their constructor, or passed to each method/function call.

The name for this general style of programming is "dependency injection" - you inject into an object/function everything it depends on. Generally speaking it is considered a good thing to do.

That said, it is possible to go far with dependency injection. If a function needs to fetch something from the web, it's a good idea to allow people to "inject" an LWP::UserAgent or compatible object. But if a function needs to multiply two numbers together, then allowing people to inject their own implementation of integer multiplication as a coderef is a step too far. Generally common sense will tell you what dependencies should be injectable and what can be hard-coded.

perl -E'sub Monkey::do{say$_,for@_,do{($monkey=[caller(0)]->[3])=~s{::}{ }and$monkey}}"Monkey say"->Monkey::do'


Comment on Re^2: Passing a variable recursively

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others browsing the Monastery: (12)
As of 2014-12-29 16:47 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    Is guessing a good strategy for surviving in the IT business?





    Results (193 votes), past polls