PERL 5.8:: Need to have classes and objects

I need a sample for how to create and object of a class in perl. Pass this object to another class

Explanation
====
main code
=======
#!/usr/bin/perl

use utility;
use blobManage;


my $utility = new utility;

# How to pass a reference of utility object to blobManage

my $blMng = new blobManage;



==========================
blobManage.pm
================

package blobManage;

warn "blobManage is successfully loaded!\n";
1;
use utility;
use File::Copy;
# How to declare, set and use the reference of the utility object.
sub new {
       my ($class_name) = @_;

        my ($self) = {};

            #my ref $utility ;
#       warn "We just created our new variable...\n ";

        bless ($self, $class_name);
#        warn "and now it's a $class_name object!\n";

        $self->{'_created'} = 1;
        return $self;
}

EmadGirgisAsked:
Who is Participating?
 
Adam314Connect With a Mentor Commented:
The bless function turns a reference into an object.
http://perldoc.perl.org/functions/bless.html

If you want $utility to be available to the blobManage from the new function, you'll have to save it in the blobManage object.

If you just want one function of the blobManage object to use the utility object, you can pass it to that function.


Here are examples of both

############################## blobManage keeps a reference to the utility object
#create utility object
my $utility = new utility;
...
#You can pass the utility when creating the blobManage:
my $blMng = new blobManage($utility);
$blMng->doSomething(); 
 
 
 
package blobManage;
...
sub new {
    my ($class_name, $util) = @_;
    #now, $util is a reference to the same object as $utility in your main program
    
    my $self = {utility => $util};
    bless $self, $class_name;
    ...
    return $self;
}
 
sub doSomething {
    my $self = shift;
    $self->{utility}->doSomethingElse
}
 
############################## blobManage does not keep a reference to the utility object
############################## it gets the reference only for the functions that need it
 
#create utility object
my $utility = new utility;
...
#You can pass the utility when creating the blobManage:
my $blMng = new blobManage();
$blMng->doSomething($utility); 
 
 
 
 
package blobManage;
...
sub new {
    my ($class_name) = @_;
    #now, $util is a reference to the same object as $utility in your main program
    
    my $self = {};
    bless $self, $class_name;
    ...
    return $self;
}
 
sub doSomething {
    my ($self, $util) = @_;
    #do stuff with $self
    $util->doSomethingElse();
}

Open in new window

0
 
Adam314Commented:

#You can pass the utility when creating the blobManage:
my $blMng = new blobManage($utility);
 
 
 
 
package blobManage;
...
sub new {
    my ($class_name, $util) = @_;
    #now, $util is a reference to the same object as $utility in your main program

Open in new window

0
 
EmadGirgisAuthor Commented:
and $util is visible for all functions of the blobManage class

If I have a sub doSomething in blobManage

doSomething{

$util->doSomethingelse();
}
Would that work
0
 
EmadGirgisAuthor Commented:
Do I need to bless $utility

What is bless anyway? I
0
All Courses

From novice to tech pro — start learning today.