I just discovered (thanks to diotalevi for pointing me at it) an arcane (?) but possibly useful feature of Perl's object method calling syntax: You can specify a fully qualified (including the package) name for the method.

package SomeDatabaseStuff; sub my_funny_sql{ my ($dbh) = @_; $dbh->do('my great query'); }

You (or at least I) have until now called the subroutine (note that it is not even intended to be a method at all) like so

my_funny_sql($dbh);

But it can also be called like this:

$dbh->SomeDatabaseStuff::my_funny_sql();

This is especially nice if you have additional parameters that you want to visually seperate from $dbh:

$dbh->SomeDatabaseStuff::my_funny_sql ( 'some parameter', 12 , 34 );

Note that this syntax can only be used when $dbh is indeed a blessed reference (although its class seems to be not involved in the "method" lookup anymore), and if the name of subroutine you want is fully qualified (otherwise the normal method lookup will occur, starting at the package of $dbh)

This even works in combination with @ISA-inheritence (although the search starts at the package of the subroutine, not the package of the object as usual):

package MoreDatabaseStuff; @MoreDatabaseStuff::ISA = qw( SomeDatabaseStuff); # no subroutines here $dbh->MoreDatabaseStuff::my_funny_sql(); # ends up being dispatched properly # to the sub in the parent SomeDatabaseStuff

All this is probably mentioned in the Perl OO docs, but it was new to me.

Update: I forgot to point out the important detail that $dbh is a DBI connection handle (and not something blessed into SomeDatabaseStuff).

my $dbh = DBI->connect('....'); $dbh->SomeDatabaseStuff::my_funny_sql;

In reply to $object->UnrelatedPackage::some_subroutine() by Thilosophy

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • 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:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.