Problem with my code for passing block

Yamadaえりな <[email protected]> Tue, 11 Jan 2022 16:51:33 +0800
Newsgroups gmane.comp.apache.mod-perl
Message-ID <CADnCw13WGQL8RmGVQyu8JrvRsr-sFZ5H5r7jOYFD13-037U+tA@mail.gmail.com>
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