![]() |
|
|
|
|
|||||||
| Forums | Portal | Register | Forum Rules | FAQ | Contribute | Members List | Arcade | Search | Today's Posts | Mark Forums Read |
| Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts here. |
|
|
||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| Perl: Global Search and replace | epi8 | Shell Programming and Scripting | 3 | 05-06-2008 12:18 PM |
| Problem with global and local variables | qzv2jm | Shell Programming and Scripting | 2 | 03-04-2008 10:18 AM |
| Declaring Global Variables in KLD | int80h | BSD | 1 | 01-21-2008 06:11 AM |
| global variables in KLD (FreeBSD) | int80h | High Level Programming | 0 | 01-17-2008 05:14 PM |
| How to declare global variables for shell script | risshanth | UNIX for Dummies Questions & Answers | 2 | 10-31-2007 12:27 AM |
|
|
Submit Tools | LinkBack | Thread Tools | Display Modes |
|
#1
|
|||
|
|||
|
perl global variables
Can someone give me "the lecture" on why you shouldn't make all your varables global when programming in perl. I have been doing this but I have heard that it is not a good practice.
|
| Forum Sponsor | ||
|
|
|
#2
|
||||
|
||||
|
From a purely practical point-of-view, it's not so much that globals are bad, as it is that locals are really handy. Say you're in a loop, and you want to do the same function with a different data in each iteration. If you use a local (my) variable to feed data to the function, you're guarenteed that no data from a previous iteration of the loop can pollute a later iteration, if say the variable doesn't get filled every time.
Also, with local variables, you can re-use common variable names like $i for loop iterations and not have to worry about there already being data in the var. |
|
#3
|
|||
|
|||
|
Keeping things local that should be local is a good housekeeping practice.
Technically speaking, using local (my) variables arises from the need of eliminating namespace clashes, but the fact is it has a somewhat unrelated purpose. That is, to make sure the scope of a variable solely depends on the lexical scope only. So you can tell the scope of the variable directly by looking within the current block and nowhere else. Subroutines invoked from within this block will not see the variable. so, Code:
{ # BLOCK 1
my $var = 10;
{ # BLOCK 2
local $var2 = 100;
&mysub;
print "\$var = $var\n"; # (1)
print "\$var2 = $var2\n"; # (2)
}
$var += 10;
print "\$var = $var\n"; # (3)
print "\$var2 = $var2\n"; # (4)
}
&mysub;
print "\$var = $var\n"; # (5)
print "\$var2 = $var2\n"; # (6)
sub mysub {
$var += 2;
$var2 += 2;
}
If you need to deal with references, you may need to expire a data structure pointed to by a reference at a certain point in the lifetime of the script by controlling the lifetime of the references. If your references are declared as local variables they will naturally go out of scope when the containing block terminates and subsequently get cleaned up. Otherwise, references as global variables will stick around unless you reassign its value. That increases the chance of clashes if you forget to do so. But bear in mind that Perl global variables are not truly global (see the perlmod manpage), but further subject to the package (a.k.a. namespace) mechanism. Package is the method that addresses name clashes. This is important as we build components that are intended to be reused, so we don't want the $name defined in one component used to clash with another $name in my program. But I'm not going into details of packages here. See the perlmod manpage or relevant literature for details. This explains why you should try to put your code in modules (at least, in different packages) and use local variables most of the time. Not only this makes your code more reusable, it also helps prevent a lot of problems in the long run as your code becomes more complicated when you try to stick bits and pieces together. |
|
#4
|
|||
|
|||
|
Thanks for the replys. I'll have much better understanding of the importance of using locals.
|
|||
| Google The UNIX and Linux Forums |