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
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