mcdave has asked for the wisdom of the Perl Monks concerning the following question:
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:
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: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 ; }
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: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 ; } }
no strict qw/refs/ ; my $originalFunction = \&{__PACKAGE__ . "::$meth" } ; *{__PACKAGE__ . "::$meth"} = sub { ... my $ans = $originalFunction->( $class, @_ ) ; ... }
... my ($papa) = Class::ISA::super_path($class) ; my $papaMethod = $papa->can($meth) ; my $ans = $papaMethod->($papa, @_ ) ; ...
... my $ans = $class->SUPER::{"$meth"}( @_ ) ; ...
Thanks,
dave
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: How to get a reference to a superclass method?
by runrig (Abbot) on Feb 10, 2012 at 17:39 UTC | |
by mcdave (Beadle) on Feb 10, 2012 at 18:00 UTC | |
by Anonymous Monk on Feb 10, 2012 at 19:47 UTC | |
by mcdave (Beadle) on Feb 11, 2012 at 14:59 UTC | |
by Anonymous Monk on Feb 11, 2012 at 15:15 UTC | |
by Anonymous Monk on Feb 10, 2012 at 17:42 UTC | |
|
Re: How to get a reference to a superclass method?
by Anonymous Monk on Feb 10, 2012 at 17:40 UTC |