in reply to printing references
There isn't a direct, simpe way of retrieving the name of a variable from a reference to it. There is probably a package in the Devel::* group (Padwalker?) that could be used to do this.
There are a couple of ways that you could do this yourself.
my %lookup; $lookup{ \@CBR } = 'CBR'; ... sub my_sub{ my( $aref ) = @_; print "Using: $lookup{ $aref } array\n"; ... }
sub my_sub{ my( $aname, $aref, $size, $name, $bref, $size ) = @_; print "Using $aname and $bname\n"; ... }
Of the three, I think I prefer the second option, but in truth, I think that any is taking a hammer to crack a nut!
Why do you want to do this? If the idea is to log the name of the array in error messages, you should probably be referring to the array by some logical name pertinent to what the sub is doing with the contents of the array rather than the name of a specific instance.
If the idea is to give you an indication of where the sub was called from with bad data, then you should probably look at the caller function which will allow you to access various information about code that called your sub including the file, package, line, subroutine and others. It also has the ability to track back through several levels of caller, and can be used to provide a complete callback trace of the path through the code that was followed.
If your purpose is to provide good information for debugging purposes when your sub gets bad input, then you would probably find the Carp module really useful as it will do all the work of obtaining and formatting the callback trace information for you.
|
|---|