Results 1 to 4 of 4
Hi,
I've a code. I am unable to understand below code. Can anyone help me in understanding the belwo code. What the (caller 1)[3] means in below code.
package CD::Music;
...
- 01-17-2008 #1Just Joined!
- Join Date
- Jun 2006
- Posts
- 40
what does (caller 1)[3] in Perl
Hi,
I've a code. I am unable to understand below code. Can anyone help me in understanding the belwo code. What the (caller 1)[3] means in below code.
package CD::Music;
use strict;
use Carp;
sub read_only
{
croak "Can't change value of read only attribute".(caller 1)[3] if @_ > 1;
}
#read only accessors
sub name { &read_only; $_[0]->{_name} }
sub artist { &read_only; $_[0]->{_artist} }
sub publisher { &read_only; $_[0]->{_publisher} }
sub ISBN { &read_only; $_[0]->{_ISBN} }
#read write accessors
sub last_played
{
my ($self, $when) = @_;
$self->{_played} = $when if @_ > 1;
$self->{_played};
}
sub rating
{
my ($self, $rating) = @_;
$self->{_rating} = $rating if @_ > 1;
$self->{_rating};
}
Suppose all the above read only accessors are called, then what will be the output of read_only subroutine each time, it is called. I just want to know the output by (caller 1)[3]. I am not familiar with this term.
Is caller 1 is printing the calling function's name or something else....please help....I'll appreciate your help..in this regard.
Thanks & Regards
Pawan Sangal
- 01-17-2008 #2
Do this at the command prompt:
and you will get documentation on every Perl function, including caller. I just looked, and caller's in there.Code:man perlfunc
If that man page is not installed on your system, you can google for this:
instead.Code:man perlfunc Linux
Hope this helps.--
Bill
Old age and treachery will overcome youth and skill.
- 01-17-2008 #3
- 01-23-2008 #4Just Joined!
- Join Date
- Jun 2006
- Posts
- 40
Hi Bill,
I think I have got the meaning of the (caller 1)[3] in above program.
Actually, the (caller 1)[3] will give us the name of the calling subroutine.
I read little theory on caller, and found the use of caller function. Every time Perl calls a subroutine, details of that subroutine are stored in a stack, which is for Perl's internal use.
When caller called on scalar context:
$package = caller; #only the package name is retreived
When caller called on list context:
($package, $file, $line) = caller; #retreives the package, filename, line of the current subroutine
It's same writing:
($package, $file, $line) = caller;
-- or --
($package, $file, $line) = caller 1;
Since, @caller = caller 0; #will return whole information about the calling subroutine. around 10 items.
Hence, to know the calling subroutine's name in a program can be written in two ways:
($package, $file, $line, $subrout) = caller 1; print "$subrout\n";
-- or --
$subrout = (caller 1)[3]; print "$subrout\n";


Reply With Quote
