note
afoken
<blockquote>The right hand side of s/// does not execute code unless you use the "e" modifier.</blockquote>
<p>Would you bet your money on that statement? I would not.</p>
<p>You are right, <c>s///</c> <i>usually</i> does not execute code without the <c>/e</c> modifier. But Perl would not be Perl if you could not mess around with that. Note that even if you could mess with perl, you probably should not.</p>
<p>I'll abuse a trick invented by Andrew Pimlott, that I saw first in the "Perl hardware store" talks ([http://perl.plover.com/yak/hw1/|1], [http://perl.plover.com/yak/hw-dcpm/|2], [http://perl.plover.com/yak/hw-nylug/|3], [http://perl.plover.com/yak/hw2/|4]) by [id://3737]:</p>
<c>
#!/usr/bin/perl
use strict;
use warnings;
sub TIEHASH
{
return bless {},$_[0];
}
sub FETCH
{
return $_[1];
}
tie my %X,__PACKAGE__;
my $foo='look! no eval: 3735928559';
$foo=~s/(\d+)/$X{sprintf '0x%08X',$1}/;
print $foo;
</c>
<c>
>perl eval.pl
look! no eval: 0xDEADBEEF
>
</c>
<p>But there is also a way without [doc://tie] magic. Dereferencing an anonymous array reference:</p>
<c>
#!/usr/bin/perl
use strict;
use warnings;
my $foo='look! no eval: 3735928559';
$foo=~s/(\d+)/${[sprintf '0x%08X',$1]}[0]/;
print $foo;
</c>
<c>
>perl eval2.pl
look! no eval: 0xDEADBEEF
>
</c>
<p>Both tricks (ab)use the fact that <c>s///</c> interpolates the replacement expression, unless you force it not to do so (using <c>'</c> as the delimiter instead of <c>/</c>).</p>
<p>Alexander</p>
<div class="pmsig"><div class="pmsig-747201">
--<br>
Today I will gladly share my knowledge and experience, for there are no sweeter words than "I told you so". ;-)
</div></div>
1194845
1194850