nudged has asked for the wisdom of the Perl Monks concerning the following question:

$ cat ./coderref #!/usr/bin/perl package CodeRef; { use Object::InsideOut; my @code :Field :Std( Name => 'code', Private => 1 ) :Arg(Name => 'code', Mand =>1 ) :Type(CODE_REF); sub call() { my $self = shift; my $code = $self->get_code(); return $code->(@_); } } 1; #===== package main; sub sum { my $sum =0 ; foreach my $num (@_) { $sum += $num; } return $sum; } my $cref = CodeRef->new( code => \&sum ); $ ./coderef OIO::Args error: Bad value for initializer 'code': CODE(0x9d16cc0) Usage: Initializer 'code' for class 'CodeRef' must be an object or ref + of type 'CODE_REF' Package: main File: coderef Line: 35 Trace begun at coderef line 35
I don't see how mode code ref I could make the argument.

Replies are listed 'Best First'.
Re: Object::InsideOut with CODE_REF constructor argument
by Corion (Patriarch) on Jul 10, 2011 at 07:08 UTC

    You declare the type as CODE_REF - what is that supposed to do? It is nowhere documented within Object::InsideOut.

    Maybe if you use Type(CODE), you get a working type constraint for callbacks?

      No luck with Type(CODE) and various combinations of fn, &fn or \&fn as argument. Most frustrating.

        The following code works for me (in teh sense that Object::InsideOut doesn't immediately complain):

        #!/usr/bin/perl package CodeRef; use strict; { use Object::InsideOut; warn sprintf "Using %s version %s", 'Object::InsideOut::VERSION', $Object::InsideOut::VERSION ; my @code :Field :Std( Name => 'code', Private => 1 ) :Arg(Name => 'code', Mand =>1 ) :Type(CODE); sub call() { my $self = shift; my $code = $self->get_code(); return $code->(@_); } } 1; #===== package main; sub sum { my $sum =0 ; foreach my $num (@_) { $sum += $num; } return $sum; } my $cref = CodeRef->new( code => \&sum ); __END__ Using Object::InsideOut::VERSION version 3.81 at tmp.pl line 9.
      No luck with Type(CODE) and various combinations of fn, &fn or \&fn as argument. Most frustrating. Prefer O:IO if I can. Moose and I tend not to get along well.