Siaris
Simple Things
Syndicate: full/short
Siaris
Categories
General0
News2
Programming2
LanguageBits0
Perl50
Ruby10
VersionControl1
Misc1
Article Calendar
<= May, 2008
S M T W T F S
123
45678910
11121314151617
18192021222324
25262728293031
Search this blog

Key links
External Blogs
Brought to you by ...
Ruby
1and1.com

The Constructor (soot part III)

Andrew L. Johnson (First published by ItWorld.com 2001-06-14)

The basic framework of an OO module is simpler than a standard module because OO modules don’t export anything.

    package Slot.pm
    use strict;

    # any class data

    # class and object methods (subroutines) here

    1;
    __END__

Like a normal module you provide a package name and end with a true value. In between you define whatever class data (if any) you need, and write the subroutines that create an instance of the object (the ‘constructor’) or provide methods that the object can perform.

The only real difference between an object or class method and an ordinary subroutine is that perl does a little magic relating to the first argument the subroutine gets. Recall how methods (class or object) are called:

    my $obj = MyClass->new('argument');   # constructor: class method
    $obj->some_method('argument');        # object or instance method

You may also see methods called using the indirect object syntax:

    my $obj = new MyClass 'argument';

Most people avoid this calling form except for a class method such as the constructor. Either way, you need to know that first argument to the routine is the object itself (for an object method), or the class name (for a class method such as the constructor).

Typically the constructor is named new(), but that is only a convention. We wanted our constructor to be called with an optional argument defining how many credits to start with:

    my $slot = Slot->new(100);

So, our constructor will begin like:

    sub new {
        my $class   = shift;         # class name is first argument
        my $credits = shift || 100;
        # ... more stuff
    }

Now we have enough information to start construction of our object, but what is an object? An object is simply a reference to anything that you can take a reference to, and that reference is "blessed" into the class (we will get to that shortly). Quite often a hash reference is used because it provides a convenient way to have named attributes or properties in the object (attributes are the actual data the object stores). Our Slot object will need a few attributes: the number of credits remaining on the machine, the current betting amount, the spin result (what is displayed), and information regarding whether it was a winning spin and how much was paid. Our Slot module and constructor now look like:

    package Slot;
    use strict;

    sub new {
        my $class   = shift;
        my $credits = shift || 100;
        my $self    = { credits => $credits,
                        bet     => 1,
                        win     => 0,
                        paid    => 0,
                        spin    => undef,
                       };
        return bless $self, $class;
    }

    1;
    __END__

That’s it — we now have an OO module that creates an instance of a slot machine. It doesn’t do anything now because we haven’t created methods for it, but you can actually test this module:

    #!/usr/bin/perl -w
    use strict;
    use Slot;
    my $slot = Slot->new(100);
    print $slot->{credits},"\n";

Note, the $slot variable is really just a reference to a hash, and we have used it as an ordinary reference to access the ‘credits’ key. This is a Bad Thing(tm), and I only show it to you to demonstrate that the object is simply the hash reference we got back from the new() method. However, it isn’t really just a hash now, it has been "blessed". The bless() function is a perl built-in that associates a reference with a class — this is what will allow perl to find a given objects methods (remember, we didn’t import anything). Our Slot class doesn’t define any object methods yet, and that is the topic for next week’s discussion.

*****