not sure why I am getting the following warning "Useless use of aconstant in void context"

M

mcardeiro

Hello

I have a line of code that is creating an entry in a mysql table using
DBI. I have the record values for the insert in a hash reference. A
separate array holds information about the table structure so that
when I do my insert I map the hash ref using the array so only
relevant values are entered into the execute statement and they are in
the proper order.

It works exactly as it is supposed to but I keep getting a warning
from perl "Useless use of a constant in void context at /var/www/web1/
lib/TS.pm line 1258."

I am ignoring it because the code works, but figure I'll throw it out
there in case I am doing something boneheaded.

heres some code fragments:

#######################################################################################
###holds info about the structure of the table and is used to verify
data before entry
########################################################################################
our @userTableOrder = (
{ 'name' => 'username',
'required' => 1,
'regexp' => '^[\w\d]{1,15}$',
},

{ 'name' => 'password',
'required' => 1,
},
{ 'name' => 'authorize',
'required' => 1,
'regexp' => '^[\d]+$',
},
);

############################
### record to be inserted
##########################
my $newUser = {
'username' => 'mcardeiro',
'password' => 'booba',
'authorize' => 3,
'workgroup' => 'philadelphia',
};

############################
### DBI stuff
##########################
$sth = $dbh->prepare("INSERT INTO users VALUES (?,?,?")
|| error_page("cant prepare insert statement at userEntry: $dbh-
errstr");

#############################################################
##### the code below is causing the warning...why?
#########################################################
$sth->execute( map {$newUser->{$_->{'name'}}} @userTableOrder)
|| error_page("cant execute insert statement at userEntry: $dbh-
 
J

John W. Krahn

I have a line of code that is creating an entry in a mysql table using
DBI. I have the record values for the insert in a hash reference. A
separate array holds information about the table structure so that
when I do my insert I map the hash ref using the array so only
relevant values are entered into the execute statement and they are in
the proper order.

It works exactly as it is supposed to but I keep getting a warning
from perl "Useless use of a constant in void context at /var/www/web1/
lib/TS.pm line 1258."

I am ignoring it because the code works, but figure I'll throw it out
there in case I am doing something boneheaded.

heres some code fragments:

############################
### DBI stuff
##########################
$sth = $dbh->prepare("INSERT INTO users VALUES (?,?,?")
|| error_page("cant prepare insert statement at userEntry: $dbh-

The assignment operator '=' has lower precedence than the OR '||'
operator so $sth is assigned the value returned from $dbh->prepare() or
if that is false it is assigned the value returned from error_page().
You need to either enclose the assignment in parentheses:

( $sth = $dbh->prepare() ) || error_page();

or use the lower precedence 'or' operator.

$sth = $dbh->prepare() or error_page();



John
 
B

Ben Morrow

Quoth "[email protected] said:
I have a line of code that is creating an entry in a mysql table using
DBI. I have the record values for the insert in a hash reference. A
separate array holds information about the table structure so that
when I do my insert I map the hash ref using the array so only
relevant values are entered into the execute statement and they are in
the proper order.

It works exactly as it is supposed to but I keep getting a warning
from perl "Useless use of a constant in void context at /var/www/web1/
lib/TS.pm line 1258."

I am ignoring it because the code works, but figure I'll throw it out
there in case I am doing something boneheaded.

Ignoring warnings is a very bad idea. If the warning really is in error,
enclose the statement in question in a block with an appropriate 'no
warnings' statement and a comment as to why the warning is wrong in this
case. This particular warning usually means you have precedence
problems: for instance, my @x = 1, 2; will warn as the 2 isn't actually
assigned to the array.
heres some code fragments:

'Code fragments' is not the same as a minimal example we can all run.
######################################################################
###holds info about the structure of the table and is used to verify
data before entry
#######################################################################

This style of block comment is not helpful. It just makes it impossible
to get any overview of the structure of the code. Posting code with more
than 80 columns is rude.
$sth = $dbh->prepare("INSERT INTO users VALUES (?,?,?") ||
error_page("cant prepare insert statement at userEntry: $dbh-

As John said, this is a precedence error: get into the habit of always
using 'and' and 'or' for flow control, not && and ||.
$sth->execute( map {$newUser->{$_->{'name'}}} @userTableOrder)
|| error_page("cant execute insert statement at userEntry: $dbh-

One of the annoying things about the -> syntax is that it doesn't
interpolate into strings. This needs to be

... or error_page('can't execute insert statement at userEntry:'
. $dbh->errstr);

or the equivalent with sprintf.

This line doesn't give that warning, in a minimal example I just
constructed. Please give us a whole example.

Ben
 
M

mcardeiro

the execute() routine returns a value that you're ignoring.

no I'm not. the "|| error_page()" written after the execute means if
there is no return value from execute run error_page() (but as Ben
pointed out I will not get a very helpful message as I have $dbh-
errstr enclosed in double quotes ).





This style of block comment is not helpful. It just makes it impossible
to get any overview of the structure of the code.
???

Posting code with more
than 80 columns is rude.

sorry. was unaware of this rule.
As John said, this is a precedence error: get into the habit of always
using 'and' and 'or' for flow control, not && and ||.

this is not a problem as error_page() causes the script to exit (I
should have changed it to "die" in my post for clarity)

This line doesn't give that warning, in a minimal example I just
constructed. Please give us a whole example.

thats a bummer, the example I gave was about as complete as I could
make it without pasting in the hundereds of lines of code in the real
script. Thanks for going through the code and trying to help.

Mike Cardeiro
 
J

John W. Krahn

this is not a problem as error_page() causes the script to exit (I
should have changed it to "die" in my post for clarity)

Just because it "works", for some value of "works", does not mean it is
the correct way to do it.


John
 
M

mcardeiro

Just because it "works", for some value of "works", does not mean it is
the correct way to do it.


what exactly is incorrect about saying run a subroutine and assign the
return to a variable or exit?

Mike Cardeiro
 
J

John W. Krahn

what exactly is incorrect about saying run a subroutine and assign the
return to a variable or exit?

I was commenting on the use of '||' instead of 'or' for flow control.
That the subroutine in question uses 'exit()' is just a side effect.
You shouldn't rely on side effects for flow control.


John
 
P

Peter J. Holzer

I was commenting on the use of '||' instead of 'or' for flow control.
That the subroutine in question uses 'exit()' is just a side effect.
You shouldn't rely on side effects for flow control.

You are doing the same thing with or. It just happens at a slightly
different point in the program.

hp
 

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,769
Messages
2,569,581
Members
45,056
Latest member
GlycogenSupporthealth

Latest Threads

Top