Solved

PERL 5.8:: Need to have classes and objects

Posted on 2008-10-02
4
186 Views
Last Modified: 2012-05-05
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;
}

0
Comment
Question by:EmadGirgis
  • 2
  • 2
4 Comments
 
LVL 39

Expert Comment

by:Adam314
ID: 22628577

#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
 

Author Comment

by:EmadGirgis
ID: 22628608
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
 

Author Comment

by:EmadGirgis
ID: 22628641
Do I need to bless $utility

What is bless anyway? I
0
 
LVL 39

Accepted Solution

by:
Adam314 earned 500 total points
ID: 22629130
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

Featured Post

Does Powershell have you tied up in knots?

Managing Active Directory does not always have to be complicated.  If you are spending more time trying instead of doing, then it's time to look at something else. For nearly 20 years, AD admins around the world have used one tool for day-to-day AD management: Hyena. Discover why

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
PHP equivalent of opening a com object 5 83
Add additional column to .csv using Perl. 8 140
Perl string filter 5 79
how to print sub line with  sed /awk /perl in bash file 11 24
On Microsoft Windows, if  when you click or type the name of a .pl file, you get an error "is not recognized as an internal or external command, operable program or batch file", then this means you do not have the .pl file extension associated with …
I have been pestered over the years to produce and distribute regular data extracts, and often the request have explicitly requested the data be emailed as an Excel attachement; specifically Excel, as it appears: CSV files confuse (no Red or Green h…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
A short tutorial showing how to set up an email signature in Outlook on the Web (previously known as OWA). For free email signatures designs, visit https://www.mail-signatures.com/articles/signature-templates/?sts=6651 If you want to manage em…

770 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question