Understanding 'scope'

P

Prabh

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
 
B

Ben Morrow

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
 
D

David K. Wall

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. :)
 
T

Tad McClellan

Prabh said:
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.
 
U

Uri Guttman

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
 
T

thumb_42

Prabh said:
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";
}
 
A

Anno Siegel

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
 
R

robic0

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.
 

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,769
Messages
2,569,579
Members
45,053
Latest member
BrodieSola

Latest Threads

Top