Re: Problem with my code for passing block
Yamadaえりな <[email protected]> Tue, 11 Jan 2022 17:14:18 +0800
| Newsgroups | gmane.comp.apache.mod-perl |
|---|---|
| Message-ID | <CADnCw10JOSw45ra8o-_PxPrDxE4+gryK-g-kXVrA3B+-_0heow@mail.gmail.com> |
Thanks for all your help.
Yes I have made the mistake to think {} is a code reference in perl.
私はすでに理解していますありがとう
On Tue, Jan 11, 2022 at 5:10 PM Jacques Deguest <[email protected]> wrote:
> Yamada-san,
>
> The value you pass to 'run' is not a code reference, but an hash
> reference, i.e. '{}'
>
> A code reference would be: 'sub{}', so do '$obj->run( sub{ "hello world"}
> );' instead.
>
> Jacques
>
> On 2022/01/11 17:51, Yamadaえりな wrote:
>
> Good afternoon,
>
> Can you help check my problem with this?
>
> $ cat t1.pl
> use strict;
>
> package Myclass;
>
> sub new {
> my $self = shift;
> bless {},$self;
> }
>
> sub run {
> my $self = shift;
> my $block = shift;
> &{$block};
> }
>
> 1;
>
> package main;
>
> my $obj = Myclass->new;
> $obj->run( { "hello world"} );
>
> $ perl t1.pl
> Not a CODE reference at t1.pl line 13.
>
>
> It seems I can't pass a code block to the caller. Where am I doing wrong?
>
> Sorry I have the background of other language, such as in scala I always
> do:
>
> scala> def strTrans(s:String)(f:String=>String) = f(s)
> strTrans: (s: String)(f: String => String)String
>
> scala> strTrans("katorena"){s=>s.reverse}
> res0: String = anerotak
>
> scala> strTrans("katorena"){s=>s.take(4)}
> res1: String = kato
>
> In above code {s=>s.reverse} and {s=>s.take} are the anonymous func (or
> code block) I passed to the caller.
>
> Thank you in advance.
> Yamada
>
>
>