Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Perl > Perl Misc > Understanding 'scope'

Reply
Thread Tools

Understanding 'scope'

 
 
Prabh
Guest
Posts: n/a
 
      02-15-2004
Hello all,
Is it possible to access a variable with local scope in a subroutine
from outside of the subroutine?

e.g.,

#usr/local/bin/perl

use strict ;
use warnings ;

sub testScope
{
my $var = 10 ;
}

&testScope ;
print "$var\n" ;

==========================================

It doesnt compile with 'use strict', after removing strict, I get
blank from the print.

Is it possible to retrieve $var in some 'testScope::$var' fashion?
I realize I could add "return $var;" to the subroutine and access it
from the out, but was just curious if its possible to access
variables.

If I dont use "my" or "strict" I can.
If its a frowned-upon practice, then why does Perl allow it in the
first place?

Thanks for your time,
Prab
 
Reply With Quote
 
 
 
 
Ben Morrow
Guest
Posts: n/a
 
      02-15-2004

(Prabh) wrote:
> Is it possible to access a variable with local scope in a subroutine
> from outside of the subroutine?


No. That is the meaning of 'scope'.

> #usr/local/bin/perl
>
> use strict ;
> use warnings ;
>
> sub testScope
> {
> my $var = 10 ;
> }
>
> &testScope ;


Don't call subs with &:

testScope;
or
testScope();

> print "$var\n" ;
>
> ==========================================
>
> It doesnt compile with 'use strict', after removing strict, I get
> blank from the print.


What is happening there is that you are accessing the global variable
$main::var, which currently is set to undef. Try

no strict 'vars';

$main::var = "hello";

sub test_scope {
my $var = "world";
}

print "$var\n";

> Is it possible to retrieve $var in some 'testScope::$var' fashion?
> I realize I could add "return $var;" to the subroutine and access it
> from the out, but was just curious if its possible to access
> variables.


Nope. That's the point: as you can't get at $var from outside
testScope, nothing weird can happen to its value elsewhere in the
program without you realising.

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

Ben

--
If you put all the prophets, | You'd have so much more reason
Mystics and saints | Than ever was born
In one room together, | Out of all of the conflicts of time.
|----------------+---------------| The Levellers, 'Believers'
 
Reply With Quote
 
 
 
 
David K. Wall
Guest
Posts: n/a
 
      02-15-2004
(Prabh) wrote:

> Is it possible to access a variable with local scope in a subroutine
> from outside of the subroutine?


Ben Morrow already answered this, so I'll pick up a crumb he left behind...

> Is it possible to retrieve $var in some 'testScope::$var' fashion?
> I realize I could add "return $var;" to the subroutine and access it
> from the out, but was just curious if its possible to access
> variables.
>
> If I dont use "my" or "strict" I can.
> If its a frowned-upon practice, then why does Perl allow it in the
> first place?


Because IMHO Larry and the other authors of Perl don't like arbitrary
restrictions on what a programmer can do. Package variables can be useful
in one-liners: programs typed in at the command line, used once and never
saved. Or a programmer might choose to have global variables just to make
life easier in some way. Or whatever.

Somewhat more facetiously: Perl gives you the freedom to program however
you like. It's up to you to choose between Good and Evil.

--
David Wall
 
Reply With Quote
 
Tad McClellan
Guest
Posts: n/a
 
      02-15-2004
Prabh <> wrote:

> Is it possible to access a variable with local scope in a subroutine
> from outside of the subroutine?



No. (and that is a Good Thing)


> #usr/local/bin/perl

^^

Do not re-type Perl code
Use copy/paste or your editor's "import" function rather than
attempting to type in your code. If you make a typo you will get
followups about your typos instead of about the question you are
trying to get answered.


Have you seen the Posting Guidelines that are posted here frequently?


> use strict ;
> use warnings ;
>
> sub testScope
> {
> my $var = 10 ;
> }
>
> &testScope ;
> print "$var\n" ;
>
>==========================================
>
> It doesnt compile with 'use strict', after removing strict, I get
> blank from the print.



But you also get a warning too, right?

What did it say?


> Is it possible to retrieve $var in some 'testScope::$var' fashion?



Yes and no.


Yes, if you make $var a package variable rather than a lexical variable:

our $var = 10;

