perlmeditation
Zaxo
<p>Once more, a [id://300471|great answer has bit the dust in testing]. I wanted to give [jacques] a different answer than I did to [id://452026], namely,
<code>local *CORE::GLOBAL::die = sub (@) {
die scalar(localtime()), ' ', @_;
};</code>
but [die] keeps on dieing the same old way.
<code>$ perl -e'local *CORE::GLOBAL::die = sub (@) {CORE::die(scalar(localtime()), " ", @_);}; die "Foo!"'
Foo! at -e line 1.
$ </code></p>
<p>Apparently, [die] is not overridable. It has a prototype,
<code>$ perl -e'print prototype "CORE::die"'
@$ </code>
and it doesn't appear on [diotalevi]'s toke.c list in [id://300477]. Now how do we know which builtins are overridable? Is [die] somehow special-cased to not be overridden, as [glob] and [readline] are to permit it?</p>
<p>Overriding builtins is indeed a tricky business.</p>
<p><b>Added: </b>Thanks, [tye]++ and [leriksen]++. You showed me something I didn't really understand before. It looks like builtins are outside both lexical and dynamic scope, being resolved sometime while compiling. [local|Local]izing them has no effect, and the last-compiled override is the one which counts. It's still a little freaky:
<code>BEGIN {
*CORE::GLOBAL::warn = sub (@) {
CORE::warn(scalar(localtime), " ", @_);
};
warn "Foo!";
}
BEGIN {
*CORE::GLOBAL::warn = sub (@) {
CORE::warn( @_);
};
}
warn "Bar!"'
__END__
Foo! at -e line 1.
Bar! at -e line 1.</code>
It looks like BEGIN blocks are all compiled to a single parse tree before they run, but that's only guessing.</p>
<div class="pmsig"><div class="pmsig-82147">
<p>After Compline,<br/>Zaxo</p>
</div></div>