[Israel.pm] Help: why does it warn me?

Gaal Yahas gaal at forum2.org
Wed Feb 4 07:20:22 PST 2009


In general, you should avoid using "local".

Local variables are actually global :-) but the *values* you give them
are local to the dynamic scope. This means two things.

1. If you're running under 'strict', you still have to declare them as
globals with "use vars" or "our". Or you can refer to them with their
fully-qualified name, e.g. $main::foo or $some_pkg::foo.

2. Anyone can modify them (they're global), but once you leave the
scope of "local", they revert to their old value.

Here's an illustration:

use strict;
package Moo;

our $global = 1;

sub f {
  print $global;       # 1
  {
    local $global = 2;  # (or: local $Moo::global = 1, which even
works without strict)
    g();
    print $global;     # 3
  }
  print $global;       # 1 again
}

sub g {
  $Moo::global = 3;
}

f();

I'll not explain "our" in this email, I can recommend you look online
for "Coping with Scoping", an excellent read.

Hope this helps,
Gaal

On Wed, Feb 4, 2009 at 4:58 PM, Chanan Berler <chananb at centerity.com> wrote:
> Hi All,
>
> I wrote this script:
>
>
> sub update_event_log
> {
>    my ($lv_reg) = @_;
>
>    local $lv_attemp_stmt;
>    local $lv_service_stmt;
>    local $lv_host_stmt;
>    local $lv_hostgroup_stmt;
>    local $lv_servicegroup_stmt;
>
>    &_el_parse_reg_($lv_reg);
>
> }
>
> The idea that sub el_parse_reg will update these fields.
> I  tired doing something like that as a test (but without warnings) and it
> worked.
> But when I tried to imply use warnings and use strict I get this kind of
> warning / error message:
>
> Global symbol "$lv_host_stmt" requires explicit package name at event_log.pl
> line 162.
>
> Why is that?
> Thanks
> Chanan
>
> PS: correct me if I am wrong
>  But as what I understood:
>
> my - applies to sub private variables
> local - protected scalars that can be updated by other sub called by the sub
> which created the local variables
> our - more like a global like variables - which can be accessed from all the
> module / script found on that file
>
>
> _______________________________________________
> Perl mailing list
> Perl at perl.org.il
> http://perl.org.il/mailman/listinfo/perl
>



-- 
Gaal Yahas <gaal at forum2.org>
http://gaal.livejournal.com/



More information about the Perl mailing list