Beefy Boxes and Bandwidth Generously Provided by pair Networks
Syntactic Confectionery Delight
 
PerlMonks  

Re: Handling cascading defaults

by ncw (Friar)
on Aug 19, 2000 at 11:38 UTC ( [id://28628]=note: print w/replies, xml ) Need Help??


in reply to Handling cascading defaults

If you wanted to use OO then you could do this
package Err; sub new { return bless { title => 'Really Bad Error', msg => 'Unknown', @_ }; } sub err { my %args = ( %{shift()}, @_ ); print "Error: $args{title}: $args{msg}\n"; } ########### package MyModule; $myerror = Err::new( title =>'My new default title for MyModule.pm', msg => 'MyModules generic error message' ); $myerror->err(); $myerror->err(title=>'Oops',msg=>'something went wrong');
I was wondering whether you could do something using the __PACKAGE__ variable and a hash in the Err package, but I couldn't think of a way of reading your caller's package. I expect there is a way though. Then the err() function could read the callers package and read the default strings from a hash which the calling package had assigned.

Replies are listed 'Best First'.
RE (tilly) 2: Handling cascading defaults
by tilly (Archbishop) on Aug 19, 2000 at 17:29 UTC
    I would actually organize that slightly differently:
    package Err; sub new { return bless {}, shift; } sub err { my $self = shift; my %args = @_; # Do something with %args here }
    And then in other code you would do this:
    $handler ||= new Err; # time passes &do_right_thing() or $handler->err(title=>'Oops',msg=>'something went +wrong');
      At this point, since you've not shown the need for inheritance implementation reuse or even interface reuse, I'd stay away from the complexity of objects. My personal rule is not to pull in OO technology in Perl until the line count of the program exceeds about 1000 lines, and using all the OO features of abstraction, inheritance, and data hiding all becomes useful. So far, all you've done is named a data structure. {grin}

      -- Randal L. Schwartz, Perl hacker

        I agree that OO is not a great wheel for this case. But I do see two points with that code. The first is that you probably want your error message put into the handler, and not the constuctor. (Which is why I posted.) The second is that you get an indirection layer.

        OTOH if the indirection layer is all that is desired, then a reference to a sub does that without the OO machinery hanging around.

        Incidentally I don't like to put full OO designs into a ton of code either. However I do like trying to put some sort of indirection in early. But to do it in a way where I can behind the scenes figure out how to do it better later.

        For instance some of the arguments will likely go into many messages, so the code I posted could have been improved to

        package Err; sub new { return bless {@_}, shift; } sub err { my $self = shift; my %args = (%$self, @_); # Do something with %args here }
        Now, while OO probably still isn't a great fit, at least I have done more with it than provide a level of indirection in how the subroutine is named. :-)
OO vs. procedural style
by markjugg (Curate) on Aug 19, 2000 at 18:02 UTC
    Thanks ncw. I had thought of an arrangement like the one above. I think the code design could be considered to cleaner, but here's what bothers me about it:

    One programming principle is "optimizing for the common case". The common case here is that I create a new "err" call in a script, as above. If I switch the OO style, instead of writing out 'err' each time, I'm now writing out '$err->err', so the solution makes more work in the common case.

    What about the magic that CGI.pm uses, where you can call routines with an OO style, or with a procedural style, and you don't even have to start with "new CGI", because there's a default object created. Can someone explain that?

    As far as fetching the caller's package, perl makes that easy: $callers_pkg = caller;

    I did end up using that in an inbetween module once, with the logic of "If my caller's package has defaults use those, otherwise use the defaults in CGI::Err". It worked, but that didn't seem clean either...

    Thanks for any further comment. -mark

      The trick that CGI uses is all in the following sub:
      sub self_or_default { return @_ if defined($_[0]) && (!ref($_[0])) &&($_[0] eq 'CGI'); unless (defined($_[0]) && (ref($_[0]) eq 'CGI' || UNIVERSAL::isa($_[0],'CGI')) # slightly optimized for common case ) { $Q = $CGI::DefaultClass->new unless defined($Q); unshift(@_,$Q); } return @_; }
      They preprocess the args to every function this way, and as long as the first arg is 'CGI' or an object that inherits from CGI they leave the arguments alone. Otherwise they prepend a global to the list.

      This is nice but imposes quite a bit of overhead.

      Another way of doing the same thing is to make the procedural interface be in a different package, and have a well thought-out AUTOLOAD sub that creates needed procedural wrappers at run-time. More complex but probably more efficient than what CGI does.

      Another solution that I have used is to think functionally. There you actually have your error handler be an anon sub. By default it is your default, but it can be anything you want. In this case you would leave your package alone and in your CGI script say:

      $handler ||= \&Err::err; # time passes do_the_right_thing() || $handler->(title=>'Oops',msg=>'something went +wrong');
      If $handler is left alone, well you get the default. But anyone who wants can change it.

      I have often found that this functional approach is a more efficient way to implement optional hooks. YMMV.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://28628]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others examining the Monastery: (5)
As of 2024-04-23 15:28 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found