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

Hi all, let's consider

my $current_yummy_cake = "apple pie"
I'd like to have a function that does this :
say_var($current_yummy_cake); # prints : $current_yummy_cake = >apple pie<
To avoid having to type this all the time:
say "$current_yummy_cake= >" . $current_yummy_cake . "<";

It doesn't seem simple: the say_var() subroutine needs to retrieve the name of a variable which is declared in the scope just above. Is this possible ?

I am aware that there are other solutions such as
- having a shortcut in my (Vim) editor, to type this line for me.
=> I'd like to avoid ending up with hundreds of shortcuts.
- having a subroutine that takes the name of the variable, and the variable itself.
=> It'd be nice to avoid the redundancy.

If it's not achievable, no worries (^c^)

Replies are listed 'Best First'.
Re: How to retrieve the name of a variable ?
by toolic (Bishop) on Jul 18, 2012 at 23:59 UTC

      Perfect, thank you toolic !

Re: How to retrieve the name of a variable ?
by roboticus (Chancellor) on Jul 19, 2012 at 00:10 UTC

    mascip:

    In addition to toolic's suggestions, you could try Smart::Comments. If you're feeling adventurous, you could even use symbolic references. In general, not a great habit to get into, but for some tasks like this, they're not so bad.

    $ cat t.pl #!/usr/bin/perl # Prevent symbolic references, etc. in general use strict; use warnings; # Use our instead of my to ensure that the value is accessible our $boom = 'Kaboom!'; our $bam = 'Kerblam!'; + # Unfortunately, the call looks odd... say_var('boom'); say_var('bam'); sub say_var { # Turn off strict for now no strict; my $t = shift; print "\$$t = >$$t<\n"; } $ perl t.pl $boom = >Kaboom!< $bam = >Kerblam!<

    .... somehow I feel that I'll be roasted for suggesting such a thing ....

    ...roboticus

    When your only tool is a hammer, all problems look like your thumb.

      This IS a cool simple solution, thanks. I'll go for the CPAN one for now, though =)
      I don't remember why, i had decided to not use Smart::Comment at the time. I might re-change my mind.

        mascip:

        I'll go for the CPAN one for now, though =)

        Good call! I only suggested it because it was the only thing I ever thought that symbolic references might be good for. But when I saw that toolic had a better answer, I just didn't feel like throwing it away. (It's the first time I've used 'em, and the fact that you need to declare the variables with 'our' instead of 'my' made me bang my head a bit trying to figure out why I couldn't get it working...)

        ...roboticus

        When your only tool is a hammer, all problems look like your thumb.

        The main reason not to use Smart::Comments is that it's a source filter: it changes the code of your program between when perl loads and when your program compiles. (Or while your program compiles.) This can introduce bugs or unintended effects which would be very hard to debug.

        Of course, it's a well-written module and has been used by many without that happening, but the possibility still exists. Your choice if you want to take the risk.