Beefy Boxes and Bandwidth Generously Provided by pair Networks
XP is just a number

Re: passing a variable from one subroutine to another

by jeroenes (Priest)
on Oct 05, 2001 at 14:38 UTC ( #116956=note: print w/replies, xml ) Need Help??

in reply to passing a variable from one subroutine to another

You are asking about general advice, so that's what you'll get ;-).

What you have done (implicitly) is creating a global variable. That's a Bad Thing (tm).

Why? Because your subs become dependent on each other. On its turn that means that your code becomes harder to debug/ maintain, harder to reuse ("Where the hack does that $error come from?") and easier to break, among others.

So, pass the variable to your sub. Or pass an object or hash that contains the info or has the right methods. Everything is better than a global.

Try to read 'Code Complete' by McConnell to get it explained better than I could....

"We are not alone"(FZ)

PS: ++ for the use of strict, and for your approach to break the sub into components. Maybe try to think of the different functionalities. Like: Is error reporting a subfunction of logging, or something separate, or at another level, or something that the average user should be protected from (yes!)?

  • Comment on Re: passing a variable from one subroutine to another

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://116956]
[choroba]: no, just trying to judge a book in the same way
[choroba]: i.e. using it for a totally different purpose than it was written for

How do I use this? | Other CB clients
Other Users?
Others taking refuge in the Monastery: (5)
As of 2017-11-17 21:42 GMT
Find Nodes?
    Voting Booth?
    In order to be able to say "I know Perl", you must have:

    Results (274 votes). Check out past polls.