I am familiar with the caller function which can be used by a program to know where it was invoked, from which file, package etc. Is there an easy way for a program to know how deeply nested the caller was? The use case would be to add some debugging lines without having to explicitly count the nesting level.
I am not looking for a Stack Trace. I am trying to find how deeply nested a calling function is within the scope of another function.
For example:
sub my_debug {
my ($txt) = @_;
my ($package, $filename, $line) = caller;
# Is there a way to know how deeply nested the caller is?
my $level = ... # How to get this?
print "DEBUG: You are calling from package $package in $filename "
. "line $line nesting level $level: MSG: $txt\n";
}
sub badly_nested {
for my $i ( 1..10 ) {
# 1-level deep
for my $j ( 1 .. 10 ) {
# 2-levels deep
my_debug( "j is $j" );
for my $k ( 1 .. 10 ) {
# 3-levels deep
}
}
}
}