FelixDaCat has asked for the wisdom of the Perl Monks concerning the following question:

Here's my code:
#!C:/Perl/bin/perl # # dbug.pl # @currTime = localtime(time); dBug_showArray(@currTime); exit; sub dBug_showArray { print "\n\n\Value of array '" . WHATISTHISVARIABLE . "' is:\n"; my (@lclarray) = @_; my $counter = 0; foreach (@lclarray) { print $counter . " = '" . $_ . "'\n"; $counter += 1; } print "\nPress [Enter] to continue "; my $dummy_var = <STDIN>; print "\n"; return; }
I'm looking for it to produce the output:
Value of array '@currtime' is: 0 = '33' 1 = '29' 2 = '8' 3 = '28' 4 = '10' 5 = '101' 6 = '3' 7 = '331' 8 = '0'
(So in case it's not clear, I'm looking for what goes in place of WHATISTHISVARIABLE). Thanks in advance for any and all help!

Replies are listed 'Best First'.
Re: Can I get the name of a variable passed to a subroutine?
by IlyaM (Parson) on Nov 28, 2001 at 22:10 UTC
    Short answer: it is impossible.

    Long answer: everything is possible if you really, really want. Take a look at function caller. It returns information about context of the current subroutine call. It can return many things. Among them it can return filename of script and line in it where your subroutine was called. In theory you can open your script, find that line, parse it and find how its arguments have been passed.

    Well, by why do you need it? Why just don't add another argument for dBug_showArray - string of message? Something like:

    sub dBug_showArray { my $msg = shift; print "\n\n\Value of array '$msg' is:\n"; ... ...

    so you can call it as

    dBug_showArray("@currTime", @currTime);
      Update:.

      There is small bug in my code:

      dBug_showArray("@currTime", @currTime);
      It should be
      dBug_showArray('@currTime', @currTime); ^^^^^ ^^^^^
      Thanks to clintp who have noticed it.
Re: Can I get the name of a variable passed to a subroutine?
by stefan k (Curate) on Nov 29, 2001 at 17:23 UTC
    Well,
    I think I'd turn the logic round: call that routine with the name of the variable:
    dBug_showArray("@currTime");
    and eval() that string in your subroutine.

    Hmm, one major flaw of this would be scoping: you'd need to really know that variable there, and that would mean you'd only be able to show global variables there. Maybe something could be done using closures, but since I don't exactly know what they are/look like I could be wrong.

      It almost useless. Unless you use global vars all time (very bad idea) it doesn't work because of scoping.

      --
      Ilya Martynov (http://martynov.org/)

Re: Can I get the name of a variable passed to a subroutine?
by Biker (Priest) on Nov 29, 2001 at 19:15 UTC

    I think you should be able to call dBug_showArray with a hash.

    dBug_showArray({my_array_reference=>\@my_array});
    Inside dBug_showArray you may use either keys or each to get at it.
    You may even send several arrays (by reference) to your debug routine.

      And does it help? You don't get string '@my_array' into this subroutine so it can't be printed. Or do you suggest to pass name of array variable as key? It is almost same as passing it as first argument as I suggested in my post.

      --
      Ilya Martynov (http://martynov.org/)

        "Or do you suggest to pass name of array variable as key?"

        That was my intention, yes. And that was what I tried to demonstrate in my little example. It may not have been crystal clear, though.

        "It is almost same as passing it as first argument as I suggested..."

        Almost the same is not the same. TIMTOWTDI.

        It wasn't my intention to step on your toes. Sorry if you feel it that way.

        f--k the world!!!!
        /dev/world has reached maximal mount count, check forced.