Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
815 views
in Technique[技术] by (71.8m points)

perl - What is the purpose of passing undef to DBI's `do` method in this context?

I don't understand what undef is doing in this snippet:

$dbh->do (qq {
            INSERT INTO todo SET t = NOW(), status = 'open', content = ?
        }, undef, $content);

Can someone please explain? I think I understand the whole code, but not this where it came from.

use warnings;
use strict;
use lib q(/data/TEST/perl/lib);
use CGI qw(:standard);
use WebDB;

sub insert_item {
    my $content = shift;
    my $dbh;
    $content =~ s/^s+//;
    $content =~ s/^s+$//;
    if ($content ne "") {
        $dbh = WebDB::connect();
        $dbh->do (qq {
            INSERT INTO todo SET t = NOW(), status = 'open', content = ?
        }, undef, $content);
        $dbh->disconnect();
    }
}

sub display_entry_form {
    print start_form(-action=> url()),
    "To-do item:", br (),
    textarea ( -name => "content",
               -value => "",
               -override => 1,
               -rows =>3,
               -columns => 80),
    br (),
    submit(-name=> "choice", -value => "Submit"),
    end_form();
}

print header(), start_html(-title=>"To-Do List", -bgcolor => "white"), h2("To-Do List");

my $choice = lc(param ("choice"));

if ($choice eq "") {
    display_entry_form();
} elsif ( $choice eq "submit" ) {
    insert_item(param("content"));
    display_entry_form();
} else {
    print p ("Logic error, unknown choice: $choice");
}
question from:https://stackoverflow.com/questions/65878122/what-is-the-purpose-of-passing-undef-to-dbis-do-method-in-this-context

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

The do() method takes 3 arguments: the query, query attributes, and bind data. The undef in your example means that there are no attributes to apply.

See "do()" in DBI on CPAN.

$rows = $dbh->do($statement)           or die $dbh->errstr;
$rows = $dbh->do($statement, \%attr)   or die $dbh->errstr;
$rows = $dbh->do($statement, \%attr, @bind_values) or die ...

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

2.1m questions

2.1m answers

60 comments

57.0k users

...