in reply to Objects and references

I prefer to do something as such:
use Scalar::Util qw( blessed ); sub new { my $class = shift; return $class->clone( @_ ) if blessed $class; # Continue on with constructor. } sub clone { my $self = shift; return $self->new( @_ ) unless blessed $self; # Continue on with cloner. }

Otherwise, I document what my methods expect and laugh at the people who don't RTFM.


My criteria for good software:
  1. Does it work?
  2. Can someone else come in, make a change, and be reasonably certain no bugs were introduced?

Replies are listed 'Best First'.
Re^2: Objects and references
by jeanluca (Deacon) on Sep 30, 2005 at 20:09 UTC
    Ok, that makes sense! But all this can be solved if you do:
    package PackageName ; sub new { $class = shift ; return bless {}, PackageName ; }
    This way you don't care if $class is a reference or not!
    Luca
      This way you don't care if $class is a reference or not!

      And you don't allow people to subclass your class.

      Plus, if you're going to do that, do it right:

      sub new { return bless {}, __PACKAGE__; }

      My criteria for good software:
      1. Does it work?
      2. Can someone else come in, make a change, and be reasonably certain no bugs were introduced?
        Thanks for your comments!!!!
        Can you also give an example (or link) that demontrates the usage of subclasses.

        Luca