S
szr
Given the code and output below, I believe there to be a bug in caller,
specifically when called with a sole argument of 0 (zero.) In the output
below, I get back the subroutine (method), B::foo, which is located in
Package "B", inside file Test.pm, at line 10, yet caller(0) is yielding
Package "main", file main.pl, and line 8.
The latter location is where 'foo' was called /from/, but caller(0),
according to `perldoc5.10.0 -f caller`, whose first four elements are
defined as "$package, $filename, $line, $subroutine", states that:
The value of EXPR indicates how many call frames to go back
before the current one.
Therefore 0 should be the current frame, and while the subroutine is
correct, is appears the package, file, and line number (aka, the
location of said subroutine), is incorrect. I would have expected
caller(1) to return where it was called _from_, but caller(0) be giving
the location of the subroutine where caller was called.
If I am missing something, please let me know. I have thought though
this for a couple hours and read over that perldoc entry several times
before writing this.
Thank you.
###### Output #####
$ ./main.pl
[main, ./main.pl, 8, B::foo]
$ # shouldn't this be [B, ./Test.pm, 10, B::foo] ?
###################
##### Test.pm #####
package B;
use strict;
sub new {
my $this = shift;
return bless { }, ref($this) || $this;
}
sub foo {
my $this = shift;
return join (', ', (caller(0))[0..3]);
}
package D;
use strict;
our @ISA = qw(B);
sub new {
my $this = shift;
my $class = ref($this) || $this;
my $obj = $class->SUPER::new(@_);
return bless $obj, $class;
}
1;
###################
##### main.pl #####
#!/usr/bin/perl -w
use strict;
use lib '.';
use Test;
my $d = new D;
print "[", $d->foo(), "]\n";
###################
specifically when called with a sole argument of 0 (zero.) In the output
below, I get back the subroutine (method), B::foo, which is located in
Package "B", inside file Test.pm, at line 10, yet caller(0) is yielding
Package "main", file main.pl, and line 8.
The latter location is where 'foo' was called /from/, but caller(0),
according to `perldoc5.10.0 -f caller`, whose first four elements are
defined as "$package, $filename, $line, $subroutine", states that:
The value of EXPR indicates how many call frames to go back
before the current one.
Therefore 0 should be the current frame, and while the subroutine is
correct, is appears the package, file, and line number (aka, the
location of said subroutine), is incorrect. I would have expected
caller(1) to return where it was called _from_, but caller(0) be giving
the location of the subroutine where caller was called.
If I am missing something, please let me know. I have thought though
this for a couple hours and read over that perldoc entry several times
before writing this.
Thank you.
###### Output #####
$ ./main.pl
[main, ./main.pl, 8, B::foo]
$ # shouldn't this be [B, ./Test.pm, 10, B::foo] ?
###################
##### Test.pm #####
package B;
use strict;
sub new {
my $this = shift;
return bless { }, ref($this) || $this;
}
sub foo {
my $this = shift;
return join (', ', (caller(0))[0..3]);
}
package D;
use strict;
our @ISA = qw(B);
sub new {
my $this = shift;
my $class = ref($this) || $this;
my $obj = $class->SUPER::new(@_);
return bless $obj, $class;
}
1;
###################
##### main.pl #####
#!/usr/bin/perl -w
use strict;
use lib '.';
use Test;
my $d = new D;
print "[", $d->foo(), "]\n";
###################