Hi,
I've been messing around with the AUTOLOAD, specifically, loading subs from a database table. Everything's going fine, except for the past few week's i've been trying to add the ability to have mulitiple methods stored in the database, depending on how the method is called.
I.e, as an instance $blah->hello(); or straight from the module Blah::hello();
I think that there's the third way of calling the sub from inside another database loaded sub with just hello();
How on earth can I detect how the function has been called from within the AUTOLOAD function?
I know I can use ref() to check the first value of @_ to differentiate between $blah->hello() and Blah::hello(), but i'm having a mental block trying to work out what would happen if the function was called from an instance method, but passed an instance as the first parameter?
Maybe this doesnt make sense and i've just been thinking about it for far too long, but if you can help, i'd be most grateful. The code's below.
Thanks,
Stephen
sub AUTOLOAD {
my $self = shift;
my $type = ref($self) || $self;
return if $AUTOLOAD =~ /::DESTROY$/;
my $name = $AUTOLOAD;
$name =~ s/.*://;
my $dbh = BSN::DB::connect();
my $sql = "select * from method_tbl where name='".$name."' and typ
+e='".$self->{_type}."'";
my $sth = $dbh->prepare($sql);
$sth->execute();
my $ref = $sth->fetchrow_hashref();
unless (exists $ref->{body}) {
$sql = "select * from method_tbl where name='".$name."' and ty
+pe='Default'";
$sth = $dbh->prepare($sql);
$sth->execute();
$ref = $sth->fetchrow_hashref();
unless (exists $ref->{body}) {
return "Can't access `$name' method in class ".$self->{_ty
+pe};
}
}
my $call = eval("$ref->{body}");
if ($@) {
croak "Method Error: $@ in method `$name' in class $type";
}
unshift @_,$self;
goto &$call;
}
Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
Read Where should I post X? if you're not absolutely sure you're posting in the right place.
Please read these before you post! —
Posts may use any of the Perl Monks Approved HTML tags:
- a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
| |
For: |
|
Use: |
| & | | & |
| < | | < |
| > | | > |
| [ | | [ |
| ] | | ] |
Link using PerlMonks shortcuts! What shortcuts can I use for linking?
See Writeup Formatting Tips and other pages linked from there for more info.