Perl Scope problem

From: Randall Perry <rgp(at)systame(dot)com>
To: <pgsql-general(at)postgresql(dot)org>, <macperl(at)perl(dot)org>
Subject: Perl Scope problem
Date: 2001-05-03 03:27:28
Message-ID: B716485F.ABBE%rgp@systame.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-general

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.

Can someone explain why this happens and how to make it work right?

use strict;
$cust_data = {};

while ($condition) {
...

$cust_data = get_cust_data();

if ($condition2) {
if (send_mail($cust_data)) {
print $cust_data->{'customer'};
...

}
else {

if (send_mail($cust_data)) {
print $cust_data->{'customer'};
...

}
}

--
Randy Perry
sysTame
Mac Consulting/Sales

Responses

Browse pgsql-general by date

  From Date Subject
Next Message hciR nellA 2001-05-03 03:55:13 Re: [MacPerl] Perl Scope problem
Previous Message Diana Cionoiu 2001-05-03 03:20:24 Re: Multiple INSERT