我想做这样的事情:
my $text = "The owls are not what they seem.";
my $pattern = '(\s+)';
my $replacement = '-$1-';
$text =~ s/$pattern/$replacement/g;
Run Code Online (Sandbox Code Playgroud)
$ text应该是:The-owls- -are- -not- -what- -they--seem.
但当然更像是: - $ 1-owls- $ 1-are- $ 1-not-$ 1-what- $ 1-they-$ 1-seem.
我尝试了各种反向引用($ 1,\ 1,\ g {1},\ g1),但它们都没有用./ e修饰符也不起作用.这有可能吗?
目的是用这样的行改变对象内的一些文本:$ object-> replace('(.)oo','$ 1ar')
还有其他想法如何做到这一点?
非常感谢你.
package JustTesting;
use strict;
use warnings;
sub new {
my $self = {};
bless($self, shift);
END { $self->goodbye() };
return $self;
}
sub goodbye {
print "Goodbye.\n";
}
package main;
my $this = JustTesting->new();
Run Code Online (Sandbox Code Playgroud)
输出:
变量"$ self"不会在./test第10行保持共享.
再见.
显然它可以工作,我可以no
warnings在END块内抑制警告.但我想知道是否有更好的方法来做到这一点.
我尝试使用这样的匿名子:
my $cleanup = sub { $self->goodbye() };
END { $cleanup->() };
Run Code Online (Sandbox Code Playgroud)
然后像这样:
END { sub { $self->goodbye() }->() };
Run Code Online (Sandbox Code Playgroud)
但我总是得到同样的警告.