Interpolation question

Discussion in 'Perl Misc' started by worldcyclist@gmail.com, Mar 6, 2005.

  1. Guest

    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
    , Mar 6, 2005
    #1
    1. Advertising

  2. Guest

    wrote:
    >
    > 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
    , Mar 6, 2005
    #2
    1. Advertising

  3. Guest

    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

    wrote:
    > wrote:
    > >
    > > 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
    , Mar 6, 2005
    #3
  4. Guest

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

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


    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
    , Mar 6, 2005
    #4
  5. <> wrote:


    > 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:
    >
    >> > print "\$country_name is ";
    >> > print (defined $country_name)
    >> > ? "\"$country_name\"\n"
    >> > : "UNDEFINED\n";

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

    --
    Tad McClellan SGML consulting
    Perl programming
    Fort Worth, Texas
    Tad McClellan, Mar 6, 2005
    #5
  6. Guest

    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
    , Mar 9, 2005
    #6
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Pratap Das
    Replies:
    1
    Views:
    441
    Richard Heathfield
    Oct 14, 2003
  2. Andrew Fabbro

    Variable interpolation question

    Andrew Fabbro, Nov 17, 2003, in forum: Python
    Replies:
    3
    Views:
    290
    anton muhin
    Nov 17, 2003
  3. Älphä Blüë

    Interpolation question with each do block

    Älphä Blüë, Jul 16, 2009, in forum: Ruby
    Replies:
    2
    Views:
    172
    Älphä Blüë
    Jul 16, 2009
  4. Dudebot

    Interpolation question

    Dudebot, Feb 27, 2010, in forum: Ruby
    Replies:
    7
    Views:
    120
    Dudebot
    Feb 27, 2010
  5. Dudebot

    Interpolation question

    Dudebot, Feb 27, 2010, in forum: Ruby
    Replies:
    0
    Views:
    79
    Dudebot
    Feb 27, 2010
Loading...

Share This Page