\&Foo::some_sub autovivifies a sub stub at run-time (just like sub Foo::some_sub; would at compile-time).
So it will work fine as long as we don't make any reference to the sub in any compiled code that comes before the definition.
use B;
$\="\n";
# line 99 'bin/some_code.pl'
{
no strict 'refs';
print B::svref_2object(eval q(\\*{'Foo::some_sub'}))->LINE;
print B::svref_2object(eval q(\\&Foo::some_sub))->GV->LINE;
}
eval q{Foo::some_sub()};
package Foo;
# line 23 'bin/some_file.pl'
sub some_sub {
warn "Got to here";
}
__END__
25
25
Got to here at 'bin/some_file.pl' line 24.
Of course, string eval has a code smell, but so does no strict 'refs'.
-
Are you posting in the right place? Check out Where do I post X? to know for sure.
-
Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
<code> <a> <b> <big>
<blockquote> <br /> <dd>
<dl> <dt> <em> <font>
<h1> <h2> <h3> <h4>
<h5> <h6> <hr /> <i>
<li> <nbsp> <ol> <p>
<small> <strike> <strong>
<sub> <sup> <table>
<td> <th> <tr> <tt>
<u> <ul>
-
Snippets of code should be wrapped in
<code> tags not
<pre> tags. In fact, <pre>
tags should generally be avoided. If they must
be used, extreme care should be
taken to ensure that their contents do not
have long lines (<70 chars), in order to prevent
horizontal scrolling (and possible janitor
intervention).
-
Want more info? How to link
or How to display code and escape characters
are good places to start.
|