ultimate n00b question re: my / our

K

Koncept

I hope this is not too silly...

What is the difference between setting a var with my or our? When would
I know to distingish between the two? Also, is this necessary to always
define when setting a var?

Thanks!
 
N

Nick Santos

Koncept said:
I hope this is not too silly...

What is the difference between setting a var with my or our? When would
I know to distingish between the two? Also, is this necessary to always
define when setting a var?

Thanks!


It is not necessary to define your variables as mine or our. You can simple
declare them like so

$foo;
#or
$foo=12;

the first of which isn't necessary. One of the great things about perl is
that scalar variables are pretty much all your basic types in one, so you
don't even NEED to declare them at all, you can just start using them. As
for the case of my and our, I've seen people use my because it defines that
it comes from a local source(though I may be wrong). I think the programmers
that put my are using it as much for their knowledge as for the compilers. I
personally have never heard of using an "our" defiinition." That'll have to
come from someone else's knowledge
-Nick
 
K

Koncept

Nick Santos said:
As
for the case of my and our, I've seen people use my because it defines that
it comes from a local source(though I may be wrong). I think the programmers
that put my are using it as much for their knowledge as for the compilers.

I figured this much. Thanks for your help.

As for "our" this is what I found:

"An our declares the listed variables to be valid globals within the
enclosing block, file, or eval. That is, it has the same scoping rules
as a ``my'' declaration, but does not create a local variable. If more
than one value is listed, the list must be placed in parentheses. The
our declaration has no semantic effect unless ``use strict vars'' is in
effect, in which case it lets you use the declared global variable
without qualifying it with a package name."

http://aspn.activestate.com/ASPN/docs/ActivePerl/lib/Pod/perlfunc.html#i
tem_our
 
N

Nick Santos

Koncept said:
compilers.

I figured this much. Thanks for your help.

As for "our" this is what I found:

"An our declares the listed variables to be valid globals within the
enclosing block, file, or eval. That is, it has the same scoping rules
as a ``my'' declaration, but does not create a local variable. If more
than one value is listed, the list must be placed in parentheses. The
our declaration has no semantic effect unless ``use strict vars'' is in
effect, in which case it lets you use the declared global variable
without qualifying it with a package name."

http://aspn.activestate.com/ASPN/docs/ActivePerl/lib/Pod/perlfunc.html#i
tem_our

Cool, looks like I learned as much from your post as you did. Thanks
-Nick
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,733
Messages
2,569,439
Members
44,829
Latest member
PIXThurman

Latest Threads

Top