Perl caller function is used to find a place "From where the particular function is getting called"
D:\Dinesh.Y\Scripting\Perl\OOPS\1>c:\Perl\bin\perl.exe
$VAR1 = 'main';
$VAR2 = 'employee.pl';
$VAR3 = 24;
$VAR1 = 'Employee';
$VAR2 = 'Employee.pm';
$VAR3 = 39;
Name:Khurt Williams
Address : 10 Anywhere Lane , Anytown, NJ, 12345
# employee.pl
#!/usr/bin/perl
use strict;
use warnings;
use diagnostics;
use Employee;
#create Employee class instance
my $khurt = new Employee();
#set object attributes
$khurt->firstName('Khurt');
$khurt->lastName('Williams');
$khurt->id(1001);
$khurt->title('Executive Director');
$khurt->address( new Address() );
$khurt->address->street('10 Anywhere Lane');
$khurt->address->city('Anytown');
$khurt->address->state('NJ');
$khurt->address->zip('12345');
#diplay Employee info
$khurt->print();
# -------------------------------------------------------------------------------------------------
# class Employee
package Employee;
use Person;
use strict;
use Data::Dumper;
our @ISA = qw(Person); # inherits from Person
#constructor
sub new {
my ($class) = @_;
#call the constructor of the parent class, Person.
my $self = $class->SUPER::new();
$self->{_id} = undef;
$self->{_title} = undef;
bless $self, $class;
return $self;
}
#accessor method for id
sub id {
my ( $self, $id ) = @_;
$self->{_id} = $id if defined($id);
return ( $self->{_id} );
}
#accessor method for title
sub title {
my ( $self, $title ) = @_;
$self->{_title} = $title if defined($title);
return ( $self->{_title} );
}
sub print {
my ($self) = @_;
print Data::Dumper::Dumper(caller());
# we will call the print method of the parent class
$self->SUPER::print;
$self->address->print;
}
1;
# -------------------------------------------------------------------------------------------------
#class Person
package Person;
use strict;
use Address; #Person class will contain an Address
#constructor
sub new {
my ($class) = @_;
my $self = {
_firstName => undef,
_lastName => undef,
_ssn => undef,
_address => undef
};
bless $self, $class;
return $self;
}
#accessor method for Person first name
sub firstName {
my ( $self, $firstName ) = @_;
$self->{_firstName} = $firstName if defined($firstName);
return $self->{_firstName};
}
#accessor method for Person last name
sub lastName {
my ( $self, $lastName ) = @_;
$self->{_lastName} = $lastName if defined($lastName);
return $self->{_lastName};
}
#accessor method for Person address
sub address {
my ( $self, $address ) = @_;
$self->{_address} = $address if defined($address);
return $self->{_address};
}
#accessor method for Person social security number
sub ssn {
my ( $self, $ssn ) = @_;
$self->{_ssn} = $ssn if defined($ssn);
return $self->{_ssn};
}
sub print {
my ($self) = @_;
print Data::Dumper::Dumper(caller());
#print Person info
printf( "Name:%s %s\n\n", $self->firstName, $self->lastName );
}
1;
# -------------------------------------------------------------------------------------------------
#class Address
package Address;
use strict;
#constructor
sub new {
my ($class) = @_;
my $self =
{ _street => undef, _city => undef, _state => undef, _zip => undef ,road=>undef};
bless $self, $class;
return $self;
}
#accessor method for street
sub street {
my ( $self, $street ) = @_;
$self->{_street} = $street if defined($street);
return ( $self->{_street} );
}
#accessor method for city
sub city {
my ( $self, $city ) = @_;
$self->{_city} = $city if defined($city);
return ( $self->{_city} );
}
#accessor method for state
sub state {
my ( $self, $state ) = @_;
$self->{_state} = $state if defined($state);
return ( $self->{_state} );
}
#accessor method for zip
sub zip {
my ( $self, $zip ) = @_;
$self->{_zip} = $zip if defined($zip);
return ( $self->{_zip} );
}
sub print {
my ($self) = @_;
printf( "Address : %s , %s, %s, %s\n",$self->street, $self->city, $self->state, $self->zip );
}
1;
No comments:
Post a Comment