mascip has asked for the wisdom of the Perl Monks concerning the following question:
Hi all, let's consider
I'd like to have a function that does this :my $current_yummy_cake = "apple pie"
To avoid having to type this all the time:say_var($current_yummy_cake); # prints : $current_yummy_cake = >apple pie<
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 | |
by mascip (Pilgrim) on Jul 19, 2012 at 00:11 UTC | |
|
Re: How to retrieve the name of a variable ?
by roboticus (Chancellor) on Jul 19, 2012 at 00:10 UTC | |
by mascip (Pilgrim) on Jul 19, 2012 at 00:15 UTC | |
by roboticus (Chancellor) on Jul 19, 2012 at 00:19 UTC | |
by DStaal (Chaplain) on Jul 19, 2012 at 14:55 UTC | |
by mascip (Pilgrim) on Jul 19, 2012 at 15:38 UTC | |
by clueless newbie (Curate) on Jul 20, 2012 at 11:12 UTC |