Understanding 'scope'

Discussion in 'Perl Misc' started by Prabh, Feb 15, 2004.

  1. Prabh

    Prabh Guest

    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
     
    Prabh, Feb 15, 2004
    #1
    1. Advertising

  2. Prabh

    Ben Morrow Guest

    (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'
     
    Ben Morrow, Feb 15, 2004
    #2
    1. Advertising

  3. (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
     
    David K. Wall, Feb 15, 2004
    #3
  4. 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
     
    Tad McClellan, Feb 15, 2004
    #4
  5. Prabh

    Uri Guttman Guest

    >>>>> "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
     
    Uri Guttman, Feb 15, 2004
    #5
  6. Prabh

    Guest

    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
     
    , Feb 15, 2004
    #6
  7. Prabh

    Anno Siegel Guest

    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
     
    Anno Siegel, Feb 15, 2004
    #7
  8. Prabh

    robic0 Guest

    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.
     
    robic0, Nov 29, 2005
    #8
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Paul Opal
    Replies:
    12
    Views:
    976
    Paul Opal
    Oct 11, 2004
  2. Pyenos
    Replies:
    9
    Views:
    297
    WaterWalk
    Dec 29, 2006
  3. Alex

    understanding list scope

    Alex, Sep 21, 2008, in forum: Python
    Replies:
    5
    Views:
    277
  4. Roger

    Understanding Scope with 'my'

    Roger, May 17, 2004, in forum: Perl Misc
    Replies:
    2
    Views:
    116
    Roger
    May 18, 2004
  5. Andrew Falanga
    Replies:
    2
    Views:
    205
    Andrew Falanga
    Nov 22, 2008
Loading...

Share This Page