Interpolation question

W

worldcyclist

Any one have an idea why this first statement works just fine....
-------------------------------------------------------------------
($iso, $name, $size, $location) = $dbh->selectrow_array("SELECT `iso`,
`name`, `size`, `location` FROM `country_lookup` WHERE `name` =
\"France\"");



This works fine and this works fine also....
--------------------------------------------
$country_name = "France";

($iso, $name, $size, $location) = $dbh->selectrow_array("SELECT `iso`,
`name`, `size`, `location` FROM `country_lookup` WHERE `name` =
\"$country_name\"");



Where I am having a problem is if I try this....
--------------------------------------------------------
($country_name) = $dbh->selectrow_array('SELECT `category_label` FROM
`mt_category` WHERE `category_id` = '.$placement_category_id);

($iso, $name, $size, $location) = $dbh->selectrow_array("SELECT `iso`,
`name`, `size`, `location` FROM `country_lookup` WHERE `name` =
\"$country_name\"");

Invariably (no pun intended) I get this error....
Use of uninitialized value in concatenation (.) or string at......

I have used the variable $country_name in later places and it is fine.

Has anyone else experienced this?
Many thansk in advance...
Wilkie
 
J

jl_post

Where I am having a problem is if I try this....
--------------------------------------------------------
($country_name) = $dbh->selectrow_array('SELECT `category_label` FROM
`mt_category` WHERE `category_id` = '.$placement_category_id);

($iso, $name, $size, $location) = $dbh->selectrow_array("SELECT `iso`,
`name`, `size`, `location` FROM `country_lookup` WHERE `name` =
\"$country_name\"");

Invariably (no pun intended) I get this error....
Use of uninitialized value in concatenation (.) or string at......

I have used the variable $country_name in later places and it is
fine.


Dear Wilkie,

Even though you used the variable $country_name in later places and
it is fine, apparently the Perl interpreter thinks it is undefined when
it gets to that line. Or maybe it's not complaining about
$country_name in the second line, but rather about
$placement_category_id in the first.

Whatever the reason, you can figure out what is wrong by placing the
following lines before that first call to selectrow_array():

print "\$placement_category_id is ";
print (defined $placement_category_id)
? "\"$placement_category_id\"\n"
: "UNDEFINED\n";

and the following lines before your second call to selectrow_array():

print "\$country_name is ";
print (defined $country_name)
? "\"$country_name\"\n"
: "UNDEFINED\n";

These lines will tell you exactly what the Perl interpreter thinks
the values for $placement_category_id and $country_name are as it tries
to evaluate those lines you are having trouble with.

This might also be a great time to learn how to use the Perl
debugger. If you can execute this script from a command line, then
start the debugger like this:

perl -d script.pl

At the debugger prompt, type "c line_num", where "line_num" is the line
number of that first selectrow_array() call. This will cause the
debugger to run UP TO that line (that line will NOT be executed).

Then type "x $placement_category_id" to see what the Perl interpreter
thinks is the value for $placement_category_id.

Then type "n" to execute that line of code and advance to right before
the next.

Then type "x $country_name" to see what the Perl interpreter thinks is
the value for $country_name.

Hopefully, at this point you've discovered what's wrong. If so, you
can type "q" to quit out of the Perl debugger.

I hope this helps you, Wilkie.

-- Jean-Luc
 
W

worldcyclist

True to Occam's Razor the culprit has proven to be faulty data!
My country codes I had listed in the DB were preceeded a space!
It's always something simple it seems.
Jean - Luc, thank you SO very much for helping me with this.
By simply printing out the variable I caught the space.
Wilkie
 
J

jl_post

True to Occam's Razor the culprit has proven to be
faulty data! My country codes I had listed in the
DB were preceeded a space!
It's always something simple it seems.
Jean - Luc, thank you SO very much for helping me
with this. By simply printing out the variable I
caught the space.


I'm glad I could be of help. Often, successful debugging means
knowing where to place debug statements that will help you analyze
what's wrong.

Since you've already figured out what's wrong, this might sound
rather outdated, but I found a small (but significant) bug in the code
I sent you:

The second print statement is immediately followed by an open
parenthesis. As a result of the motto "if it looks like a parameter
list, it is a parameter list," the print statement will try to print
the result of "defined $country_name" which is either a true value
(most likely "1") or a false value (most likely "").

Then, depending on the return value of print() (most likely "1"),
the string '\"$country_name\"\n" will be evaluated (but not printed),
not giving any output.

There are three ways to fix this problem:

1. Explicitly put in the "parameter parentheses", like this:

print((defined $country_name)
? "\"$country_name\"\n"
: "UNDEFINED\n");

2. Put a '+' (a plus sign) before the open parenthesis, which
lets the Perl interpreter know that the parentheses are not
"parameter parentheses":

print +(defined $country_name)
? "\"$country_name\"\n"
: "UNDEFINED\n";

3. Combine that statement with the previous print() statement,
so that there is no way for the parentheses to get mixed up
as "parameter parentheses":

print "\$country_name is ",
(defined $country_name)
? "\"$country_name\"\n"
: "UNDEFINED\n";

Sorry for going into detail about this when it's not really useful
to you anymore, but I thought it would be better if I fixed a mistake I
made that might have tripped you up and caused you confusion.

This is a common error for people new to Perl, but someone like me
should have known better. But, of course, we all make mistakes, so I
was just letting you know that if you encountered a problem using my
original print() statements, it was more likely my fault and not yours.

Sorry for the confusion, and I'm glad I could help.

-- Jean-Luc
 
T

Tad McClellan

Since you've already figured out what's wrong, this might sound
rather outdated, but I found a small (but significant) bug in the code
I sent you:


The second print statement is immediately followed by an open
parenthesis.

There are three ways to fix this problem:


Four, M'lord! [1]

1. Explicitly put in the "parameter parentheses", like this:

print((defined $country_name)
? "\"$country_name\"\n"
: "UNDEFINED\n");

2. Put a '+' (a plus sign) before the open parenthesis, which
lets the Perl interpreter know that the parentheses are not
"parameter parentheses":

print +(defined $country_name)
? "\"$country_name\"\n"
: "UNDEFINED\n";


Yuk! (IMHO)

3. Combine that statement with the previous print() statement,
so that there is no way for the parentheses to get mixed up
as "parameter parentheses":

print "\$country_name is ",
(defined $country_name)
? "\"$country_name\"\n"
: "UNDEFINED\n";


4. Choose to use parenthesis on the _defined_ param list (best, IMO):


print "\$country_name is ";
print defined($country_name)
? "\"$country_name\"\n"
: "UNDEFINED\n";

But I would never inject all of those backslashes only to have to
filter them all back out when trying to understand the code:


print '$country_name is ';
print defined( $country_name )
? qq("$country_name"\n)
: qq(UNDEFINED\n);





[1] Wondering if DHA will notice that reference...
 
W

worldcyclist

This entire thread and the helpfulness therein have proven to me for
the 86,138th consecutive time why the Perl community is the best there
is when you have a problem.

Julian
 

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

Forum statistics

Threads
473,769
Messages
2,569,577
Members
45,052
Latest member
LucyCarper

Latest Threads

Top