The UNIX and Linux Forums  
Hello and Welcome from United States to the UNIX and Linux Forums! Thank You for Visiting and Joining Our Global Community.

Go Back   The UNIX and Linux Forums > Top Forums > Shell Programming and Scripting
.
google unix.com



Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts and shell scripting languages here.

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
Perl: Global Search and replace epi8 Shell Programming and Scripting 3 05-06-2008 03:18 PM
Problem with global and local variables qzv2jm Shell Programming and Scripting 2 03-04-2008 01:18 PM
Declaring Global Variables in KLD int80h BSD 1 01-21-2008 09:11 AM
global variables in KLD (FreeBSD) int80h High Level Programming 0 01-17-2008 08:14 PM
How to declare global variables for shell script risshanth UNIX for Dummies Questions & Answers 2 10-31-2007 03:27 AM

Closed Thread
English Japanese Spanish French German Portuguese Italian Dutch Swedish Russian Norwegian Hungarian Hebrew Danish Powered by Powered by Google
 
LinkBack Thread Tools Search this Thread Rate Thread Display Modes
  #1 (permalink)  
Old 08-26-2005
reggiej reggiej is offline
Registered User
  
 

Join Date: May 2005
Posts: 27
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.
  #2 (permalink)  
Old 08-26-2005
vertigo23's Avatar
vertigo23 vertigo23 is offline
Registered User
  
 

Join Date: Jul 2005
Location: SF, CA
Posts: 73
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 (permalink)  
Old 08-26-2005
cbkihong cbkihong is offline Forum Advisor  
Advisor
  
 

Join Date: Sep 2002
Location: Hong Kong, China
Posts: 1,624
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;
}
You can tell that the scope of "my $var" is within BLOCK 1 but not to subroutines called inside. So (1) and (3) are 10 and 20 respectively. So which $var did mysub() change in the subroutine? It's the global $var, which is shown in (5) as "4" (2 invocations) after the scope of "my $var" expires. As you can be certain of the scope (instead of depending on the call stack, i.e. the nesting of subroutines that get called), it will be easier to debug when the value goes wrong. Perl also gives you a variant "local" which implements the scope-depend-on-call-stack policy (rare among commonly in-use programming languages today), so (2), (4), (6) are 102, undef and 2. This has a disadvantage that the scope depends on the call stack at runtime so it is more confusing.

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 (permalink)  
Old 08-29-2005
reggiej reggiej is offline
Registered User
  
 

Join Date: May 2005
Posts: 27
Thanks for the replys. I'll have much better understanding of the importance of using locals.
Sponsored Links
Closed Thread

Bookmarks

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On




All times are GMT -4. The time now is 07:34 AM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited. Language Translations Powered by .
vBCredits v1.4 Copyright ©2007 - 2008, PixelFX Studios
The UNIX and Linux Forums Content Copyright ©1993-2009. All Rights Reserved.Ad Management by RedTyger

Content Relevant URLs by vBSEO 3.2.0