On Tue, 11 Jan 2022, 16:53 Yamadaえりな, <yamoer...@gmail.com> 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?
>

Try putting the keyword "sub" in front of the block. You don't pass blocks,
you pass subroutine references. There is special syntaxes to coerce blocks
into sub refs, but they don't apply to method calls and have all kinds of
subtleties so you are better off avoiding them until you are far more
versed in perl.

$obj->run( sub { "hello world"} );

Almost modern perl programmers would not write:

  &{$block};

They would write

$block->();

You may want to check out perlmonks for these kinds of questions. This list
is for folks who maintain the various ports of perl not such as a help
forum.

Also see perlsub.

Good luck!
Yves

Reply via email to