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 <http://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 <http://t1.pl>
Not a CODE reference at t1.pl <http://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


Attachment: OpenPGP_0x5F3C0261CDB2E752.asc
Description: OpenPGP public key

Attachment: OpenPGP_signature
Description: OpenPGP digital signature

Reply via email to