Beefy Boxes and Bandwidth Generously Provided by pair Networks DiBona
P is for Practical
 
PerlMonks  

Re: Re: OO: Leaving a constructor midway?

by pdcawley (Hermit)
on Oct 25, 2003 at 06:15 UTC ( #302035=note: print w/ replies, xml ) Need Help??


in reply to Re: OO: Leaving a constructor midway?
in thread OO: Leaving a constructor midway?

What you describe in that code is not catching an exception, it's handling a return value, which means that it's doing the work too near to the failure. Have your login_user method use a real exception class, so if login fails you'd do:

... die Exception::NoUser->new(-requested_uri => $request->uri); ...
Then, way up at the top level, in your basic handler method that everything uses, you would have:
my $result = eval { $app->process($request) }; if ($@) { $result = ref($@) ? $@ : Exception::Unknown->new(-requested_uri => $request->uri, -message => $@); } $self->emit($result);
There are big problems with your code though, in that it makes assumptions about how it's being used, which makes it very hard to write tests for it. For instance, what is it that's inherent in your Book object that means it needs to know it's being used via a webpage? Slightly more controversially, why does it need to know it's being fetched from a database? As someone else suggested, maybe your application's process method needs to do something like:
sub process { my($self, $request) = @_; local Exception::current_uri = $request->uri; $self->get_resources($request); $self->authenticate_user($request); $self->authorize_user($request); $self->generate_response($request); } sub get_resources { my($self,$request) = @_; # Fetch your book from the database. $self->set_book($book); } sub authenticate_user { my($self, $request) = @_; my $user = $self->make_user_from_request($request) or die Exception::NoUser->new; $self->set_user($user); } sub authorize_user { my $self = shift; $self->user_has_required_access($user) or die Exception::InsufficientAccess->new(user => $user); }
If you set up your inheritance tree (or compose your objects) correctly, you'll only have to override one or two methods for each distinct page. And you'll have a collection of methods that you can test without having to pretend to be a webserver all the time, and that's a big win.


Comment on Re: Re: OO: Leaving a constructor midway?
Select or Download Code

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others meditating upon the Monastery: (5)
As of 2014-04-18 01:36 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    April first is:







    Results (460 votes), past polls