Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Perl > Perl Misc > Class::Std - how to indicate construction failure

Reply
Thread Tools

Class::Std - how to indicate construction failure

 
 
Dave Weaver
Guest
Posts: n/a
 
      04-12-2006
I'm experimenting with using Class::Std to implement 'inside-out'
objects.

In the 'normal' style Perl objects, it's traditional for the
constructor to return undef if the construction fails. In Class::Std
the user-level meat of the construction phase is put in the BUILD
method, but in reading the docs I see no way that the BUILD method can
pass back a failure status (other than by die-ing) to the code that
wants to constuct the object.

For example, using old-style Perl objects,

my $obj = MyFileObject->new({ file => 'test.txt' });

would return undef if, for example, the passed file couldn't be found.

What is the correct way to do this construct-time checking using
Class::Std? Do I have to override new() ?


 
Reply With Quote
 
 
 
 
Anno Siegel
Guest
Posts: n/a
 
      04-12-2006
Dave Weaver <(E-Mail Removed)> wrote in comp.lang.perl.misc:
> I'm experimenting with using Class::Std to implement 'inside-out'
> objects.
>
> In the 'normal' style Perl objects, it's traditional for the
> constructor to return undef if the construction fails. In Class::Std
> the user-level meat of the construction phase is put in the BUILD
> method, but in reading the docs I see no way that the BUILD method can
> pass back a failure status (other than by die-ing) to the code that
> wants to constuct the object.
>
> For example, using old-style Perl objects,
>
> my $obj = MyFileObject->new({ file => 'test.txt' });
>
> would return undef if, for example, the passed file couldn't be found.
>
> What is the correct way to do this construct-time checking using
> Class::Std? Do I have to override new() ?


In _PBP_, section Error Handling, Damian Conway recommends throwing exceptions
instead of returning special values. So the idea would be to die() in BUILD
and, if necessary, catch the error through "eval { Class->new(...) }".

On the other hand, Class::Std is not the only module on CPAN that supports
inside-out classes, and not necessarily the best one. Last time I looked
there were Class::InsideOut and Object::InsideOut. Take a look at them
if they fit your needs better.

If you are serious about studying inside-out classes you should also
build at least one small (but more than trivial) class hierarchy directly,
creator, destructor and all, if only to see what it is the pre-made classes
save you.

Anno
--
If you want to post a followup via groups.google.com, don't use
the broken "Reply" link at the bottom of the article. Click on
"show options" at the top of the article, then click on the
"Reply" at the bottom of the article headers.
 
Reply With Quote
 
 
 
 
Anno Siegel
Guest
Posts: n/a
 
      04-12-2006
Abigail <(E-Mail Removed)> wrote in comp.lang.perl.misc:
> Dave Weaver ((E-Mail Removed)) wrote on MMMMDCVII September MCMXCIII in
> <URL:news:443d0223$0$665$(E-Mail Removed) >:
> I'm experimenting with using Class::Std to implement 'inside-out'
> objects.


Hey, a smiley as quotation marker.

> IMNSHO, I think the best method to learn what 'Inside Out Objects'
> are is to implement them without modules.
>
> Once you know what they're about, you can make a judgement on what
> you're willing to sacrifice in order to able to use a module.
>
> I've looked at several of the modules implemented Inside Out Objects,
> and I've found them ranging from overly complicated to completely
> missing the point of Inside Out Objects.
>
> In the 'normal' style Perl objects, it's traditional for the
> constructor to return undef if the construction fails.
>
> Really?
>
> I think that constructors should construct, and nothing else. Which
> means that the only reason the constructor can fail is the program
> running out of memory. Which is a nontrappable "very fatal error".


Ah, but he's talking about Class::Std, which doesn't support the
separation of creation and initialization very well. The question
was really how to deal with errors in BUILD, which is initialization,
not creation. Having to deal with run time errors in initialization
is common.

On the other hand, having separate ->init and ->new methods (or whatever
they're called) isn't particular to inside-out classes. Traditional
classes would benefit from the separation just as well. It isn't
seen very often, probably because the disadvantages of a combined
creator/initializer only become apparent with multiple inheritance.

With single inheritance it is possible to slip by, using the parent's
->new to create an object blessed in your class, but initialized for
the parent. Then you insert your own data into the existing structure,
(often in rather unclean ways). That gives you another ->new that
initializes its objects. Only when there is yet another parent class
to initialize it becomes a problem that you can't do that without
creating another object.

Anno
--
If you want to post a followup via groups.google.com, don't use
the broken "Reply" link at the bottom of the article. Click on
"show options" at the top of the article, then click on the
"Reply" at the bottom of the article headers.
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Re: How include a large array? Edward A. Falk C Programming 1 04-04-2013 08:07 PM
Default construction versus construction with initial values Ook C++ 10 10-08-2005 09:00 PM
LibXML UTF8 - Input is not proper UTF-8, indicate encoding ! Vlajko Knezic Perl 1 03-06-2005 08:53 AM
Corrupted Closed-Captioning Indicate Possible Disc Failure? Confessor DVD Video 1 08-14-2004 06:49 AM
Can someone indicate me the following Newsgroups, please? Miguel Dias Moura ASP .Net 0 05-24-2004 03:18 PM



Advertisments