Local Scope Variables

Ask a Question related to PERL Beginners, Design and Development.

  1. #1

    Default Local Scope Variables

    Hi,

    I'm having a bit of trouble with using some local scope variables. I
    have a function where I want to define a variable, then use that
    variable in a sub function. That sounds like the text book definition
    for when to use a local scope variable. Below is the test script that
    I'm using.

    I'm using the 'use strict' declaration as all the books I've read
    (O'Reilly's) say that's a Good Thing to do.

    Can someone offer some insight as to why when I declare $a it's ok, but
    declaring $aa give me an error?

    I'm using Active State's Perl v5.8.3 build 809 for Windows.

    Thanks for any help
    - Josh
    [email]jgage@kyocera-wireless.com[/email]

    --------------------------------------------------------------------------------------------
    use strict;

    test1();

    sub test1
    {
    local $a = 1;
    local $aa = 2;
    test2();
    }

    sub test2
    {
    print "a = $a\n";
    print "aa = $aa\n";
    }

    # Global symbol "$aa" requires explicit package name at
    C:\projects\Tracing\test.pl line 8.
    # Global symbol "$aa" requires explicit package name at
    C:\projects\Tracing\test.pl line 15.
    # Execution of C:\projects\Tracing\test.pl aborted due to compilation
    errors.
    Joshua A. Gage Guest

  2. Similar Questions and Discussions

    1. Application Scope variables ?
      I am migrating to PHP from ASP where there are the Application Scope variables which are accessible from any page on a website and which are used, in...
    2. #39833 [NEW]: Session variables overwritten by local variables (register_globals=off)
      From: sup1382 at accedo dot es Operating system: OpenBSD 3.9 PHP version: 5.2.0 PHP Bug Type: Session related Bug...
    3. Global Variables and scope
      Ok, I'm creating a fairly large RIA using many components set on a page. They all share a common list. It is a Project list that is displayed in a...
    4. Local Variable Scope in Ruby2
      Hi, I was going through matz's slides from the Ruby Conference 2003, and was checking out what the slides claimed was the most regrettable behaviour...
    5. Enumerate Local Variable Scope
      Does anyone know of a way to enumerate the local variable scope? The reason I ask is that I have a function that dumps the contents of...
  3. #2

    Default Re: Local Scope Variables

    Joshua A. Gage wrote:
    > Hi,
    >
    > I'm having a bit of trouble with using some local scope variables. I
    > have a function where I want to define a variable, then use that
    > variable in a sub function. That sounds like the text book definition
    > for when to use a local scope variable. Below is the test script that
    > I'm using.
    >
    > I'm using the 'use strict' declaration as all the books I've read
    > (O'Reilly's) say that's a Good Thing to do.
    >
    > Can someone offer some insight as to why when I declare $a it's ok, but
    > declaring $aa give me an error?
    >
    > I'm using Active State's Perl v5.8.3 build 809 for Windows.
    >
    > Thanks for any help
    > - Josh
    > [email]jgage@kyocera-wireless.com[/email]
    >
    > --------------------------------------------------------------------------------------------
    >
    > use strict;
    >
    > test1();
    >
    > sub test1
    > {
    > local $a = 1;
    > local $aa = 2;
    > test2();
    > }
    >
    > sub test2
    > {
    > print "a = $a\n";
    > print "aa = $aa\n";
    > }
    >
    > # Global symbol "$aa" requires explicit package name at
    > C:\projects\Tracing\test.pl line 8.
    > # Global symbol "$aa" requires explicit package name at
    > C:\projects\Tracing\test.pl line 15.
    > # Execution of C:\projects\Tracing\test.pl aborted due to compilation
    > errors.

    hello,

    use my
    extract from perldoc -f my :
    A "my" declares the listed variables to be local
    (lexically) to the enclosing block, file, or
    "eval".

    cheers

    Eternius Guest

  4. #3

    Default Re: Local Scope Variables

    On 02/12/04 18:19, Joshua A. Gage wrote:
    > Hi,
    >
    > I'm having a bit of trouble with using some local scope variables. I
    > have a function where I want to define a variable, then use that
    > variable in a sub function. That sounds like the text book definition
    > for when to use a local scope variable. Below is the test script that
    > I'm using.
    >
    > I'm using the 'use strict' declaration as all the books I've read
    > (O'Reilly's) say that's a Good Thing to do.
    >
    > Can someone offer some insight as to why when I declare $a it's ok, but
    > declaring $aa give me an error?
    >
    > I'm using Active State's Perl v5.8.3 build 809 for Windows.
    >
    > Thanks for any help
    > - Josh
    > [email]jgage@kyocera-wireless.com[/email]
    >
    > --------------------------------------------------------------------------------------------
    >
    > use strict;
    >
    > test1();
    >
    > sub test1
    > {
    > local $a = 1;
    > local $aa = 2;
    > test2();
    > }
    >
    > sub test2
    > {
    > print "a = $a\n";
    > print "aa = $aa\n";
    > }
    >
    > # Global symbol "$aa" requires explicit package name at
    > C:\projects\Tracing\test.pl line 8.
    > # Global symbol "$aa" requires explicit package name at
    > C:\projects\Tracing\test.pl line 15.
    > # Execution of C:\projects\Tracing\test.pl aborted due to compilation
    > errors.
    >
    'local' does not create a variable; the variable must already exist. In
    order to create a package variable you must use 'our' or 'use vars':

    our $a;
    our $aa;

    -or-

    our ($a, $aa);

    -or-

    use vars qw($a $aa);

    The above are all equivelant, and they all create to package variables
    named $a and $aa;

    The reason you get no error for $a is because there is already a package
    variable with that name: the variables $a and $b exist automatically in
    every package for use in the 'sort' function.

    Regards,
    Randy.
    Randy W. Sims Guest

  5. #4

    Default Re: Local Scope Variables

    Eternius wrote:
    > Joshua A. Gage wrote:
    >
    >> Hi,
    >>
    >> I'm having a bit of trouble with using some local scope variables. I
    >> have a function where I want to define a variable, then use that
    >> variable in a sub function. That sounds like the text book definition
    >> for when to use a local scope variable. Below is the test script that
    >> I'm using.
    >>
    >> I'm using the 'use strict' declaration as all the books I've read
    >> (O'Reilly's) say that's a Good Thing to do.
    >>
    >> Can someone offer some insight as to why when I declare $a it's ok,
    >> but declaring $aa give me an error?
    >>
    >> I'm using Active State's Perl v5.8.3 build 809 for Windows.
    >>
    >> Thanks for any help
    >> - Josh
    >> [email]jgage@kyocera-wireless.com[/email]
    >>
    >> --------------------------------------------------------------------------------------------
    >>
    >> use strict;
    >>
    >> test1();
    >>
    >> sub test1
    >> {
    >> local $a = 1;
    >> local $aa = 2;
    >> test2();
    >> }
    >>
    >> sub test2
    >> {
    >> print "a = $a\n";
    >> print "aa = $aa\n";
    >> }
    >>
    >> # Global symbol "$aa" requires explicit package name at
    >> C:\projects\Tracing\test.pl line 8.
    >> # Global symbol "$aa" requires explicit package name at
    >> C:\projects\Tracing\test.pl line 15.
    >> # Execution of C:\projects\Tracing\test.pl aborted due to compilation
    >> errors.
    >
    >
    >
    > hello,
    >
    > use my
    > extract from perldoc -f my :
    > A "my" declares the listed variables to be local
    > (lexically) to the enclosing block, file, or
    > "eval".
    >
    In most cases you would be right, but in this case it appears that the
    OP understands what 'local' does, as he is using it inside of an
    embedded function. Do I like that, no, but it is a "correct" usage of
    'local'. Your suggestion of using 'my' changes how the variables must
    be used so is not equivalent without suggesting the interface to 'test2'
    changes...

    sub test1 {
    my $aa = 1;
    my $aaa = 2;
    test2($aa, $aaa);
    }

    sub test2 {
    my ($bb, $bbb) = @_;
    print "aa = $bb\n";
    print "aaa = $bbb\n";
    }

    Is the above better, in the court of readability and encapsulation, yes,
    in the court of elegance, maybe not...

    [url]http://danconia.org[/url]
    Wiggins D'Anconia Guest

  6. #5

    Default Re: Local Scope Variables


    Wiggins d'Anconia wrote:
    >sub test1 {
    > my $aa = 1;
    > my $aaa = 2;
    > test2($aa, $aaa);
    >}
    >
    >sub test2 {
    > my ($bb, $bbb) = @_;
    > print "aa = $bb\n";
    > print "aaa = $bbb\n";
    >}
    >
    >Is the above better, in the court of readability and encapsulation,
    >yes, in the court of elegance, maybe not...
    Elegance in programming should be defined in terms of readability, at leastpartly. Very dense ways of coding a certain goal are smart (and I admit that I like to compress my code as much as possible), but not necessarily very elegant. ;)

    - Jan
    --
    There's no place like ~/
    Jan Eden Guest

Posting Permissions

  • You may not post new threads
  • You may post replies
  • You may not post attachments
  • You may not edit your posts

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 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139