Re: Who called me?

2017-09-15 Thread ToddAndMargo

On 09/13/2017 04:56 PM, ToddAndMargo wrote:

Hi All,

I am trying to convert this from Perl 5:

 my $WhoCalledMe = ( caller(0) )[1];

I use it inside a sub to determine who called the sub.

How is this done in P6?

Many thanks,
-T



Follow up:

With a lot of help from the chat line, it transpires that
there is no easy way to do this.

The correct method is indeed
my $WhoCalledMe = ( caller(0) )[1];
but it is not yet implemented. See bug:

https://rt.perl.org/Public/Bug/Display.html?id=123826


This was the closest I could get:

$ perl6 -e 'sub x{say "x was called by: $(callframe(1).code.name)"}; sub 
y{x; say "y was called by: $(callframe(1).code.name)" }; y;'


x was called by: y
y was called by: 


Poop!


Re: Who called me?

2017-09-13 Thread Brandon Allbery
On Wed, Sep 13, 2017 at 8:15 PM, ToddAndMargo  wrote:

> Would this be what I am looking for?
>
>
> for 1..* -> $level {
> given callframe($level) -> $frame {
> when $frame ~~ CallFrame {
> next unless $frame.code ~~ Routine;
> say $frame.code.package;
> last;
> }
> default {
> say "no calling routine or method found";
> last;
> }
> }
> }
>
> And I can't put it in a pm6 or I would get the next
> level down.
>

Presumably you'd just have to go up another level (or more since you also
get frames for blocks). I'm unclear if you need to check if the code block
produced is from a sub.

  pyanfar Z$ 6 'sub foo { my $x = callframe(0); dd $x.code.name }; sub bar
{ foo() }; bar()'
  "foo"
  pyanfar Z$ 6 'sub foo { my $x = callframe(1); dd $x.code.name }; sub bar
{ foo() }; bar()'
  "bar"
  pyanfar Z$ 6 'sub foo { my $x = callframe(2); dd $x.code.name }; sub bar
{ foo() }; bar()'
  ""

-- 
brandon s allbery kf8nh   sine nomine associates
allber...@gmail.com  ballb...@sinenomine.net
unix, openafs, kerberos, infrastructure, xmonadhttp://sinenomine.net


Re: Who called me?

2017-09-13 Thread ToddAndMargo

On 09/13/2017 05:15 PM, ToddAndMargo wrote:

On 09/13/2017 05:01 PM, Brandon Allbery wrote:
On Wed, Sep 13, 2017 at 7:56 PM, ToddAndMargo > wrote:


I am trying to convert this from Perl 5:

 my $WhoCalledMe = ( caller(0) )[1];

I use it inside a sub to determine who called the sub.

How is this done in P6?


You want the callframe method. Note that it can be a bit more complex 
than perl 5's caller because there are more things that act like call 
frames.
https://docs.perl6.org/routine/callframe (but most of the actual 
documentation is at https://docs.perl6.org/type/CallFrame).


Hi Brandon,

Would this be what I am looking for?


for 1..* -> $level {
 given callframe($level) -> $frame {
 when $frame ~~ CallFrame {
 next unless $frame.code ~~ Routine;
 say $frame.code.package;
 last;
 }
 default {
 say "no calling routine or method found";
 last;
 }
 }
}


And I can't put it in a pm6 or I would get the next
level down.

Thank you for the help!

-T


Would there be a way of catching the next level up?
That way I could put it in a pm6


Re: Who called me?

2017-09-13 Thread ToddAndMargo

On 09/13/2017 05:01 PM, Brandon Allbery wrote:
On Wed, Sep 13, 2017 at 7:56 PM, ToddAndMargo > wrote:


I am trying to convert this from Perl 5:

 my $WhoCalledMe = ( caller(0) )[1];

I use it inside a sub to determine who called the sub.

How is this done in P6?


You want the callframe method. Note that it can be a bit more complex 
than perl 5's caller because there are more things that act like call 
frames.
https://docs.perl6.org/routine/callframe (but most of the actual 
documentation is at https://docs.perl6.org/type/CallFrame).


Hi Brandon,

Would this be what I am looking for?


for 1..* -> $level {
given callframe($level) -> $frame {
when $frame ~~ CallFrame {
next unless $frame.code ~~ Routine;
say $frame.code.package;
last;
}
default {
say "no calling routine or method found";
last;
}
}
}


And I can't put it in a pm6 or I would get the next
level down.

Thank you for the help!

-T


Re: Who called me?

2017-09-13 Thread Brandon Allbery
On Wed, Sep 13, 2017 at 7:56 PM, ToddAndMargo  wrote:

> I am trying to convert this from Perl 5:
>
> my $WhoCalledMe = ( caller(0) )[1];
>
> I use it inside a sub to determine who called the sub.
>
> How is this done in P6?
>

You want the callframe method. Note that it can be a bit more complex than
perl 5's caller because there are more things that act like call frames.
https://docs.perl6.org/routine/callframe (but most of the actual
documentation is at https://docs.perl6.org/type/CallFrame).

-- 
brandon s allbery kf8nh   sine nomine associates
allber...@gmail.com  ballb...@sinenomine.net
unix, openafs, kerberos, infrastructure, xmonadhttp://sinenomine.net


Who called me?

2017-09-13 Thread ToddAndMargo

Hi All,

I am trying to convert this from Perl 5:

my $WhoCalledMe = ( caller(0) )[1];

I use it inside a sub to determine who called the sub.

How is this done in P6?

Many thanks,
-T