Question on "defined" function

K

kemton

I am getting unexpected results while checking whether the value of a
reference is defined. Using the "defined" check, or a couple of other
checks, the value I'm checking seems to get defined. Can anybody shed
some light on this for me please? I am expected to get 1 row out of
the database, which I do, but after the "define" check, the define
seems to define the value I'm checking, and the count of the reference
becomes 2.

Here's the code. It uses DBI to grab the data out of my database, but
I've put in several actual checks. The results are posted below the
code:
----------------------------------------------------
#!/usr/bin/perl

use DBI;

connect_db();

$statement = "SELECT id,hostname,reserver,res_email,start_date,";
$statement .= "end_date,status FROM reserve WHERE ";
$statement .= "hostname = 'kern8' AND reserver = 'kemton' ";
$statement .= "AND end_date >= '2006-02-23' AND status = 'Active'";
$statement .= "ORDER BY end_date";
$reservations = $dbh->selectall_arrayref($statement);

$num_reservations = @$reservations;
print "The number of reservations now: $num_reservations\n";
if ( !(defined $reservations->[1]->[0]) ) {
$num_reservations2 = @$reservations;
print "The number of reservations later: $num_reservations2\n";
}

$statement = "SELECT id,hostname,reserver,res_email,start_date,";
$statement .= "end_date,status FROM reserve WHERE ";
$statement .= "hostname = 'kern8' AND reserver = 'kemton' ";
$statement .= "AND end_date >= '2006-02-23' AND status = 'Active'";
$statement .= "ORDER BY end_date";
$reservations = $dbh->selectall_arrayref($statement);

$num_reservations = @$reservations;
print "The number of reservations now: $num_reservations\n";
if ( !($reservations->[1]->[0]) ) {
$num_reservations2 = @$reservations;
print "The number of reservations later: $num_reservations2\n";
}

$statement = "SELECT id,hostname,reserver,res_email,start_date,";
$statement .= "end_date,status FROM reserve WHERE ";
$statement .= "hostname = 'kern8' AND reserver = 'kemton' ";
$statement .= "AND end_date >= '2006-02-23' AND status = 'Active'";
$statement .= "ORDER BY end_date";
$reservations = $dbh->selectall_arrayref($statement);

$num_reservations = @$reservations;
print "The number of reservations now: $num_reservations\n";
if ( $reservations->[1]->[0] eq "" ) {
$num_reservations2 = @$reservations;
print "The number of reservations later: $num_reservations2\n";
}

$statement = "SELECT id,hostname,reserver,res_email,start_date,";
$statement .= "end_date,status FROM reserve WHERE ";
$statement .= "hostname = 'kern8' AND reserver = 'kemton' ";
$statement .= "AND end_date >= '2006-02-23' AND status = 'Active'";
$statement .= "ORDER BY end_date";
$reservations = $dbh->selectall_arrayref($statement);

$num_reservations = @$reservations;
print "The number of reservations now: $num_reservations\n";
if ( $num_reservations == 1 ) {
$num_reservations2 = @$reservations;
print "The number of reservations later: $num_reservations2\n";
}


sub connect_db() {
$dbh = DBI->connect("DBI:mysql:database=monitor;host=monitor",
"userid","password",{'RaiseError' => 1});
$dbh->{RaiseError} = 1;
}
------------------------------------------------

And the results are:
$ ./testrs.pl
The number of reservations now: 1
The number of reservations later: 2
The number of reservations now: 1
The number of reservations later: 2
The number of reservations now: 1
The number of reservations later: 2
The number of reservations now: 1
The number of reservations later: 1
 
X

xhoster

I am getting unexpected results while checking whether the value of a
reference is defined. Using the "defined" check, or a couple of other
checks, the value I'm checking seems to get defined. Can anybody shed
some light on this for me please?

It is not the value you are checking which gets defined, it is the value
just before (in the dereferencing chain) the one you are checking which
became defined.

It is called autovivification. You can read all about it on the web.
I am expected to get 1 row out of
the database, which I do, but after the "define" check, the define
seems to define the value I'm checking, and the count of the reference
becomes 2.

So don't do that. Perl told you via $num_reservations that you only got
one value back. Apparently, you didn't believe it. If you go around
sticking your fingers in the wounds, you have to expect to get blood on
your hands.

Xho
 
J

John W. Krahn

I am getting unexpected results while checking whether the value of a
reference is defined. Using the "defined" check, or a couple of other
checks, the value I'm checking seems to get defined. Can anybody shed
some light on this for me please?

What you are seeing is called autovivification. It means that in order to
test the value in $reservations->[1]->[0] Perl automatically creates
$reservations->[1]


John
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,995
Messages
2,570,230
Members
46,819
Latest member
masterdaster

Latest Threads

Top