PEPPLER.ORG
Michael Peppler
Sybase Consulting
Menu
Home
Sybase on Linux
Install Guide for Sybase on Linux
General Sybase Resources
General Perl Resources
Freeware
Sybperl
Sybase::Simple
DBD::Sybase
BCP Tool
Bug Tracker
Mailing List Archive
Downloads Directory
FAQs
Sybase on Linux FAQ
Sybperl FAQ
Personal
Michael Peppler's resume

sybperl-l Archive

Up    Prev    Next    

From: Brad_Ziegler at notes dot mdor dot state dot mn dot us
Subject: return values
Date: Mar 25 1999 2:55PM


Hello everyone, I'm new to Perl and I have a question about return values from a subroutine.

Environment:  using ActiveState Perl build 509 on Windows 95, Sybperl 2.10, Sybase Open Client 11.1.1, and Sybase SQL
Server 11.0.3 on AIX.  I get the same results when running on AIX too.

I used Michael Peppler's SybTools Perl Module from his article in the ISUG Journal (downloaded it from his web site).  I
was able to get Scalar to work, but not HashRow.  After many iterations of trial and error, I finally modified SybTools
to verify that the hash being passed back, %data, was a valid hash, and that I could assign a reference to it.  The
results were confusing.  The hash reference was fine before returning to the test program, but when it got back to the
test program, it was no longer a valid hash reference.  When I placed a "return" before {%data};, the subroutine passed
the hash reference back correctly to the test program.  See below for the listings.  According to Learning Perl, "The
return value of a subroutine is the value of the return statement or of the last expression evaluated in the
subroutine."  The question is:  What was I doing wrong on the return value?

SybTools' base code:

sub HashRow {
    my $self = shift;
    my $sql  = shift;

    my $restype;
    my %data;
    $self->ct_execute($sql) == CS_SUCCEED || return undef;
    while($self->ct_results($restype) == CS_SUCCEED) {
     next unless $self->ct_fetchable($restype);
     # fetch one row as a hash
     %data = $self->ct_fetch(CS_TRUE);
     # we're only interested in the first row of the first result set
     $self->ct_cancel(CS_CANCEL_ALL);
    }
    {%data};
}


The code after I put the displays in:

sub HashRow {
    my $self = shift;
    my $sql  = shift;

    my $restype;
    my %data;
my $dummyref;
    $self->ct_execute($sql) == CS_SUCCEED || return undef;
    while($self->ct_results($restype) == CS_SUCCEED) {
     next unless $self->ct_fetchable($restype);
     # fetch one row as a hash
     %data = $self->ct_fetch(CS_TRUE);
     # we're only interested in the first row of the first result set
     $self->ct_cancel(CS_CANCEL_ALL);
    }
$dummyref = {%data};
print "\$dummyref=$dummyref\n";
print "@{[ %data ]}\n";
    return {%data};
}


The test program:
#!\Perl\bin -w
use Getopt::Std;
use SybTools;
my $abortOnError = 1;
$dbh = new SybTools "user", "pw", "Server";
$dbh->ExecSql("use master",$abortOnError);
print "\nUsing HashRow \n";
$data = $dbh->HashRow("select name, spid from sysprocesses sysp, syslogins sysl where sysp.suid = sysl.suid and name =
'tester'");
if (ref($data) ne 'HASH') { print "\$data is not a valid hash reference\n" }
print "\$data=$data\n";
$user = $data->{name};
$spid = $data->{spid};
print "The spid for $user is $spid.\n";


Test program results before the modifications to HashRow:

C:\perlscripts>perl test.pl

Using HashRow
$dummyref=HASH(0x7a4d14)
spid 1 name tester
$data is not a valid hash reference
$data=2/8
The spid for  is .



Test program results after the modifications to HashRow:

C:\perlscripts>perl test.pl

Using HashRow
$dummyref=HASH(0x7a496c)
spid 1 name tester
$data=HASH(0x7a4990)
The spid for tester is 1.


Thanks for any help.
Brad