"Use of uninitialized value at..."

[b][red]This message was edited by Supergibbs at 2004-10-5 15:46:7[/red][/b][hr]
Is this error normal in CGI SQL code? After I do a SELECT I have a line like this:
[code]if($ref->[1])
{
print "[1]'>Link";
}
else
{
print "No Link";
}[/code]

This writes a link if there is a URL in the mySQL DB. If it doesn't exist, I get the "Use of uninitialized value at line <##>" error in my apache log. It displays correctly, but it fills up my log, can I avoid this somehow?

thanks,
Jesse


Comments

  • : [b][red]This message was edited by Supergibbs at 2004-10-5 15:46:7[/red][/b][hr]
    : Is this error normal in CGI SQL code? After I do a SELECT I have a line like this:
    : [code]if($ref->[1])
    : {
    : print "[1]'>Link";
    : }
    : else
    : {
    : print "No Link";
    : }[/code]
    :
    : This writes a link if there is a URL in the mySQL DB. If it doesn't exist, I get the "Use of uninitialized value at line <##>" error in my apache log. It displays correctly, but it fills up my log, can I avoid this somehow?
    :
    The problem probably comes down to the fact that you dereference $ref in your if statement, thus giving the error. Try:-

    if ($ref) {
    ...
    }

    If if you need to check the second result is there...

    if ($ref && $ref->[1]) {
    ...
    }

    Note: The second works due to && being a short-circuit operator, so if $ref is undefined then if never bothers to look at $ref->[1], as the overall result of the evaluation cannot be true.

    Jonathan

    ###
    for(74,117,115,116){$::a.=chr};(($_.='qwertyui')&&
    (tr/yuiqwert/her anot/))for($::b);for($::c){$_.=$^X;
    /(p.{2}l)/;$_=$1}$::b=~/(..)$/;print("$::a$::b $::c hack$1.");

  • [b][red]This message was edited by Supergibbs at 2004-10-6 18:5:59[/red][/b][hr]
    Thanks but that wasn't it. Those lines are in a loop that does the first check.
    [code]
    while($ref = $sth->fetchrow_arrayref())
    {
    if($ref->[1])
    {
    print "[1]'>$link";
    }
    else
    {
    print "$nolink";
    }
    }
    [/code]

    Any other ideas?

    -Jesse

    PS-this is a simpler version, the full one uses $ref in other places successfully, so it isn't a matter of whether $ref is defined or not

    : : [b][red]This message was edited by Supergibbs at 2004-10-5 15:46:7[/red][/b][hr]
    : : Is this error normal in CGI SQL code? After I do a SELECT I have a line like this:
    : : [code]if($ref->[1])
    : : {
    : : print "[1]'>Link";
    : : }
    : : else
    : : {
    : : print "No Link";
    : : }[/code]
    : :
    : : This writes a link if there is a URL in the mySQL DB. If it doesn't exist, I get the "Use of uninitialized value at line <##>" error in my apache log. It displays correctly, but it fills up my log, can I avoid this somehow?
    : :
    : The problem probably comes down to the fact that you dereference $ref in your if statement, thus giving the error. Try:-
    :
    : if ($ref) {
    : ...
    : }
    :
    : If if you need to check the second result is there...
    :
    : if ($ref && $ref->[1]) {
    : ...
    : }
    :
    : Note: The second works due to && being a short-circuit operator, so if $ref is undefined then if never bothers to look at $ref->[1], as the overall result of the evaluation cannot be true.
    :
    : Jonathan
    :
    : ###
    : for(74,117,115,116){$::a.=chr};(($_.='qwertyui')&&
    : (tr/yuiqwert/her anot/))for($::b);for($::c){$_.=$^X;
    : /(p.{2}l)/;$_=$1}$::b=~/(..)$/;print("$::a$::b $::c hack$1.");
    :
    :



  • : Thanks but that wasn't it. Those lines are in a loop that does the
    : first check.
    : [code]
    : while($ref = $sth->fetchrow_arrayref())
    : {
    : if($ref->[1])
    : {
    : print "[1]'>$link";
    : }
    : else
    : {
    : print "$nolink";
    : }
    : }
    : [/code]
    :
    Which line does the error occur on (match up the line in the error log with that line in your source code)? The only other variables in the code you posted are $link and $nolink - are they declared and defined (e.g. set to something) anywhere before this point?

    Jonathan

    ###
    for(74,117,115,116){$::a.=chr};(($_.='qwertyui')&&
    (tr/yuiqwert/her anot/))for($::b);for($::c){$_.=$^X;
    /(p.{2}l)/;$_=$1}$::b=~/(..)$/;print("$::a$::b $::c hack$1.");

  • Yes, the error is still on the if($ref->[1]) line. Basically I have a DB of info that get outputted in HTML all the fields are set except the URL field sometimes. If it is there should be a link otherwise there shouldn't. When there is a link, it works fine. When there isn't, it still works (it returns false) but logs an error as well. I guess I could set the url value to NOLINK or a valid url and check that. But this should work and is a bit cleaner.

    -Jesse

    : : Thanks but that wasn't it. Those lines are in a loop that does the
    : : first check.
    : : [code]
    : : while($ref = $sth->fetchrow_arrayref())
    : : {
    : : if($ref->[1]) #HERE
    : : {
    : : print "[1]'>$link";
    : : }
    : : else
    : : {
    : : print "$nolink";
    : : }
    : : }
    : : [/code]
    : :
    : Which line does the error occur on (match up the line in the error log with that line in your source code)? The only other variables in the code you posted are $link and $nolink - are they declared and defined (e.g. set to something) anywhere before this point?
    :
    : Jonathan
    :
    : ###
    : for(74,117,115,116){$::a.=chr};(($_.='qwertyui')&&
    : (tr/yuiqwert/her anot/))for($::b);for($::c){$_.=$^X;
    : /(p.{2}l)/;$_=$1}$::b=~/(..)$/;print("$::a$::b $::c hack$1.");
    :
    :

  • : Yes, the error is still on the if($ref->[1]) line. Basically I have a
    : DB of info that get outputted in HTML all the fields are set except
    : the URL field sometimes. If it is there should be a link otherwise
    : there shouldn't. When there is a link, it works fine. When there
    : isn't, it still works (it returns false) but logs an error as well. I
    : guess I could set the url value to NOLINK or a valid url and check
    : that. But this should work and is a bit cleaner.
    :
    Aha, OK. I think I know what it is - definedness and truth are different. It's true that undefined always means untrue, but what you are actually doing in the if statement is looking at the value - essentially, using an undefined value. So it wants to be:-

    if (defined($ref->[1])) {
    ...
    }

    I'm pretty sure that will be it...sorry it took so long for me to spot it. I generally choose not to use the warnings pragma (and just use strict) because I find many of the warnings are excessively annoying. :-)

    Jonathan

    ###
    for(74,117,115,116){$::a.=chr};(($_.='qwertyui')&&
    (tr/yuiqwert/her anot/))for($::b);for($::c){$_.=$^X;
    /(p.{2}l)/;$_=$1}$::b=~/(..)$/;print("$::a$::b $::c hack$1.");

  • [b][red]This message was edited by Supergibbs at 2004-10-7 19:40:19[/red][/b][hr]
    I tried that and it didn't help either. After staring at it for a long time I realized the problem. PERL treats
    [code]if($ref->[1])
    {
    print "$test";
    }[/code]

    as one statement and therefore one line.
    The problem ended up being $test not $ref->[1].

    Thanks for all your help,
    Jesse




    : : Yes, the error is still on the if($ref->[1]) line. Basically I have a
    : : DB of info that get outputted in HTML all the fields are set except
    : : the URL field sometimes. If it is there should be a link otherwise
    : : there shouldn't. When there is a link, it works fine. When there
    : : isn't, it still works (it returns false) but logs an error as well. I
    : : guess I could set the url value to NOLINK or a valid url and check
    : : that. But this should work and is a bit cleaner.
    : :
    : Aha, OK. I think I know what it is - definedness and truth are different. It's true that undefined always means untrue, but what you are actually doing in the if statement is looking at the value - essentially, using an undefined value. So it wants to be:-
    :
    : if (defined($ref->[1])) {
    : ...
    : }
    :
    : I'm pretty sure that will be it...sorry it took so long for me to spot it. I generally choose not to use the warnings pragma (and just use strict) because I find many of the warnings are excessively annoying. :-)
    :
    : Jonathan
    :
    : ###
    : for(74,117,115,116){$::a.=chr};(($_.='qwertyui')&&
    : (tr/yuiqwert/her anot/))for($::b);for($::c){$_.=$^X;
    : /(p.{2}l)/;$_=$1}$::b=~/(..)$/;print("$::a$::b $::c hack$1.");
    :
    :



Sign In or Register to comment.

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories