I happen to be struggling with Class::DBI and date formats, but that's not what my question is about. I'll use my existing code for the sake of concreteness, but it's actually a question about Perl classes. The short version of the question is "how can I refer to 'the method I'm overriding' from within a method declaration in a package?"

Before querying my database for anything, I need to set NLS_DATE_FORMAT. It's possible that someone else needed to use a different format, so I should set it myself before every call, and reset it (to be polite). Don't be too mean about what a bad idea this is (I should probably use triggers); it's just the example to show my point.

Empirically, I've found that the following does what I want:

package MyDateExperiment ; use base qw/Class::DBI/ ; use Class::DBI::Plugin::DateFormat::Oracle qw(0.01) ; my $NLS_DATE_FORMAT_CONVENTIONAL = 'YYYY-MM-DD HH24:MI:SS' ; # # connection stuff... # name the table... # field definitions... # sub create { my $class = shift ; my $oldFormat = $class->get_nls_date_format; $class->set_nls_date_format( $NLS_DATE_FORMAT_CONVENTIONAL ) ; my $ans = $class->SUPER::create( @_ ) ; $class->set_nls_date_format( $oldFormat ) ; return $ans ; } sub get { my $class = shift ; my $oldFormat = $class->get_nls_date_format; $class->set_nls_date_format( $NLS_DATE_FORMAT_CONVENTIONAL ) ; my $ans = $class->SUPER::get( @_ ) ; $class->set_nls_date_format( $oldFormat ) ; return $ans ; } sub set { my $class = shift ; my $oldFormat = $class->get_nls_date_format; $class->set_nls_date_format( $NLS_DATE_FORMAT_CONVENTIONAL ) ; my $ans = $class->SUPER::set( @_ ) ; $class->set_nls_date_format( $oldFormat ) ; return $ans ; }
Now, I'm "smart" enough to see that create, get, and set are pretty similar and I'd prefer to be lazy. So I poke around and decide that this is what I'd prefer:
foreach my $meth (qw/create get set/) { no strict qw/refs/ ; *{__PACKAGE__ . "::$meth"} = sub { my $class = shift ; my $oldFormat = $class->get_nls_date_format; $class->set_nls_date_format( $NLS_DATE_FORMAT_CONVENTIONAL ) ; my $ans = _good_lord_what_function( @_ ) ; ### ??? $class->set_nls_date_format( $oldFormat ) ; return $ans ; } }
This "works" in that it defines the methods and they get called properly, but it's two hours later and I've got no idea how to call the superclass's "$meth" method when all I know is the string "$meth". Things I've tried (paraphrasing, because I didn't keep good track of all the experiments) include: There were others. Each failed in its own way (some aren't even syntactic). Can someone tell me the way I should have done it?

Thanks,

dave


In reply to How to get a reference to a superclass method? by mcdave

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.