Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Perl > Perl Misc > [newbie] scope of the variables

Reply
Thread Tools

[newbie] scope of the variables

 
 
John
Guest
Posts: n/a
 
      09-30-2003
Hi all,


I am trying to create a module. and I have a question about the scope
of the variables.



lets say,

use strict;


sub one {

my ($rvalue) = @_; # here I define a variable

for my $i (0..$#$rvalue){ # here I define a local
# variable $i and I use it

# do something
}

}


sub second {

my ($rvalue) = @_; # on this second sub $rvalue
# is a new one or I carry it
# as the first one


for my $i (0..$#$rvalue){ # now the same case, $i was
# already defined and if the
# scope is just its
# own subsroutine, now this
# $i is something copletely
# new. Am I right?

# do something else
}
}


# Thanks for your help
# John
 
Reply With Quote
 
 
 
 
Tad McClellan
Guest
Posts: n/a
 
      09-30-2003
John <(E-Mail Removed)> wrote:

> I am trying to create a module. and I have a question about the scope
> of the variables.



None of the variables in your module code below will be visible
in the "use"ing program, they are all "lexical variables".

Learn all about Perl's two separate systems of variables at:

"Coping with Scoping":

http://perl.plover.com/FAQs/Namespaces.html



> for my $i (0..$#$rvalue){ # here I define a local

^^^^^
> # variable $i and I use it

^^^^^^^^

(I can't say "for" for "foreach". I reserve "for" for for(;


In Perl, local() has to do with _package_ variables, which are
a form of _global_ variable, so it is confusing to use "local"
to mean "local". or should it be ?

You have defined a _lexical_ variable.

Lexical variables (my) always get you a completely new variable,
new memory allocated and all.

So, yes, $i will be visible only within the body of the foreach() block.

When the (foreach) block exits, $i will cease to exist.


> for my $i (0..$#$rvalue){ # now the same case, $i was
> # already defined and if the
> # scope is just its
> # own subsroutine, now this
> # $i is something copletely
> # new. Am I right?



You are right.


--
Tad McClellan SGML consulting
http://www.velocityreviews.com/forums/(E-Mail Removed) Perl programming
Fort Worth, Texas
 
Reply With Quote
 
 
 
Reply

Thread Tools

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 Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Re: Lexical scope vs. dynamic scope Xah Lee Java 0 02-26-2009 10:08 AM
newbie question about scope, variables, declarations of variables and option strict (as in perl) Talha Oktay Ruby 8 03-08-2006 04:11 PM
Scope - do I need two identical classes, each with different scope? ann Java 13 09-13-2005 03:07 AM
How do namespace scope and class scope differ? Steven T. Hatton C++ 9 07-19-2005 06:07 PM
IMPORT STATIC; Why is "import static" file scope? Why not class scope? Paul Opal Java 12 10-10-2004 11:01 PM



Advertisments