CGI/Perl Guide | Learning Center | Forums | Advertise | Login
Site Search: in

  Main Index MAIN
INDEX
Search Posts SEARCH
POSTS
Who's Online WHO'S
ONLINE
Log in LOG
IN

Home: Perl Programming Help: Beginner:
variable scope

 



zolteck06
New User

Apr 6, 2013, 7:08 AM

Post #1 of 7 (605 views)
variable scope Can't Post

So this is probably a redundant question that has been answered before, and believe me I have tried answering it myself I have Modern perl and Perl programming by orileys and there is a lot of stress put on variable declaration
I just can't wrap my foiled brain around the subject one line in the book that got my attention was on minimizing the use of global vars maybe if someone could answer this question the rest of this will come together for me? lol sorry but Im rather confused by it all.


FishMonger
Veteran / Moderator

Apr 6, 2013, 7:14 AM

Post #2 of 7 (604 views)
Re: [zolteck06] variable scope [In reply to] Can't Post

What is your question? I don't see one in your post.


zolteck06
New User

Apr 6, 2013, 7:24 AM

Post #3 of 7 (600 views)
Re: [FishMonger] variable scope [In reply to] Can't Post

The question is: why is it a good idea to minimize global vars in your scripts?or why is it so important to have vars that are subroutine specific to "protect" the var...protect from what?

In Reply To


Laurent_R
Veteran / Moderator

Apr 6, 2013, 7:53 AM

Post #4 of 7 (594 views)
Re: [zolteck06] variable scope [In reply to] Can't Post

Limiting the scope of a variable to the chunk of code where it is needed (a block, a subroutine, a package) helps avoiding bugs that are often insidious and difficult to track.

It also helps maintaining the code. Suppose you have in a long program global variables with very common names such as $line, $file, $count, $size, $sum, @list, etc. You must change the program and have to add a newsubroutine to do something new. If you don't declare locally your variables, you might unexpectedly use a variable name that is used somewhere else in the program and that is likely to break everything, as the $line variable will no longer contain the expected value. If you make your new variables local (or, more precisely lexically scoped) to the new subroutine, then you don't have this danger of side effects: locally in the subroutine, your local variable will contain what you expect, but once you are executing code outside of the subroutine, the variable will contain what the writed of the original code expected it to contain.

Of course, using the 'use strict;' and 'use warnings;' pragmas is part of the same strategy to prevent this type of insidious side effects.


BillKSmith
Veteran

Apr 6, 2013, 6:48 PM

Post #5 of 7 (581 views)
Re: [Laurent_R] variable scope [In reply to] Can't Post

Laurent provided excellent reasons for limiting the scope of perl variables. I am not sure that you understand what we mean by global and lexical variables.

The difference is primarily in the scope rules. When we do not use strict, all variables default to global. Their scope is the entire file (in fact, even other files if we prefix them with the package name). When we use strict, all variables must be declared. Variables declared with the keyword my are lexical variables which have the scope of the smallest enclosing block.

On extremely rare occasions, we need a global variable when strict is in effect. We can declare them with either use vars or our. Refer perldoc -f our for an explanation of the difference.
Good Luck,
Bill


g4143
Novice

Apr 7, 2013, 2:05 PM

Post #6 of 7 (574 views)
Re: [zolteck06] variable scope [In reply to] Can't Post

Try looking at the enclosed code below.


Code
#!/usr/bin/perl 

use warnings;
use strict;

my $var = 12;

sub do_something
{
return $var + 10;
}

print do_something(), "\n";

$var = 34;

print do_something(), "\n";

__END__


The function do_something has the nasty feature of returning different values depending on how the program deals with the global variable $var and that can be a nasty feature for several reasons already mentioned in previous posts.


zolteck06
New User

Apr 9, 2013, 9:22 AM

Post #7 of 7 (556 views)
Re: [g4143] variable scope [In reply to] Can't Post

Thank you guys sooo much. That was a very logical discourse that I understood well....I am grateful.

 
 


Search for (options) Powered by Gossamer Forum v.1.2.0

Web Applications & Managed Hosting Powered by Gossamer Threads
Visit our Mailing List Archives