Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl: the Markov chain saw
 
PerlMonks  

Re: Perl Programming guidlines/rules

by hardburn (Abbot)
on Nov 21, 2002 at 16:00 UTC ( [id://214801]=note: print w/replies, xml ) Need Help??


in reply to Perl Programming guidelines/rules

5. OO always to be used where possible

For CGI scripts, I don't see the point. Most CGI scripts are going to have around 100-200 lines of real code (not including the stuff that is just outputing HTML). While using OO-based modules is a good idea, it seems like overkill for the script itself.

17. Assign CGI parameters to hashes
...
23. No Global Variables

OK, let's give the mandatory "Global Variables are Bad" (tm). Fine.

In CGI scripts, I prefer to put options passed to the script in global vars. Every function can get that information anyway, we just make it easier this way. Each of my scripts has a set_params() function that is the only function allowed to change those global vars. Example:

use CGI qw(:standard); # Other modules my ($param1, $param2, $param3); sub set_params { $param1 = param("param1") || 0; $param2 = param("param2") || 0; $param3 = param("param3") || 0; } set_params(); # Whatever other code you want

(I rarely use the fancy features of CGI.pm, so I usually don't bother with the OO interface, though I probably should).

The problem with putting them into a hash is that you take away many of the benifits you get from "use strict 'vars'".

7. No hardcoding of values especially file paths, script names etc
8. Config files or config scripts to be used

How do you get the config file without hardcoding the path to it?

Update: For config files, I suggest being very careful with absolute paths. At my job, we recently moved to a new server, and we had a lot of problems with scripts that were using absolute paths that were no longer available on the new server. Additionally, our test server had some paths to the server's directory that didn't exist on the production server, which broke even more stuff.

Replies are listed 'Best First'.
Re: Re: Perl Programming guidlines/rules
by perrin (Chancellor) on Nov 21, 2002 at 17:24 UTC
    Note that your approach to widely scoped lexical variables (those are not globals, by the way) could be a real problem in a persistent environment like FastCGI, PerlEx, or mod_perl. Those are closures, so if any of them didn't get set on a particular request they would retain their previous values.

      . . . those are not globals, by the way . . .

      OK, fine, in Perl, there technically aren't any global vars. There is just vars that have a really wide scope. However, for practical matters, widely scoped lexicals are global vars.

        Not really. "Global variable" means something specific in Perl: a variable like $SomePackagage::Foo or just $Foo that is declared without "my". They are global because any other package can see them and change them. Lexical (my) variables are not visible outside of their current scope. It's a significant difference.

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others perusing the Monastery: (3)
As of 2024-04-26 05:56 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found