develooper Front page | perl.perl6.users | Postings from June 2020

Playing with protos and phasers

Thread Next
From:
Fernando Santagata
Date:
June 25, 2020 07:47
Subject:
Playing with protos and phasers
Message ID:
CAJ+jyGgO6ji32YwwwjiBh4tbTMrc0rxfmBxffdCM5JDbXpAucQ@mail.gmail.com
Hi *,
I was trying to see if one can delegate phasers to a proto, like this:

proto sub foo(|) {
  ENTER { say 'In' }
  LEAVE { say 'Out' }
  {*}
}
multi sub foo(Int $a) {
  $a
}
multi sub foo(Str $a) {
  $a
}

say foo(1);
say foo('hello');

Yes indeed, it outputs:

In
Out
1
In
Out
hello

But at first it didn't work, because I added a space on both sides of the
'*':

proto sub foo(|) {
  ENTER { say 'In' }
  LEAVE { say 'Out' }
  { * }
}

It outputs:

In
Out
*
In
Out
*

If I add a return:

proto sub foo(|) {
  ENTER { say 'In' }
  LEAVE { say 'Out' }
  return { * }
}

it outputs:

In
Out
-> ;; $_? is raw = OUTER::<$_> { #`(Block|94038346387904) ... }
In
Out
-> ;; $_? is raw = OUTER::<$_> { #`(Block|94038346388120) ... }

Why are spaces significant in this case?

If this behavior is intentional, can this be a trap worth being documented?

-- 
Fernando Santagata

Thread Next


nntp.perl.org: Perl Programming lists via nntp and http.
Comments to Ask Bjørn Hansen at ask@perl.org | Group listing | About