<?xml version="1.0" encoding="windows-1252"?>
<node id="289076" title="A simple example OO script for total beginners" created="2003-09-04 21:11:12" updated="2005-08-15 12:43:48">
<type id="956">
perltutorial</type>
<author id="288770">
bl0rf</author>
<data>
<field name="doctext">
&lt;h3&gt;Object Oriented Perl examples for OO beginners&lt;/h3&gt;
  I love Perl but its object-oriented facilities are downright screwy.&lt;br&gt;
I was afraid to touch OO with a ten foot pole until I learned&lt;br&gt;
how to use it from a C++ book ( yes, I am multilingual ). So&lt;br&gt;
as a result I could do OO in C++ but I was still scared of it in&lt;br&gt;
Perl.&lt;p&gt;

  A week ago or so, I took out the book &amp;quot;Programming The Perl&lt;br&gt;
DBI&amp;quot; from my local library. It had a very simple object-oriented&lt;br&gt;
script that encouraged me to start experiencing Perl OO.&lt;br&gt;
I wrote my first object-oriented module and I'd like to &lt;br&gt;share it with
you. I strongly recommend that you read the OO documentation&lt;br&gt;
that comes with perl ( &lt;i&gt;perltoot, perlobj&lt;/i&gt; ) before looking at the example&lt;br&gt;
scripts, but since the examples are only useful for OO novices I will&lt;br&gt;
try to comment the code as best as I can.&lt;p&gt;

&lt;h3&gt;My own Object Oriented Perl module&lt;/h3&gt;
&lt;code&gt;
#!/usr/bin/perl
# Number.pm, a number as an object

package Number;	# This is the &amp;quot;Class&amp;quot;

sub new		# constructor, this method makes an object
		# that belongs to class Number
{
my $class = shift;		# $_[0] contains the class name
my $number = shift;	# $_[1] contains the value of our number
			# it is given by the user as an argument
my $self = {};		# the internal structure we'll use to represent
			# the data in our class is a hash reference
bless( $self, $class );	# make $self an object of class $class

$self-&gt;{num} = $number;	# give $self-&gt;{num} the supplied value
			# $self-&gt;{num} is our internal number
return $self;		# a constructor always returns an blessed()
			# object
}

sub add		# add a number to our object's number
{
my $self = shift;	# $_[0] now contains the object on which the method
		        # was called (executed on)
my $add = shift;	# number to add to our number

$self-&gt;{num} += $add;	# add
return $self-&gt;{num};
# by returning our new number after each operation we could see
# its value easily, or we could use the dump() method which could
# show us the number without modifying its value.
}

sub subtract	# subtract from our number
{
my $self = shift;	# our object's internal data structure, as above
my $sub = shift;

$self-&gt;{num} -= $sub;
return $self-&gt;{num};
}

sub change	# assign new value to our number
{
my $self = shift;
my $newnum = shift;
$self-&gt;{num} = $newnum;
return self-&gt;{num};
}

sub dump	# return our number
{
my $self = shift;
return $self-&gt;{num};
}

1;		# this 1; is neccessary for our class to work
&lt;/code&gt;

A method ( all the subroutines in the script ) is a command that
you can execute on an object. It acts on the object's internal data,
which in this case is represented as a hash.

&lt;code&gt;$self = {};&lt;/code&gt;
$self is passed to bless() and it must be a reference to
whatever internal data structure we're using, it could be an array

&lt;code&gt;$self = [];&lt;/code&gt;
or a scalar reference.

&lt;h3&gt;Using the Object Oriented module&lt;/h3&gt;
Having the Number module is fine and dandy but if you cant
use it than...its useless.&lt;br&gt;Here is a little script that uses Number.pm

&lt;code&gt;
#!/usr/bin/perl
# useit.pl using the Number module

use Number;

$obj = Number-&gt;new(7);	# call the constructor w/a value
# $obj now contains our object from before. remember
# that new() returned it ( in the guise of $self )

print "the number is ",$obj-&gt;dump(),"\n";

$obj-&gt;add(3);		# call the add() method on our object
print "now the number is ",$obj-&gt;dump(),"\n";

$num = $obj-&gt;subtract(5);
print "now the number is $num\n";
# remember that subtract() returns the current value of our number
# so that $num has that value assigned to it.

$obj-&gt;change(999999);
print "number changed to ",$obj-&gt;dump(),"\n";

# NOTE:	if you include the object method inside the double
#	quoted string in print() then it wouldnt work, it'll return the
#	memory address of the object and "-&gt;dump()" appended
#	to it...

exit();
&lt;/code&gt;
This script works on the assumption that Number.pm ( the file&lt;br&gt;
containing our class, our module ) is either in the perl module&lt;br&gt;
directory or is in the same directory as itself. To load a module&lt;br&gt;
from somewhere else take a look at the @INC array in &lt;i&gt;perlvar&lt;/i&gt;.&lt;br&gt;&lt;p&gt;

The output of the script that uses Number.pm is this:&lt;br&gt;
the number is 7&lt;br&gt;
now the number is 10&lt;br&gt;
now the number is 5&lt;br&gt;
number changed to 999999&lt;p&gt;

To see this for yourself just type "perl" at the prompt, paste the code&lt;br&gt;
for Number.pm and then the code for useit.pl right after it onto the same&lt;br&gt;
screen ( this is called interactive-mode ). When your'e done just press the&lt;br&gt;
key combination Control-D and the code will execute. An interesting thing&lt;br&gt;
to remember is that Number.pm doesn't always need its own file, you&lt;br&gt;
can have Number.pm and useit.pl in the same file provided that you&lt;br&gt;
write the line "package Number;".&lt;p&gt;

Whew, that was fairly long. If you dont understand what my&lt;br&gt;
scripts do dont despair, copy them and run them, or read some&lt;br&gt;
OO Perl tutorials. If youre completely stumped then try tackling&lt;br&gt;
OO in C++ since it has a nicer interface to object-orientation&lt;br&gt;
than Perl. Perl is still the greatest though...&lt;p&gt;

- Jacob F&lt;p&gt;
&lt;a href="http://earth.prohosting.com/foreach/index.html"&gt;http://earth.prohosting.com/foreach/index.html&lt;/a&gt;</field>
</data>
</node>
