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?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

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
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
Cloud Class® Course: Python 3 Fundamentals

This course will teach participants about installing and configuring Python, syntax, importing, statements, types, strings, booleans, files, lists, tuples, comprehensions, functions, and classes.

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
prakashk021799Commented:
OK.
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Perl

From novice to tech pro — start learning today.