Make objects + methods

I would like to know if it's possible in Perl to make objest with it's own methods.
I would like to have a clear example of an object,a methods
an how to call this object?
 


LVL 1
PampaAsked:
Who is Participating?
 
prakashk021799Connect With a Mentor Commented:
OK.
0
 
prakashk021799Commented:
Tom's Object Oriented Tutorial is available in your perl documentation. Just do:

perldoc perltoot

It is also available at <http://language.perl.com/newdocs/pod/perltoot.html>
0
 
PampaAuthor Commented:
I know, but whta I need is a simple example of this 1 object with one method and the invocation of it.
0
Upgrade your Question Security!

Your question, your audience. Choose who sees your identity—and your question—with question security.

 
prakashk021799Commented:
You can also try:

Easy Perl5 Object Intro
<http://www.perl.com/CPAN/doc/FMTEYEWTK/easy_objects.html>

perlbot - Perl Bag'o Object Tricks
<http://www.perl.com/CPAN/doc/manual/html/pod/perlbot.html>

Recently published book "Object Oriented Perl" by Damian Conway
<http://www.perl.com/pub/p/Object_Oriented_Perl>
0
 
prakashk021799Commented:
# Here's the implementation of a simple object,
# with three methods (no including new)

package MyObject;

use Exporter;
@ISA = qw(Exporter);

sub new {
    my $self = {};
    my ($class, $value) = @_;
    $self->{VALUE} = $value || 0;
    bless $self;
    return $self;
}

sub value {
    my $self = shift;
    my $new_value = shift;
    if ($new_value) {
        $self->{VALUE} = $new_value;
    } else {
        return $self->{VALUE};
    }
}

sub increment {
    my $self = shift;
    $self->{VALUE}++;
}


sub decrement {
    my $self = shift;
    $self->{VALUE}--;
}


## Here's how to use the above package

package main;

my $obj1 = new MyObject; # create object with initial value 0
$obj1->value(5); # set the value
print "current value of obj1 = ", $obj1->value, "\n";
$obj1->increment();
print "value of obj1 after incrementing = ", $obj1->value, "\n";

my $obj2 = new MyObject(10); # create object with initial value 10
print "current value of obj2 = ", $obj2->value, "\n";
$obj1->decrement();
print "value of obj2 after decrementing = ", $obj2->value, "\n";
0
 
PampaAuthor Commented:
1) What it means :

use Exporter;
@ISA = qw(Exporter);

2) The word "shift" in the methods.
0
 
prakashk021799Commented:
1. The Exporter module provides a mechanism to export functionality (methods and variables) from a package.

The @ISA array is a list of modules where a perl looks for object methods. It provides an inheritance hierarchy for your classes. Assume that class A is derived from another class B. If you call a method 'm' on an object of class A, then perl looks for this method in class 'A'. If it cannot find it there, it will automatically looks in class 'B' (provided you included 'B' in the @ISA list in the implementation for class 'A').

These are the standard lines one would add whenever you write a package.

2. 'shift' is a perl builtin function, which removes the first element from a list and returns it.

If you don't specify the array name as a parameter to 'shift', then it will remove the element from @_.
0
 
PampaAuthor Commented:
Ok lock the quetion so I can give you the points
0
All Courses

From novice to tech pro — start learning today.