Emma Geller-Green
Emma Geller-Green

Reputation: 307

Creating a Class in Perl

package Person;
sub new
{
    my $class = shift;
    my $self = {
        _firstName => shift,
        _lastName  => shift,
        _ssn       => shift,
    };
    # Print all the values just for clarification.
    print "First Name is $self->{_firstName}\n";
    print "Last Name is $self->{_lastName}\n";
    print "SSN is $self->{_ssn}\n";
    bless $self, $class;
    return $self;
}

I got the above code from this site.

From the official Perl site on Classes:

Perl does not provide any special syntax for class definitions. A package is simply a namespace containing variables and subroutines.

  1. With that in mind, is the above code from Tutorial Point, the proper best practice when creating a class in Perl?

  2. In other OO languages you can throw exceptions from inside the constructor when a value is not set, could do you this as well inside Perl?

  3. Which is the proper way to bless?

The code in tutorial point does this:

bless $self, $class;

But from the perl documentation:

  my $self = bless {
      path => $path,
      data => $data,
  }, $class;
  return $self;

Upvotes: 2

Views: 346

Answers (2)

Dave Cross
Dave Cross

Reputation: 69264

  1. With that in mind, is the above code from Tutorial Point, the proper best practice when creating a class in Perl?

There is nothing inherently wrong with the code from Tutorial Point. It creates a perfectly adequate class. However, as others have pointed out, the Tutorial Point site is not highly regarded and you would be well advised to look elsewhere for better tutorials.

  1. In other OO languages you can throw exceptions from inside the constructor when a value is not set, could do you this as well inside Perl?

In Perl code, you throw an exception by calling die(). You catch an exception using eval and looking at the value of $@. Modules like Try::Tiny can make this process look more like other languages.

  1. Which is the proper way to bless?

The Perl slogan is "There's More Than One Way To Do It". Both of your code examples do the same thing. The only reasons to choose one over the other would be personal preference and in-house style.

However, many Perl programmers have moved away from this style of OO programming and are, instead, using Moose (or its cut-down relation Moo) for all of their OO code. You may well find that looking at these options makes your life easier.

Upvotes: 2

ikegami
ikegami

Reputation: 385809

Perl doesn't have object constructors. new is just a method like any other, and methods can throw exceptions.


bless returns its first argument, so

bless($ref, $class);
return $ref;

and

return bless($ref, $class);

are equivalent. Both are proper.


Personally, I create the object first, then add to it. This creates a parallel between the constructor in the base class

sub new {
   my ($class, ...) = @_;
   my $self = bless({}, $class);
   $self->{...} = ...;
   return $self;
}

and constructors in child classes

sub new {
   my ($class, ...) = @_;
   my $self = $class->SUPER::new(...);
   $self->{...} = ...;
   return $self;
}

But that's just my personal preference.

Upvotes: 5

Related Questions