Gossamer Forum
Home : General : Perl Programming :

my, local and global variables...

Quote Reply
my, local and global variables...
Hi folks,

Can somebody explain the differences between declaring variables as "my", "local" or "global"? I've scanned the Perl docs and I understand the basics of it, but not the intracacies (the Perl docs are a bit over my head!). For a start I don't see "global" in perlfunc.

For example, if you declare a global at the start of your script, does that mean it's locked to that value, or that you can't use it as a local variable?

And are "my" and "local" the same, or is there a minor difference.

And what are the advantages/disadvantages of using them?

And, and...

You get the idea. Could anybody give me a basic rundown on them?

Cheers,
adam
Quote Reply
Re: my, local and global variables... In reply to
dahastra,

Bobsie answered a 'simular'question already, take a look at: http://www.gossamer-threads.com/...um3/HTML/001414.html
Quote Reply
Re: my, local and global variables... In reply to
Global variables are variables that can be used anywhere in a program, whether in a subroutine or not (unless an identical variable name is declared as local or my).

Local variables are variables that are to be used in a particular block of code only. If a variable by the same name is also global, the value of the global variable is not affected by any changes to the local variable.

My variables work similarly to local variables except that new copies of the my variables are created every time the subroutine (or other code block) that contains them is called.

I hope this helps.
Quote Reply
Re: my, local and global variables... In reply to
Thank you gentlemen,

I have a follow-up question though (as usual!). Is there any *need* to declare variables as global. Won't declaring the variables as normal (as in the config portion of most scripts) do the same thing, or does declaring them as global add some handy attribute?

Again thanks,
adam
Quote Reply
Re: my, local and global variables... In reply to
Hi,

Here's some guidelines for you:

1. All variables are global unless stated otehrwise! If you don't declare a variable with my() or local() then it is global.

2. It's a bad idea to use globals unless you have to, or unless your script is just a quick fix to get something done. Your code will be difficult for others to read, and you may run into name conflicts.

3. local() variables are local dynamically scoped, and are visible to the block they are defined, as well as subroutines that call them. So:

Code:
sub a {
local $b = 5;
&b;
}

sub b {
print $b;
}
&a;

will print 5.

4. Variables declared with my() are scoped only with the current block! So the same code above with my $b = 5, won't print anything as subroutine b can't see $b.

5. You should use my() where possible and avoid local() as it is slower, and won't run under use strict.

6. Code all your programs using use strict, it enforce better programming (Yes, I know Links and DBMan don't follow that rule). =)

Hope that helps,

Alex
Quote Reply
Re: my, local and global variables... In reply to
Alex,

Thanks very much, that sorts it out in my head nicely. Now I'm off to do some research on mod_perl and use strict. Busy, busy, busy!

Thanks again,
adam
Quote Reply
Re: [Alex] my, local and global variables... In reply to
In Reply To:
6. Code all your programs using use strict, it enforce better programming (Yes, I know Links and DBMan don't follow that rule). =)

And why is that then? :-)

- wil
Quote Reply
Re: [Wil] my, local and global variables... In reply to
I'd say because Links (flatfile) and DBMAN (flatfile) were written while Alex was still learning. It'd be a pain the in ass to strict-ify them now. All of GT's newer programs are coded in strict.

--Philip
Links 2.0 moderator
Quote Reply
Re: [ThatPerson1024] my, local and global variables... In reply to
I'm having trouble stricty-fying a one liner <g> <= j/k Paul.

It would be great if someone would of told me years ago "use strict" and would of made me code every single line every under "use strict".

- wil

Last edited by:

Wil: Nov 29, 2001, 12:06 PM
Quote Reply
Re: [Wil] my, local and global variables... In reply to
>>I'm having trouble stricty-fying a one liner <g>.<<

Which is?