simple scope question
when is a global not a global?
or how to get a value from inside a while() to the outside?

two tests-extracted from much large program to show my problem
-------------------- test 1 --------------------------------
my $count;
my $service;

while( ($service) = $SERVICE->fetchrow_array ) { $count++;
   print "count $count service.number <$service>\n";
}
print "service.number <$service>\n";

produces output:
$ ./test.pl
count 1 service.number <229>
Use of uninitialized value at ./test.pl line 26.
service.number <>
note:same results with/without () around $service
----------------- test 2 ---------------------------------
my $count;
my $servicenumber;

while( my ($service) = $SERVICE->fetchrow_array ) {
   $count++;
   print "count $count service.number <$service>\n";
   $servicenumber = $service;
}
print "service.number <$servicenumber>\n";
(note: the "my" on $service was required by strict)

produces desired result:
$ ./test.pl
count 1 service.number <229>
service.number <229>

----------------------------------------------

header of test.pl is
#! /usr/bin/perl -w

use strict;
use DBI;

my $driver  =  DBI->install_driver('mysql') or die;
my $max =  DBI->connect('DBI:mysql:database=max','max','pass') or die;

my $SERVICE = $max->prepare("\
SELECT number
FROM service
WHERE state = 0

----------------------------------------------------
$ perl -v
This is perl, version 5.005_03 built for i386-freebsd

AND d03 = ?") or die;
$SERVICE->execute('0020a6-5a83b4') or die;





--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
<http://learn.perl.org/> <http://learn.perl.org/first-response>


Reply via email to