Title: RE: Perl Bug (again)

 John Deighan wrote, on Fri 17-Feb-06 12:46
: my($nextID,$maxID) = @$L if $L;
:
: The parameter to getCached, $Customer, has a different value than
: during the first call. Because of that, the variable $L is undefined.
: What I expect the statement above to do is to create 2 new variables,
: $nextID and $maxID. Then, if $L has a true value in it, it's treated
: as a reference to a list and the first 2 values in that list would be
: assigned to the variables. However, if $L is false, then the
: variables would be left undefined. What's really blowing my mind is
: the that values in $nextID and $maxID get the values THAT THEY WERE
: ASSIGNED DURING THE PREVIOUS CALL TO THIS FUNCTION. Wow! They're "my"
: variables inside the getCached() function. How could they end up with
: values from a previous invocation of the function?

Fascinating! You have one misconception right off: The "my" line is never executed (not even to make the new variables) if $L is false.

Now I'm starting to speculate: Since your my variables aren't created the second time, the ones from the first time weren't destroyed, and those are the ones picked up (with their old values).

Make the code

my ($nextID, $maxID);
($nextID, $maxID) = @$L if $L;

and see if that doesn't fix it.

Good luck,

Joe

==============================================================
          Joseph P. Discenza, Sr. Programmer/Analyst
               mailto:[EMAIL PROTECTED]

          Carleton Inc.   http://www.carletoninc.com
          574.243.6040 ext. 300    fax: 574.243.6060

Providing Financial Solutions and Compliance for over 30 Years



-----Original Message-----
From: [EMAIL PROTECTED] on behalf ofTo: [email protected]; Lewis Foster; [EMAIL PROTECTED]; Todd Thurheimer; Chris Cain
Subject: Perl Bug (again)

OK, same caveat as before - apparent bugs in Perl are usually user
errors, but once again, I'm stumped. Here is my code. The problem I
have is in the second call to getCached(), specifically, the line:


I've added the output from when I run this below. BTW, I'm running
ActiveState ActivePerl v. 5.8.7 Build 815 under MS Windows 2000 Server.

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

our $hCache = {};

my $id1 = getCached('x');
my $id2 = getCached('y');

print("id1 = '$id1\n");
print("id2 = '$id2\n");

# ---------------------------------------------------

sub getCached { my($Customer) = @_;

my $L = $hCache->{$Customer};
print(defined($L) ? "L defined\n":"L undefined\n");
my($nextID,$maxID) = @$L if $L;
print("nextID = '$nextID', maxID = '$maxID'\n");
if ($nextID==$maxID) {
        ($nextID,$maxID) = getNextMax($Customer);
        $hCache->{$Customer} = [$nextID+1, $maxID];
        }
else {
        ++$hCache->{$Customer}->[0];
        }
return $nextID;
} # getCached()

# ---------------------------------------------------

sub getNextMax { my($CustomerName) = @_;

if ($CustomerName eq 'x') {
        return (100,150);
        }
elsif ($CustomerName eq 'y') {
        return (200,250);
        }
} # getNextMax()


# ---------------------------------------------------
OUTPUT:
# ---------------------------------------------------
C:\Scripts>testIDs.pl
L undefined
nextID = '', maxID = ''
L undefined
nextID = '100', maxID = '150'
id1 = '100
id2 = '100

_______________________________________________
Perl-Win32-Users mailing list
[email protected]
To unsubscribe: http://listserv.ActiveState.com/mailman/mysubs

_______________________________________________
Perl-Win32-Users mailing list
[email protected]
To unsubscribe: http://listserv.ActiveState.com/mailman/mysubs

Reply via email to