develooper Front page | perl.perl6.language | Postings from June 2005

caller with no sub scope

Gaal Yahas
June 17, 2005 23:27
caller with no sub scope
Message ID:
In Perl 5, "caller EXPR" returns undef if the caller is top-level code. If
you need it, you can still get file, line, and package information
(this last is important for "import" subs) by using "caller" with the
EXPR-less form.

How do you do that in Perl 6? I'd been working with this presumed

     multi sub caller (Class ?$kind = Any, Int +$skip = 0, Str +$label)
             returns Control::Caller is primitive is export is safe { ... }

And an implementation which *always* expected a VCode frame (to check
$kind against). Obviously, this leaves no way to peek at frames with no
&?SUB, i.e., the top level.

The hacky way to solve this is to stipulate that kind=>Any actually
means something so general that it matches things that are not VCode
at all. The cleaner way in terms of formalism is to allow $kind to be
undef (actually, *default* to it), in which case it functions as that
wider-than-Any qualifier.

I'd go with the second way, since I think it leaves us with DWIMmy
usage. This will start working, which is good:

     sub import() {     # (do we still have that?)
         die "I'm not speaking with you!" if caller().package ~~ Rival;

Anyone spot any problems?

Gaal Yahas <> Perl Programming lists via nntp and http.
Comments to Ask Bjørn Hansen at | Group listing | About