Solved

what does undef means

Posted on 2009-07-06
6
350 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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
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!

 
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

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Many time we need to work with multiple files all together. If its windows system then we can use some GUI based editor to accomplish our task. But what if you are on putty or have only CLI(Command Line Interface) as an option to  edit your files. I…
There are many situations when we need to display the data in sorted order. For example: Student details by name or by rank or by total marks etc. If you are working on data driven based projects then you will use sorting techniques very frequently.…
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…

735 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