在 Perl 中获取变量名作为字符串 [英] Get variable name as string in Perl

查看:110
本文介绍了在 Perl 中获取变量名作为字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试获取变量名称的文本表示.例如,这将是我正在寻找的功能:

I am trying to get a text representation of a variable's name. For instance, this would be the function I am looking for:

$abc = '123';
$var_name = &get_var_name($abc); #returns '$abc'

我想要这个是因为我正在尝试编写一个递归输出传递变量内容的调试函数,我希望它事先输出变量的名称,所以如果我连续调用这个调试函数 100 次,将没有关于我在输出中查看哪个变量的困惑.

I want this because I am trying to write a debug function that recursively outputs the contents of a passed variable, I want it to output the variable's name before hand so if I call this debug function 100 times in succession there will be no confusion as to which variable I am looking at in the output.

我听说过 Data::Dumper,但我不是它的粉丝.如果有人能告诉我如何获取变量名称的字符串,那就太好了.

I have heard of Data::Dumper and am not a fan. If someone can tell me how to if it's possible get a string of a variable's name, that would be great.

谢谢!

推荐答案

数据::Dumper::简单

use warnings;
use strict;
use Data::Dumper::Simple;

my $abc = '123';
my ($var_name) = split /=/, Dumper($abc);
print $var_name, "\n";

__END__

$abc

这篇关于在 Perl 中获取变量名作为字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