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

Explain use of ‘my’ keyword in perl?

0 Answers  


Explain lexical variables.

0 Answers  


Demonstrate subroutines in perl with a simple example.

0 Answers  


How to make the following assignment, as arrayreference assignment ? my $arr_ref='[1,2,3,4,4,'elem']';

2 Answers  


What's the difference between /^Foo/s and /^Foo/?

0 Answers  


What can be done for efficient parameter passing in perl?

0 Answers  


Explain the arguments for perl interpreter.

0 Answers  


Explain the difference between declarations of 'my' and 'local' variable scope in perl?

0 Answers  


Explain chomp?

0 Answers  


How to add elements in a hash in perl?

0 Answers  


What is it meants by '$_'?

0 Answers  


Why do you use only Perl when there a lot of more languages available in market like C, Java?

1 Answers  


Categories