Perl DBI Error Msg: Can't call method “selectcol_arrayref” on an undefined value

后端 未结 3 492
梦毁少年i
梦毁少年i 2021-01-26 14:50
my $dblinks = \'\';
$dblinks = $dbh->selectcol_arrayref(\"select db_link from db_links where ticket=\\\'LOW\\\'\");
my $success = 0;
for my $dblink (@$dblinks) {
  $s         


        
3条回答
  •  说谎
    说谎 (楼主)
    2021-01-26 15:40

    Update:

    Simple, really, you assign the handle returned by the connect call to $l_dbh but invoke a method on $dbh. You must use strict.

    Original answer:

    The database handle $dbh is not defined which means the connection failed. You should either check return values of your calls, or specify { RaiseError => 1} in the connect call to find out the reason.

    Further, there is no reason to prefix every sub invocation with &: Use ConnectFailed( ) instead of &ConnectFailed( ), unless you know the effect of prefixing a sub invocation with & and desire to have that effect.

    From perldoc perlsub:

    A subroutine may be called using an explicit & prefix. The & is optional in modern Perl, as are parentheses if the subroutine has been predeclared. The & is not optional when just naming the subroutine, such as when it's used as an argument to defined() or undef(). Nor is it optional when you want to do an indirect subroutine call with a subroutine name or reference using the &$subref() or &{$subref}() constructs, although the $subref->() notation solves that problem.

    ... If a subroutine is called using the & form, the argument list is optional, and if omitted, no @_ array is set up for the subroutine: the @_ array at the time of the call is visible to subroutine instead. This is an efficiency mechanism that new users may wish to avoid. (emphasis added).

提交回复
热议问题