Beefy Boxes and Bandwidth Generously Provided by pair Networks
laziness, impatience, and hubris
 
PerlMonks  

Re^8: Global variable vs passing variable from sub to sub

by Steve_p (Priest)
on Sep 15, 2004 at 21:26 UTC ( [id://391305]=note: print w/replies, xml ) Need Help??


in reply to Re^7: Global variable vs passing variable from sub to sub
in thread Global variable vs passing variable from sub to sub

Planning takes time and time is money. If I could get by with less planning, I will....Never count on plans. Code should be flexible.

I couldn't agree more. It's useless to attempt to modularize anything until you're working on your code (by modularizing, I include putting code into functions or separate modules). So, don't modularize your code until your working on it. If you see some repeating code, or code you might want to use elsewhere, modularize it then so you don't repeat yourself later. Repeating code is what takes time and money, especially in the maintanence costs. Smart coding saves money.

You need luck when combining all code that requires a given piece of data into a single, scoped block, or region, because if you end up needing that data once you're working on a different part of the script (things like that come up in the real world), things can get messy.

Absolutely no luck is needed if you create functions or modules to access the data from somewhere else. It does prevent some boneheaded errors, though, caused by accidentily using a variable with the same name twice, so in that way, I guess it makes you lucky.

I don't think much about making each routine suitable for a module...

Again, I could not agree with you more. No module should every be a 100% solution to any problem. Every situation is unique and every script is unique. However, if you could put about 80% of the code into a module, that's where life becomes easy.

Let's think about an example module: LWP::Simple. It would be impossible to write a module that could access all the different sites on the Internet with a different function call for each one (the thought of getprint_google just makes my head spin). What LWP::Simple does is save you from having to write Perl networking code by allowing you to write my $pm = getprint("http://www.perlmonks.org"); instead. Think what it would be like to write a web client or even WWW::Mechanize without LWP. The beauty of modularized code is that once you have a great API, you never have to do it again.

  • Comment on Re^8: Global variable vs passing variable from sub to sub

Replies are listed 'Best First'.
A reply falls below the community's threshold of quality. You may see it by logging in.

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others having an uproarious good time at the Monastery: (2)
As of 2024-04-24 22:50 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found