Re: Perl Scope problem - Mailing list pgsql-general

From will trillich
Subject Re: Perl Scope problem
Date
Msg-id 20010503111222.B2034@serensoft.com
Whole thread Raw
In response to Re: Perl Scope problem  (Michelle Murrain <mpm@norwottuck.com>)
Responses Re: Perl Scope problem  ("Jeff Duffy" <jeff@alanne.com>)
List pgsql-general
On Thu, May 03, 2001 at 08:39:18AM -0400, Michelle Murrain wrote:
> On Wednesday 02 May 2001 11:27 pm, Randall Perry wrote:
> > I'm baffled by perl's scoping of variables. In the code below, the
> > $cust_data hash ref is inited outside the while loop. It's then set in the
> > while with the results of a PgSQL query.
> >
> > In the if-else statement $cust_data can be seen in the 'if' but not in the
> > 'else' (if I try to print a value in else, $cust_data->{'customer'}, I get
> > an undeclared variable error).
> >
> > I understand that by using 'strict' I can't use any global variables.
>
> No, my understanding is that it forces you to define variables both local and
> global. I've used this statement to declare variables I need throughout a
> script using strict:
>
> use vars qw($val $var $value $conf @var_array @val_array %fields_hash);

"use vars" takes a list of package globals that you'd like
shortcuts for. instead of having to say 'my::package::name::var'
every time you can just say $var instead.

so scoping SHOULD work as you suspect:

    ##########################
    package Original::Package;

    use vars($OMNI);
    # package global var
    $Original::Package::OMNI = &something();
    # same variable here
    $OMNI .= &somethingElse();

    # lexically local to this file:
    my $FileGlobal;

    {
        my $localVar;

        {
            my $eensyVar;
            # all 4 are visible here
            print $eensyVar,$localVar,$FileGlobal,$OMNI;
        }
        # eensyVar is no longer extant
        print $localVar,$FileGlobal,$OMNI;
    }
    # only FileGlobal and OMNI exist here
    print $FileGlobal,$OMNI;

    #############################
    package Something::Else::Entirely;

    # whole 'nother package, but OMNI is till reachable
    print $Original::Package::OMNI;

none of which explains randall's quandary.

--
don't visit this page. it's bad for you. take my expert word for it.
http://www.salon.com/people/col/pagl/2001/03/21/spring/index1.html

will@serensoft.com
http://sourceforge.net/projects/newbiedoc -- we need your brain!
http://www.dontUthink.com/ -- your brain needs us!

pgsql-general by date:

Previous
From: "Albertson, Chris"
Date:
Subject: RE: Database in RAM
Next
From: "Christian Marschalek"
Date:
Subject: RE: Starting the Server at Boot