develooper Front page | perl.perl5.porters | Postings from June 2021

Re: RFC 0004 - defer {} syntax

Thread Previous | Thread Next
From:
shmem
Date:
June 17, 2021 20:16
Subject:
Re: RFC 0004 - defer {} syntax
Message ID:
alpine.DEB.2.21.2106172202410.4388@mgm-net.de
From the keyboard of Paul "LeoNerd" Evans [17.06.21,20:49]:

> On Thu, 17 Jun 2021 14:35:27 -0500
> David Nicol <davidnicol@gmail.com> wrote:
>
>> really? you've said "defer <CODEREF>" repeatedly
>
> I hope I haven't ever said that. I have mentioned `defer { BLOCK }`.
>
> Blocks aren't coderefs.
>
> E.g. consider the way that  if(COND) { BLOCK }  is not a coderef.
>
> It's verymuch not a coderef - `caller()` can't see it, it doesn't get
> its own `@_`, you can't `return` from it, etc...

Well, a BLOCK passed to a sub is very likely a CODEREF - at least inside
the subroutine:

sub foo (&) {
     say $_[0];
     $_[0]->();
}
foo { say "calling foo()" }
__END__
CODE(0x55b2e63b2440)
calling foo()

So, a flow control block attached to if, for, while etc is not a CODEREF,
but one passed to a function is, e.g. sort { $b <=> $a }, also with map
although slightly different:

$f = sub { $_ x 2 };
say for map $f->(), qw(a b c);
__END__
aa
bb
cc

And since "defer" is a function, it takes a coderef.

0--gg-

-- 
_($_=" "x(1<<5)."?\n".q·/)Oo.  G°\        /
                               /\_¯/(q    /
----------------------------  \__(m.====·.(_("always off the crowd"))."·
");sub _{s./.($e="'Itrs `mnsgdq Gdbj O`qkdq")=~y/"-y/#-z/;$e.e && print}
Thread Previous | 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