Re: Perl Scope problem

From: will trillich <will(at)serensoft(dot)com>
To: pgsql-general(at)postgresql(dot)org
Subject: Re: Perl Scope problem
Date: 2001-05-03 16:12:22
Message-ID: 20010503111222.B2034@serensoft.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: 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(at)serensoft(dot)com
http://sourceforge.net/projects/newbiedoc -- we need your brain!
http://www.dontUthink.com/ -- your brain needs us!

In response to

Responses

Browse pgsql-general by date

  From Date Subject
Next Message Gyozo Papp 2001-05-03 16:19:10 Re: [PHP] psql with PHP question
Previous Message Joel Burton 2001-05-03 16:06:17 ODBC 3.0 functions (UCASE, LCASE, etc.)