Cannot find object method via package

This is my first foray into subclasses with perl, and I wonder why I get this simple error ...
"It is not possible to find the prepare method using the package" WebDB :: st "in / home / dblibs / WebDB.pm line 19. " It seems to find the WebDB module in order, but not the preparation routine in :: st
First, here is my package (both packages are in the same file, WebDB.pm)

package WebDB;
use strict;
use DBI;

sub connect {
    my $dbh = (DBI->connect ("DBI:mysql:test:127.0.0.1", "root","",
                    { PrintError => 1, RaiseError => 0 }));
    return bless $dbh, 'WebDB::st';
}

package WebDB::st;
our @ISA = qw(::st);
sub prepare {
    my ($self, $str, @args) = @_;
    $self->SUPER::prepare("/* userid:$ENV{USER} */ $str", @args);
}


1;

I also tried replacing "our @ISA = qw (;; st)" with "use the WebDB database and the same problem. I think this is probably something very simple that I'm missing. Thanks a lot! Jane

+3
3

DBI . DBI RootClass ( @ISA, DBI). , WebDB:: st DBI:: st subclassing DBD:: db WebDB:: db ( ). .

base; , , , . @ISA, parent :

package WebDB;
use parent 'DBI';
...
package WebDB::db;
use parent -norequire => 'DBI::db';
...
package WebDB::st;
use parent -norequire => 'DBI::st';
...
+9

WebDB WebDB::st ? , , use WebDB::st;, .

- ( , ), use WebDB::st; WebDB.pm.

( use strict; use warnings; .)

, ::st - ( ). prepare WebDB::st - package. , WebDB::st ::st .

0

If subclassing is as complicated as ysth seems, I could recommend Class::Delegatorfrom CPAN. I use if for classes that want to act like IO. And thanks to this, Perl is the first language (which I know) that has an expression language for aggregation, delegation, encapsulation, almost equal to inheritance.

package WebDB;
use strict;
use DBI;

use Class::Delegator
    send => [ qw<connect ...> ]
  , to   => '{_dbihandle}'
    ...
  ;
0
source

Source: https://habr.com/ru/post/1773888/


All Articles