Perl Methods Calling


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Perl Methods Calling
# 1  
Old 04-11-2013
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
Code:
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
Code:
 #!/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..
# 2  
Old 04-11-2013
RedHat perl

Quote:
Originally Posted by adisky123
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
Code:
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
Code:
 #!/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

Code:
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
# 3  
Old 04-11-2013
oops

Hey!!!

Have a look at the below,

Cocoa.pm

Code:
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

Code:
#!/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 Smilie
# 4  
Old 04-11-2013
Can you please explain how this works
Code:
$this = {'a' =>shift,'b'=>shift,'c'=>shift};

# 5  
Old 04-11-2013
Quote:
Originally Posted by adisky123
Can you please explain how this works
Code:
$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.

Code:
 
$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 Smilie

Cheers,
Ranga Smilie
# 6  
Old 04-11-2013
Quote:
Originally Posted by adisky123
Can you please explain how this works
Code:
$this = {'a' =>shift,'b'=>shift,'c'=>shift};

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 shifts 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.
# 7  
Old 04-11-2013
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
Code:
$this = {'a' =>shift,'b'=>shift,'c'=>shift};

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

Regards
Adi
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Methods For Debugging Perl Problems

Note: Not a programmer by profession but occasionally have to program. I am looking for general methods and freely/readily available tools employed to debug problems during development of perl scripts. Anything that has really helped you out with problems you just couldn't find. A couple of... (5 Replies)
Discussion started by: Vi-Curious
5 Replies

2. Shell Programming and Scripting

Methods to SSH (Perl)...

Can anyone break down the different methods of using SSH in perl? I'm currently using Net::SSH::Expect, which allows me to login to a machine and execute multiple commands without having to ssh again. This feature of holding the session works well for me, but it's slow. If I set timeouts to 4... (3 Replies)
Discussion started by: mrwatkin
3 Replies

3. Shell Programming and Scripting

calling perl subroutine from perl expect module

All, Is it possible to call a subroutine from the perl expect module after logging to a system that is within the same program. My situation is I need to run a logic inside a machine that I'm logging in using the expect module, the logic is also available in the same expect program. Thanks,... (5 Replies)
Discussion started by: arun_maffy
5 Replies

4. Shell Programming and Scripting

Calling 3 perl script from one

hi all, I have 3 perl scripts a.pl,b.pl and c.pl each of these work when i pass a date for eg: perl c.pl 2010-05-27 now i want to write a perl script that would call the 3 scripts and make it run all the 3 scripts (a.pl,b.pl,c.pl) parallelly rather than 1 after the other....... pls... (2 Replies)
Discussion started by: siva_nagarajan
2 Replies

5. UNIX for Dummies Questions & Answers

Calling a c program using perl script

On bash I run precompiled c Program as follows: ./create_cust 1 10000 US S > us_cust.csv create_cust is a c program and requires 4 parameters. I am redirecting the output of this program to csv file I need to run this same program in perl I am aware of exec command though not... (7 Replies)
Discussion started by: gkbond
7 Replies

6. Shell Programming and Scripting

calling problem in perl script

Hi , Here is my piece of code-- main(); sub main { $result = GetOptions ("LogDir=s" => \$LogDir, "Summary" => \$Summary, "Indiviual=s" => \$Individual , "Diagnostics=s" => \$Diagnostics, ... (1 Reply)
Discussion started by: namishtiwari
1 Replies

7. Programming

calling Perl from C

Hi, I am trying to execute a perl script from c program. I tried using system command. system("perl test.pl filename") ; This perl program takes filename as input and prints a number to screen. I need to get that returned number in C program. system command is... (3 Replies)
Discussion started by: pkusumam
3 Replies

8. Shell Programming and Scripting

Calling Winzip from perl script

Hi, I would like to invoke "Winzip" utility from a perl script, input the name of zip file and provide output path for unzipped files. Any pointers will be appreciated. Thanks (5 Replies)
Discussion started by: MobileUser
5 Replies

9. Shell Programming and Scripting

calling a shell script from perl

Hi all, Not sure if this is the right forum to post query regarding perl script. I have a perl script which internally calls a shell script. My problem is that the shell script should be passed command line arguments. I call a shell script from perl using: system("sript.sh"); How do... (3 Replies)
Discussion started by: gurukottur
3 Replies

10. Shell Programming and Scripting

URL calling in PERL

All Please help me to call url in Perl. Ex: http://www.test.com/dynf?urn=123 Assume it will return success if 123 is in urn or it will return " failed". I want store this return type in a variable. Please help me to call the URL through PERL. Thanx in advance Regards Deepak (1 Reply)
Discussion started by: DeepakXavier
1 Replies
Login or Register to Ask a Question