How can you create an object of a class in a package?

Answers were Sorted based on User's Feedback



How can you create an object of a class in a package?..

Answer / sh faisal

package Test;

sub new
{
$class = shift;
$self = {};

bless $self,$class; # By Mistake earlier i wrote it as
#$ref
return $self;
}
1;
# In perl Script

use Test; # Include the module you have created
$obj=new Test; # creating an onject of Class Test

Is This Answer Correct ?    4 Yes 0 No

How can you create an object of a class in a package?..

Answer / vipul dalwala

Say Pachage is My Package

package MyPackage;

sub method() {

}


Then U can create Object

$myobject = new MyPackage();

and u can call any method of object like:

$myobject->method();

Is This Answer Correct ?    3 Yes 1 No

How can you create an object of a class in a package?..

Answer / bhagwat gupta

--Like you do in any perl programme.

package PackageName;

use RelativePath::ModuleName;# The Class as per your questn
...
...

my $object = ModuleName->new();
...
...
1;

Is This Answer Correct ?    0 Yes 1 No

How can you create an object of a class in a package?..

Answer / shah faisal

package Test;

sub new
{
$class = shift;
$self = {};

bless $ref,$class;
return $ref;
}
1;
# In perl Script

use Test; # Include the module you have created
$obj=new Test; # creating an onject of Class Test

Is This Answer Correct ?    2 Yes 3 No

Post New Answer

More CGI Perl Interview Questions

How to read file into hash array ?

0 Answers  


Explain '->' in perl?

0 Answers  


What does this symbol mean '->'?

0 Answers  


Consider the following example #! /bin/perl use strict; sub sample { my @arr=(1,2,3,4); return @arr; } my ($a,$b,$c,$d) = &sample; print "$a\n$b\n$c\n$d\n"; In the above code, How can I get the $c without using the arguments such as $a,$b. I don't want to use any array to get the return values.

2 Answers  


How to convert strings into an array in perl?

0 Answers  






Explain splicing of arrays?

0 Answers  


while(my($key, $value) = each(%hash) ) { print "$key => $value\n"; } my($key, $value); while(($key, $value) = each(%hash) ) { print "$key => $value\n"; } What is the different between these two code in case of "my" usage ?

1 Answers  


How many types of operators are used in the Perl?

0 Answers  


Which functions in perl allows you to include a module file. State their differences.

0 Answers  


How can memory be managed in Perl?

0 Answers  


What are hashes?

0 Answers  


How to merge two arrays in perl?

0 Answers  


Categories