Solved

what does undef means

Posted on 2009-07-06
6
307 Views
Last Modified: 2012-05-07
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
Comment
Question by:hatem_from_mesr
6 Comments
 
LVL 84

Expert Comment

by:ozo
ID: 24783759
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
 
LVL 9

Expert Comment

by:Suhas .
ID: 24783777
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
 
LVL 7

Accepted Solution

by:
Fairlight2cx earned 250 total points
ID: 24785549
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
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 
LVL 39

Assisted Solution

by:Adam314
Adam314 earned 250 total points
ID: 24787431
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
 
LVL 7

Assisted Solution

by:Fairlight2cx
Fairlight2cx earned 250 total points
ID: 24790597
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
 
LVL 39

Assisted Solution

by:Adam314
Adam314 earned 250 total points
ID: 24791394
>>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

Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

Join & Write a Comment

A year or so back I was asked to have a play with MongoDB; within half an hour I had downloaded (http://www.mongodb.org/downloads),  installed and started the daemon, and had a console window open. After an hour or two of playing at the command …
Checking the Alert Log in AWS RDS Oracle can be a pain through their user interface.  I made a script to download the Alert Log, look for errors, and email me the trace files.  In this article I'll describe what I did and share my script.
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…

707 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

14 Experts available now in Live!

Get 1:1 Help Now