scope question

Discussion in 'Perl Misc' started by Richard Trahan, Jul 4, 2005.

  1. Consider the following code:

    use strict;
    my $x = 5;
    our $sr = sub { print "$x\n"; };

    This code gives a scope error, but if I remove 'use strict',
    it works ok.

    The 'our $sr' statement should make an entry in the SCALAR
    slot of *main::sr, right? And the &$sr should retrieve only
    that value, right?

    What am I missing here?

    Richard Trahan, Jul 4, 2005
    1. Advertisements

  2. Right, the value is there.

    You are having trouble _accessing_ that value though.

    All that "our" gets you is to make "use strict" shut up about using
    short names rather than long names.

    The "permission to use short names" is scoped, once outside that
    scope, you must use the fully qualified package name again.



    or, if you want to use the short name after the block, ask for that
    with an

    our $sr;

    after the block.
    Tad McClellan, Jul 4, 2005
    1. Advertisements

  3. Richard Trahan

    Joe Smith Guest

    With 'our', the value is global but the permission to access
    it is scoped. It would work if you replace the last line with

    our $sr;

    Joe Smith, Jul 4, 2005
    1. Advertisements

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 (here). After that, you can post your question and our members will help you out.