Beefy Boxes and Bandwidth Generously Provided by pair Networks
laziness, impatience, and hubris
 
PerlMonks  

Why undefined?

by pcouderc (Beadle)
on Apr 21, 2006 at 06:37 UTC ( #544790=perlquestion: print w/ replies, xml ) Need Help??
pcouderc has asked for the wisdom of the Perl Monks concerning the following question:

I do not understand the error in the following sequence (modperl):
my $nomSQL=$Nom;
if(undef($nomSQL)) { $nomSQL ="";}
$nomSQL =~ s/'/''/;

Use of uninitialized value in substitution (s///) at (...)/creat.pl line 33.
the "'" disappears, and I do not understand why after reading and rereading pelre, perlrequick and perlretut. The "'" is not special character?

Thank you in advance
Pierre Couderc

Comment on Why undefined?
Re: Why undefined?
by chargrill (Parson) on Apr 21, 2006 at 06:50 UTC

    Quoting perldoc -f undef:

    undef EXPR undef Undefines the value of EXPR, which must be an lvalue. +Use only on a scalar value, an array (using "@"), a hash (using +"%"), a subroutine (using "&"), or a typeglob (using "*"). (Sa +ying "undef $hash{$key}" will probably not do what you expec +t on most predefined variables or DBM list values, so don't +do that; see delete.) Always returns the undefined value.

    So, your if, while undef'ing $nomSQL, returns undef. And you can't perform a s/// substitution on an undefined variable.

    Are you sure you don't want:

    ...

    You know, I was going to post some code, but I really can't tell what you want to do. Obviously you want to 'escape' single quotes in an SQL statement, but it looks like you're trying your darndest to make sure the string is blank before you do it.

    What do you want to do?



    --chargrill
    $,=42;for(34,0,-3,9,-11,11,-17,7,-5){$*.=pack'c'=>$,+=$_}for(reverse s +plit//=>$* ){$%++?$ %%2?push@C,$_,$":push@c,$_,$":(push@C,$_,$")&&push@c,$"}$C[$# +C]=$/;($#C >$#c)?($ c=\@C)&&($ C=\@c):($ c=\@c)&&($C=\@C);$%=$|;for(@$c){print$_^ +$$C[$%++]}
Re: Why undefined?
by davido (Archbishop) on Apr 21, 2006 at 07:09 UTC

    if(undef($nomSQL)) { $nomSQL = "";} is probably not what you want. You are undefining $nomSQL, rather than testing for definedness. You probably want this:

    if( not defined $nomSQL ) { $nomSQL = ''; }

    ...also written as:

    unless( defined $nomSQL ) { $nomSQL = ''; }

    ...but the latter reads less clearly to me.


    Dave

Re: Why undefined?
by pcouderc (Beadle) on Apr 21, 2006 at 07:58 UTC
    Thank you, great Monks,
    Sure, I have confused "undef" and "!defined"...

    Pierre Couderc
Re: Why undefined?
by Jasper (Chaplain) on Apr 21, 2006 at 10:56 UTC
    $nomSQL =~ s/'/''/;

    NO NO NO NO NO

    Investigate the use of placeholders in SQL, or use an available 'quoting' method.
Re: Why undefined?
by blazar (Canon) on Apr 21, 2006 at 12:43 UTC

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://544790]
Approved by chargrill
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others musing on the Monastery: (10)
As of 2014-08-22 06:29 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    The best computer themed movie is:











    Results (148 votes), past polls