(but that might be a Bad Thing, so I hope you don't _want_ that)

(and the "fashion" would have the dollar sign first: $main::var )


No, because that is what you asked for when you decided to my()
the variable. (and that is a Good Thing)


> If its a frowned-upon practice, then why does Perl allow it in the
> first place?



"frowned-upon" does not mean "absolutely never", so Perl allows it
to account for those rare cases.


Global variables are bad (in general). By turning on "use strict",
perl was able to tell you that you were using a global variable.

That is a Very Very Good Thing. It caught your mistake for you.


--
Tad McClellan SGML consulting
Perl programming
Fort Worth, Texas
 
Reply With Quote
 
Uri Guttman
Guest
Posts: n/a
 
      02-15-2004
>>>>> "DKW" == David K Wall <> writes:

DKW> Because IMHO Larry and the other authors of Perl don't like
DKW> arbitrary restrictions on what a programmer can do. Package
DKW> variables can be useful in one-liners: programs typed in at the
DKW> command line, used once and never saved. Or a programmer might
DKW> choose to have global variables just to make life easier in some
DKW> way. Or whatever.

package globals have many important uses. they are just not the ones
newbies typically use them for. stuff that needs to be exported is one
common use. or setting flags inside a module that doesn't want to have
class methods for that.

DKW> Somewhat more facetiously: Perl gives you the freedom to program however
DKW> you like. It's up to you to choose between Good and Evil.

perl supporting package globals is needed. using them is not always
needed. is that helpful?

uri

--
Uri Guttman ------ -------- http://www.stemsystems.com
--Perl Consulting, Stem Development, Systems Architecture, Design and Coding-
Search or Offer Perl Jobs ---------------------------- http://jobs.perl.org
 
Reply With Quote
 
thumb_42@yahoo.com
Guest
Posts: n/a
 
      02-15-2004
Prabh <> wrote:
> Hello all,
> Is it possible to access a variable with local scope in a subroutine
> from outside of the subroutine?


Yes. Old perl code uses local variables, strict won't like it, and you'll
probably get warnings.

###########################################
$wacky = "Package global";
print "wacky == $wacky\n";
&top_level();
print "wacky == $wacky\n";

sub top_level {
local($wacky); # Variable is now "local" that is, the previous
# data is "pushed" on a stack, during the duration
# of this scope, $wacky will contain the value:
# "Don't do this it's bad"
#
# You'll be able to access $wacky from any sub called
# from here, and on through subs called by those subs, but
# not from the callers scope.
#
# Lots of people will point out that the use of local()
# is generally considered a very bad idea, you can safely
# ignore these people. they are probably the kind of
# people who don't enjoy the taste of licking very cold
# steel either

$wacky = "Don't do this it's bad";
&some_sub();
}

sub some_sub {
print "Value of wacky: $wacky\n";
}

>
> e.g.,
>
> #usr/local/bin/perl
>
> use strict ;
> use warnings ;
>
> sub testScope
> {
> my $var = 10 ;
> }
>
> &testScope ;
> print "$var\n" ;
>
> ==========================================
>
> It doesnt compile with 'use strict', after removing strict, I get
> blank from the print.
>
> Is it possible to retrieve $var in some 'testScope::$var' fashion?
> I realize I could add "return $var;" to the subroutine and access it
> from the out, but was just curious if its possible to access
> variables.
>
> If I dont use "my" or "strict" I can.
> If its a frowned-upon practice, then why does Perl allow it in the
> first place?
>
> Thanks for your time,
> Prab

 
Reply With Quote
 
Anno Siegel
Guest
Posts: n/a
 
      02-15-2004
Uri Guttman <> wrote in comp.lang.perl.misc:
> >>>>> "DKW" == David K Wall <> writes:


> DKW> Somewhat more facetiously: Perl gives you the freedom to program however
> DKW> you like. It's up to you to choose between Good and Evil.
>
> perl supporting package globals is needed. using them is not always
> needed. is that helpful?


Add to that:

Early in Perl 6 development there were (of course) quite a few requests
to make the default variable type lexical. Larry's reply: "Oh no".

Anno
 
Reply With Quote
 
robic0
Guest
Posts: n/a
 
      11-29-2005
On 14 Feb 2004 17:53:37 -0800, (Prabh) wrote:

>Hello all,
>Is it possible to access a variable with local scope in a subroutine
>from outside of the subroutine?
>

Why yes, yes it is. As long as that subroutine accesses the globally
named variable and was called withing the scope of the local variable
declaration, and it was a global.

 
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
Understanding your BIOS TYCOON Hardware 6 06-29-2005 09:54 AM
Confirm my wireless understanding please? Evil Uncle Chris Wireless Networking 1 05-01-2005 03:19 PM
Understanding voice AIMs Ghazan Haider Cisco 1 11-28-2004 03:15 PM
Re: understanding an error Alvin Andries VHDL 0 09-12-2003 11:38 AM
Why does Microsoft have such a hard time understanding what they say? George Hester ASP .Net 3 08-11-2003 09:16 PM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57