Perl Methods Calling

Hello
I am on my way to improve my wonderful Perl skills, I got an issue which I want to share with you all. I have a Perl module which looks like

package Cocoa;

 require Exporter;
@ISA = qw(Exporter);
 
 my $a='';
 my $b='';
 my $c='';
 
 sub new {
     my $this = shift;  # Create an anonymous hash, and #self points to it.
     my $class = ref($this) || $this; 
     my $this = {};
     bless $this,$class;       # Connect the hash to the package Cocoa.
     return $this;     # Return the reference to the hash.

     }
 1;
 
 sub doInitialization {
    $a="1";
     $b="1";
     $c="3";
 }

and I want to print the value of variables a, b and c. I am trying below but fails

 #!/usr/bin/perl
 use Cocoa;
$cup = new Cocoa;
$cup->doInitialization();
print "$cup->{$a}\n";

It does not print any thing. Also it does not throw any error..

Try to use printf

printf "output: %d\n",$cup->{a};
or
printf "output: %d\n",$cup->{$a};
or 
printf "output: %s\n",$cup->{a};
or 
printf "output: %s\n",$cup->{$a};

i do not have time to test but one them should work.

best of luck

Hey!!!

Have a look at the below,

Cocoa.pm

require Exporter;
@ISA = qw(Exporter);
our $c = ' I am a global data';
sub new {
     my $this = shift;  # Create an anonymous hash, and #self points to it.
     my $class = ref($this) || $this;
     $this = {'a' =>shift,'b'=>shift,'c'=>shift};
     bless $this,$class;       # Connect the hash to the package Cocoa.
     return $this;     # Return the reference to the hash.
}
sub doInitialization {
    my $this = shift;
    $this->{'a'}="1";
    $this->{'b'}="2";
    $this->{'c'}="3";
    $c = join(' ', $c, 'Modified in doInitialization');
    print "ACCESSING main name space's object 'p': $main::p\n"; # 
 }
1;

callmodule.pl

#!/usr/bin/perl
use Cocoa;
our $p = 'Main namespace data';
 
$cup = new Cocoa;
 
print "PRINT : B4 doInitialization call", $Cocoa::c,"\n"; ## Invoke global data from Cocoa.pm, So here $Cocoa represents the namespace.
$cup->doInitialization();
 
print "$cup->{'a'}\n"; # 
 
print "PRINT After doInitialization called:", $Cocoa::c,"\n";

Is this make sense?

Cheers,
Ranga :slight_smile:

Can you please explain how this works

$this = {'a' =>shift,'b'=>shift,'c'=>shift};

This will receive the argument passed when the object creation and associate this to that class, when you bless.

 
$cup = new Cocoa(1,2,3); # you can even pass the data from main program and that will be updated in doInitialization().
 
Just FYI.
 
We can even create object with the below syntax.
 
$cup = Cocoa->new(1,2,3);

'my' variables are not accessible unless you blessed to your object or make that variable to 'our'. But 'our' variables exist through out the program and occupy some memory space.

So, If you want you program to be more optimized, you should return the objects(variables/hash/array) from you function to the main program and use from there.

Does this makes sense?

Please use code tags :slight_smile:

Cheers,
Ranga :slight_smile:

That is evaluating 'a' =>shift,'b'=>shift,'c'=>shift in list context, storing it in an anonymous hash, and returning a reference to this hash (through the anonymous hash composer/constructor {} ) to be assigned later to the scalar variable $this .

=> is just a fancy comma. The 3 shift s get the arguments passed to the class' constructor new in that order. That is, first argument will be associated with key a , second with key b , and so on. If no arguments are passed to the constructor (apart from the default class name or object reference, if invoked as a method), the corresponding keys' values will be undef .

By the way, your Perl skills are less than wonderful. There are a lot of questionable/wrong things in your code. E.g., returning 1 in the module file at the wrong place, multiple lexical declarations with the same symbol and type in the same lexical scope, etc.

I do not mean like this..
My question is -:
For every function I declare in the package Do I have to declare in the new method also.
Like If I remove

$this = {'a' =>shift,'b'=>shift,'c'=>shift};

Then the initialization method fail to work.
I hope you understand my point

Regards
Adi

It seems that you haven't understood the concept of objects and classes in Perl.

In your original code, the variables $a , $b , $c are lexically scoped to the file containing the package declaration (assuming that nothing else exists in the file).

An object is an instance of a class. Since those variables belong to the class, they are shared/accessible by all the objects of the class as well as the class itself. All associated objects and the class will refer to same copies of the variables.

When you are saying print "$cup->{$a}\n"; , you are accessing the hash whose reference (hard) is contained in the variable $cup . This may be a simple reference, not necessarily an object (a blessed reference). Such access also breaks encapsulation of the object; one is supposed to access the object's data only through the class' (or its ancestors') methods and not directly (even if you happen to know the internal data structure).

Your constructor is just creating an empty hash and your initialization method is just assigning to some lexicals which are not part of any object. So, don't expect the print to work at all.

My Advice : Get a good book on Object-oriented Perl and learn the basics!

Thanks..Can you please refer me some good books..