在Perl中,是否可能知道在双引号中被其值替换的变量?
假设你已经:-在Perl中,是否可能知道在双引号中被其值替换的变量?,perl,double-quotes,Perl,Double Quotes,假设你已经:- my $variable = 'Value'; my $text = "$variable is stored inside scalar variable.\n"; print "$text\n"; 现在,当您看到输出时,会发现值存储在标量变量中 我想得到的信息是,这里$variable被替换为'value'。可能吗 我实际上是想得到所有这些语句的列表,其中一个变量被替换为它的值。例如,假设文件1包含上述代码。文件2包含以下内容: print "Welcome to $w
my $variable = 'Value';
my $text = "$variable is stored inside scalar variable.\n";
print "$text\n";
现在,当您看到输出时,会发现值存储在标量变量中
我想得到的信息是,这里$variable被替换为'value'。可能吗
我实际上是想得到所有这些语句的列表,其中一个变量被替换为它的值。例如,假设文件1包含上述代码。文件2包含以下内容:
print "Welcome to $website!!";
文件3已-通过电子邮件发送打印输出
我正在尝试获取文件1中的$text=$变量存储在标量变量中的详细信息。\n;文件2已经打印了欢迎访问$website
文件3不计算在内。您不能这样做
守则:
我的$variable='Value';
my$text=$变量存储在标量变量中。\n
分配$text时,$变量将被其值替换。$text是一个文本字符串
更新我的答案
我想你会喜欢这个:
#!/usr/bin/perl
use warnings;
use strict;
package myVariable;
use overload
qw("") => \&asString,
qw(+) => \&append;
sub new (;+[$]){
my $class = shift;
my $self = {
'have_var' => 0,
'string' => '',
};
&init($self, @_);
bless $self,$class;
}
sub init {
my $class = shift;
for(@_) {
if(ref($_) eq ref(\"")) {
$class->{'have_var'}++;
$class->{'string'} .= $$_;
} else {
$class->{'string'} .= $_;
}
}
}
sub asString {
my $class = shift;
return $class->{'string'};
}
sub APPEND {
my $class = shift;
$class->init(@_);
}
sub HAVE_VAR {
my $class = shift;
return $class->{'have_var'};
}
package main;
my $variable_1 = "Value";
my $variable_2 = "newVal";
my $variable_3 = "otherVal";
my $text = myVariable->new(\$variable_1, " ", \$variable_2, " this is text! ", \$variable_3);
my $appendString = " appendString";
$text->APPEND(\$appendString);
print "text content is : [$text]\n";
print "there are ", $text->HAVE_VAR, " variables in \$text";
你会得到
text content is : [Value newVal this is text! otherVal appendString]
there are 4 variables in $text
你不能那样做
守则:
我的$variable='Value';
my$text=$变量存储在标量变量中。\n
分配$text时,$变量将被其值替换。$text是一个文本字符串
更新我的答案
我想你会喜欢这个:
#!/usr/bin/perl
use warnings;
use strict;
package myVariable;
use overload
qw("") => \&asString,
qw(+) => \&append;
sub new (;+[$]){
my $class = shift;
my $self = {
'have_var' => 0,
'string' => '',
};
&init($self, @_);
bless $self,$class;
}
sub init {
my $class = shift;
for(@_) {
if(ref($_) eq ref(\"")) {
$class->{'have_var'}++;
$class->{'string'} .= $$_;
} else {
$class->{'string'} .= $_;
}
}
}
sub asString {
my $class = shift;
return $class->{'string'};
}
sub APPEND {
my $class = shift;
$class->init(@_);
}
sub HAVE_VAR {
my $class = shift;
return $class->{'have_var'};
}
package main;
my $variable_1 = "Value";
my $variable_2 = "newVal";
my $variable_3 = "otherVal";
my $text = myVariable->new(\$variable_1, " ", \$variable_2, " this is text! ", \$variable_3);
my $appendString = " appendString";
$text->APPEND(\$appendString);
print "text content is : [$text]\n";
print "there are ", $text->HAVE_VAR, " variables in \$text";
你会得到
text content is : [Value newVal this is text! otherVal appendString]
there are 4 variables in $text
这将在读取变量时显示回溯。够近了吗
package Peekaboo;
require Tie::Scalar;
our @ISA = 'Tie::StdScalar';
use Carp 'cluck';
sub FETCH { cluck }
package main;
my $variable = 'Value';
tie $variable, 'Peekaboo';
my $text = "$variable is stored inside scalar variable.\n";
如果没有,那么您可能需要和。我不知道如何使用它们。每当读取变量时,都会显示回溯。够近了吗
package Peekaboo;
require Tie::Scalar;
our @ISA = 'Tie::StdScalar';
use Carp 'cluck';
sub FETCH { cluck }
package main;
my $variable = 'Value';
tie $variable, 'Peekaboo';
my $text = "$variable is stored inside scalar variable.\n";
如果没有,那么您可能需要和。我不知道如何使用它们。解析源代码可以让你半途而废
use PPI qw();
sub walk {
my ($ref) = @_;
if ($ref->can('children')) {
for my $c ($ref->children) {
if ($c->can('interpolations')) {
if ($c->interpolations) {
say sprintf "Found interpolations of variables at line %d column %d and the string was:\n%s\n", $c->line_number, $c->column_number, $c->content;
}
}
walk($c);
}
}
return;
}
walk(PPI::Document->new(\(join '', *DATA->getlines)));
__DATA__
my $variable = 'Value';
my $text = "$variable is stored inside scalar variable.\n";
print "$text\n";
结果:
Found interpolations of variables at line 3 column 12 and the string was:
"$variable is stored inside scalar variable.\n"
Found interpolations of variables at line 5 column 7 and the string was:
"$text\n"
从插值表达式得出变量名的结论并不简单。您的示例只有简单的标量,但请考虑:
"$foo[0]" # @foo single lookup
"@foo[0,1]" # @foo slice
"$foo{key}" # %foo single lookup
"@foo{qw(k1 k2)}" # %foo slice
表达式和引用链中的变量/表达式会使事情变得更加复杂。解析源代码会让您半途而废
use PPI qw();
sub walk {
my ($ref) = @_;
if ($ref->can('children')) {
for my $c ($ref->children) {
if ($c->can('interpolations')) {
if ($c->interpolations) {
say sprintf "Found interpolations of variables at line %d column %d and the string was:\n%s\n", $c->line_number, $c->column_number, $c->content;
}
}
walk($c);
}
}
return;
}
walk(PPI::Document->new(\(join '', *DATA->getlines)));
__DATA__
my $variable = 'Value';
my $text = "$variable is stored inside scalar variable.\n";
print "$text\n";
结果:
Found interpolations of variables at line 3 column 12 and the string was:
"$variable is stored inside scalar variable.\n"
Found interpolations of variables at line 5 column 7 and the string was:
"$text\n"
从插值表达式得出变量名的结论并不简单。您的示例只有简单的标量,但请考虑:
"$foo[0]" # @foo single lookup
"@foo[0,1]" # @foo slice
"$foo{key}" # %foo single lookup
"@foo{qw(k1 k2)}" # %foo slice
表达式和引用链中的变量/表达式使事情变得更加复杂。您已经在$variable中存储了值,那么为什么需要在$text中检查相同的值。这只是一种情况。正如我在最后一行中所说,我将在不同的文件中查找所有此类语句,并应列出一个列表。是的。我们需要一个简单的代码示例来理解这个问题。只要检查$variable是否没有任何值,perl就会在编译脚本时显示一个未初始化的警告。@Aiswarya是否要分析其他perl代码?您已经在$variable中存储了值,那么为什么需要在$text中检查相同的值。这是就一个案子。正如我在最后一行中所说,我将在不同的文件中查找所有此类语句,并应列出一个列表。是的。我们需要一个简单的代码示例来理解这个问题。只要检查$variable是否没有任何值,perl就会在编译脚本时显示未初始化的警告。@Aiswarya是否要分析其他perl代码?