Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 366
  • Last Modified:

what does undef means

can any one help me to know what does undef substitute for in the next fragment of code

sub delete_item
{
my ($dbh, $id) = @_;
    return ($dbh->do (qq{ DELETE FROM todo WHERE id = ? },
                undef,
                $id));
}
0
hatem_from_mesr
Asked:
hatem_from_mesr
4 Solutions
 
ozoCommented:
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 "*").  (Saying
               "undef $hash{$key}" will probably not do what you expect on
               most predefined variables or DBM list values, so don't do that;
               see delete.)  Always returns the undefined value.  You can omit
               the EXPR, in which case nothing is undefined, but you still get
               an undefined value that you could, for instance, return from a
               subroutine, assign to a variable or pass as a parameter.
               Examples:

                   undef $foo;
                   undef $bar{'blurfl'};      # Compare to: delete $bar{'blurfl'};
                   undef @ary;
                   undef %hash;
                   undef &mysub;
                   undef *xyz;       # destroys $xyz, @xyz, %xyz, &xyz, etc.
                   return (wantarray ? (undef, $errmsg) : undef) if $they_blew_it;
                   select undef, undef, undef, 0.25;
                   ($a, $b, undef, $c) = &foo;       # Ignore third value returned

               Note that this is a unary operator, not a list operator.
0
 
Suhas .QA ManagerCommented:
perldoc undef
http://perldoc.perl.org/functions/undef.html

In general, any value that is not defined is called as undef.

A good example:
http://www.linuxtopia.org/Perl_Programming/pickingUpPerl_29.html
0
 
Fairlight2cxCommented:
Everyone so far has defined what "undef" means/does for you.  I don't think that's your question.  You asked what it substitutes for.

The '?' in the do() function's first string is a placeholder.  Arguments coming after that initial string should be taken in order and put into the placeholders' places.

Seeing as there is only one placeholder, I believe the undef being present is an error.  There is one placeholder, and there are two substitution values present.  That's a mismatched count.  I believe undef and the following comma should be removed, thus allowing $id to take its apparently rightful place in the "id ="  WHERE clause in the SQL.

Technically, using a placeholder for one value is a complete waste.  I'd just rewrite it so that it was:  

return($dbh->do(qq(DELETE FROM todo WHERE id = $id)));

A placeholder in this instance is really just overhead you don't need to impart.  It's really meant for things like INSERT INTO tablename VALUES (?,?,?,?,?,?) situations where you have a cartload of extra values to pass in.
0
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
Adam314Commented:
From the DBI documentation for the do function:
    http://search.cpan.org/~timb/DBI-1.609/DBI.pm#do
The first parameter should be the SQL statement, the second any attributes for that statement, the third any bind values.

Your statement uses undef for the attributes, which means this statement should use the default attributes for the connection.  Some possible values for the attributes could be RaiseError or PrintError - so that this statement will behave differently than the default for the connection.

0
 
Fairlight2cxCommented:
Well that makes sense.  I usually don't use do() myself, preferring prepare/execute pairs.  Hence my newness to them.  But it does solve the apparent argument mismatch issue.

I still maintain that using placeholders for one value instead of doing it inline is simply greater overhead and complexity than necessary.
0
 
Adam314Commented:
>>I still maintain that using placeholders for one value instead of doing it inline is simply greater overhead and complexity than necessary.
Depends on what types of values $id can take.  If it could be a name, it gets complicated.  What if the name is "O'Mally" - it contains a single quote.  You now have to escape that.  Or, what if the $id comes from a user that entered it on a web form, and the user maliciously enters something like "1; DELETE FROM table;".  Using a placeholder will solve these problems - so the overhead may be very worthwhile.

The %attr parameter is available for prepare also, so that the particular statement can have attributes different from the connection default.  I don't use them very often either though.
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Tackle projects and never again get stuck behind a technical roadblock.
Join Now