A look at Perl

Okay,yesterday I went over a list of possible programming languages to learn.  It ended with me decided to learn all of them, but unsure as to what to learn first.  I decided to go over each of them and figure out what I thought about them.  How would I do this?  What I have decided to do is see how complex it is to do the same thing in each language.  What better to test out a language than to create a class?

My Control Class

I started out creating a basic VB.NET class – a Person class that simply introduces itself:

Public Class Person
   Private FirstName As String
   Private LastName As String
   Private Age As Integer
   Public Sub New(ByVal firstName As String, ByVal lastName As String, ByVal age As Integer)
      Me.FirstName = firstName
      Me.LastName = lastName
      Me.Age = age
   End Sub
   Public Function IntroduceYourself() as String
      Return "Hello, my name is " & FirstName & " " & LastName & ", and I am " & Age & " year(s) old."
   End Function
End Class

My Control Implementation

Dim dan As New Person("Dan", "Appleyard", 27)
Console.WriteLine(dan.IntroduceYourself())

I will compare this to each of the languages I will investigate. Now the Perl code!

Perl Class

package Person;

sub new
{
    my $class = shift;
    my $self = {
        _firstName => shift,
        _lastName  => shift,
        _age       => shift,
    };

    bless $self, $class;
    return $self;
}

sub IntroduceYourself {
	my( $self ) = @_;
	return "Hello, my name is $self->{_firstName} $self->{_lastName}, and I am $self->{_age} year(s) old."
}
1;

Now I am not going to explain every line in this code, just know that with my limited knowledge of Perl (as of now), this is how you make a class in Perl.

Perl Implementation

use Person;

$dan = new Person ("Dan", "Appleyard", 27);

print $dan->IntroduceYourself();

My Thoughts

Defining a class in Perl is quite different than VB.NET, wouldn’t you agree? What is with this $shift? What is the heck is this bless? If I decided to do Perl first, these are things I am going to have to pick up. Tomorrow, I am going to go over the same class implementation in Python. Until tomorrow then!

This entry was posted in Development, Learning, Perl and tagged , , . Bookmark the permalink.

Leave a Reply

Your email address will not be published.

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>