mhearse has asked for the wisdom of the Perl Monks concerning the following question:
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: printing references
by BrowserUk (Patriarch) on Sep 12, 2003 at 02:11 UTC | |
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. 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. Examine what is said, not who speaks.
"Efficiency is intelligent laziness." -David Dunham"When I'm working on a problem, I never think about beauty. I think only how to solve the problem. But when I have finished, if the solution is not beautiful, I know it is wrong." -Richard Buckminster Fuller If I understand your problem, I can solve it! Of course, the same can be said for you. | [reply] [d/l] [select] |
|
Re: printing references
by edoc (Chaplain) on Sep 12, 2003 at 02:04 UTC | |
Unless I've misunderstood your question entirely... without getting into any 'B::' modules which may/may not be able to get you there I don't see how or why you would need this. Your subroutine takes any args it gets and assigns them to variables of it's choosing.. it's irrelevant what the caller knew them as so you just need to hardcode the name of the array in your subroutine's error message. cheers, J | [reply] [d/l] |
|
Re: printing references
by asarih (Hermit) on Sep 12, 2003 at 02:24 UTC | |
Having said that, here's what I think: when you pass a reference to a subroutine, it doesn't get the name of the referent, but the reference itself. Internally, reference is an RV. RV does not have a field for the name of the referent but only the address. Here's the output: The important part is the first four lines. It says that what is shifted doesn't remember anything about the referent's name. So unless you pass the referent's name along with the reference itself to the subroutine, you can't do what you want to. Of course, I'd be happy to be wrong in this case. | [reply] [d/l] [select] |
|
Re: printing references
by tachyon (Chancellor) on Sep 12, 2003 at 06:09 UTC | |
First point why on earth pass \@CBRF3 and $#CBRF3 to the function why not just get the index of the las element of @CBRF3 in the function. That data seems totally redundent. Anyway to answer your question the answer is NO as the names of the passed arrays are not remembered by Perl (as shown). You can however get details on the caller, or call stack.
I expect knowing where the call(s) came from will be much more useful for debugging than knowing the array names. You can generate this info youself using caller. If you really need them names you will have to pass something like an array of array refs:
I suspect you will find the call stack more help. cheers tachyon s&&rsenoyhcatreve&&&s&n.+t&"$'$`$\"$\&"&ee&&y&srve&&d&&print | [reply] [d/l] [select] |
|
Re: printing references
by chromatic (Archbishop) on Sep 12, 2003 at 04:44 UTC | |
I'd rarely use this code, as the variable named CBRF3 makes me think you have at least two other arrays named CBRFn. I'd rather have them in a parent array, which would render their names pretty useless. Still, it seems like a good opportunity to push your error handling code up a level, 'cuz you need it at the caller, not the callee. | [reply] [d/l] [select] |
|
Re: printing references
by jonadab (Parson) on Sep 12, 2003 at 04:44 UTC | |
When an error is detected in the subroutine, I want to print out the name of the array Arrays don't have names. All they have is an indexed list of references to scalar values. Consider the following case:
What 'name' do you propose should be printed? If you use warn or carp or something along those lines, you can get Perl to tell you the line number where your routine was called, and then you can look at that line in the source and see how it was called (e.g., what arrays were passed in), but if you want your arrays to have names then you need something more elaborate than garden-variety Perl arrays, some kind of object that knows how to report its name.
| [reply] [d/l] [select] |