Yeah, I know that inheritance might be a problem, but decided to ignore that for now. I know, I know, bad toad...

Actually, I did look at UNIVERSAL::isa(), but I didn't get that to work properly. Matter of factly, your code is very much like I started to write it - and your code breaks my testcases for this. This is what I get from them:

1..11 Testing to construct via 'start_stuff()' Can't use string ("MyClass") as a HASH ref while "strict refs" in use +at MyClass.pm line 61 (#1) (F) Only hard references are allowed by "strict refs". Symbolic references are disallowed. See perlref. Uncaught exception from user code: Can't use string ("MyClass") as a HASH ref while "strict refs" + in use at MyClass.pm line 61. MyClass::start_stuff('MyClass') called at testmyclass.pl line +10
And these are the very simple testcases:
#!/usr/bin/perl -w use strict; use Test; BEGIN { plan tests => 11 }; use MyClass; print "Testing to construct via 'start_stuff()'\n"; my $object_via_start = MyClass->start_stuff(); ok( ref( $object_via_start ), 'MyClass' ); ok( UNIVERSAL::isa( $object_via_start, 'MyClass' )); print "Testing to construct via 'init_stuff()'\n"; my $object_via_init = MyClass->init_stuff(); ok( ref( $object_via_init ), 'MyClass' ); ok( UNIVERSAL::isa( $object_via_init , 'MyClass' )); print "Testing to invoke 'start_stuff()' and 'use_stuff()' \n"; print "via object from 'init_stuff()'\n"; print "(Should not return a new object).\n"; my $start_stuff_after_init = $object_via_init->start_stuff(); my $start_stuff_after_use = $object_via_init->use_stuff(); ok( ref( $start_stuff_after_init ), 'MyClass' ); ok( UNIVERSAL::isa( $start_stuff_after_init , 'MyClass' )); ok( ref( $start_stuff_after_use ), 'MyClass' ); ok( UNIVERSAL::isa( $start_stuff_after_use , 'MyClass' )); ok( $object_via_init, $start_stuff_after_init ); ok( $object_via_init, $start_stuff_after_use ); ok( $start_stuff_after_init, $start_stuff_after_use );
So my question now is, are my testcases faulty? I would bet on that, since I am no expert at this, but no matter how I tried, I concluded that UNIVERSAL::isa() did not work.

But this might be why that approach doesn't work. From the docs of UNIVERSAL:

UNIVERSAL::isa ( VAL, TYPE )
isa returns true if one of the following statements is true.

The last point there being the catch. Since, when invoked for the first time, VAL will indeed be "MyClass" (the string). Which is what throws the exception you see above.

This did indeed bother me, so any ideas on why this is happening, or what I am doing wrong, or how I could redesign, would still be helpful.

Minor detail: Due to the way my constructor was finally written, it was well possible to fall off the end, that is why return undef was at the end.


In reply to Re: Re: Multiple "constructors", possible to call several. by Dog and Pony
in thread Multiple "constructors", possible to call several. by Dog and Pony

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.