Questions tagged [coderef]
10 questions
9
votes
3 answers
Why does Perl allow invoking coderefs on unblessed data structures?
When executing the statement $obj->method();, perldiag says that Perl needs to know what package the method belongs to. That's why it needs to be blessed:
Can't call method "%s" on unblessed reference
(F) A method call must know in what package…

Zaid
- 36,680
- 16
- 86
- 155
7
votes
2 answers
Why am I getting a syntax error when I pass a coderef to this prototyped Perl subroutine?
This is the code:
sub function($&) {
my $param1 = shift;
my $code = shift;
# do something with $param1 and $code
}
If I try to call it like this:
function("whatever") {
print "i'm inside the coderef\n";
}
I get Not enough arguments…

Geo
- 93,257
- 117
- 344
- 520
4
votes
3 answers
Which recommended Perl modules can serialize Moose objects?
I was usually using Storable with nstore, but now I have a module that has CODE and apparently Storable doesn't like that.
I found YAML (and YAML::XS which I can't really get to work).
I also experimented a bit with MooseX::Storage without much…

David B
- 29,258
- 50
- 133
- 186
2
votes
2 answers
Perl Mojolicious: Passing arguments to a code ref
In my Mojolicious Controller, I have:
my @promise;
foreach my $code (\&doit1, \&doit2,) {
my $prom = Mojo::Promise->new;
Mojo::IOLoop->subprocess(
sub {
my $r = $code->("Hello");
return $r;
},
sub {
my…

h q
- 1,168
- 2
- 10
- 23
2
votes
2 answers
Tangling knitr files that use an external code file
Is there a way to include the externaL code in the tangled file when I have:
<>=
@
(here xref is a reference to code in an external file)
or
<>=
<>
@
Or do I need to source the external file and somehow work from…

John Maindonald
- 171
- 5
1
vote
1 answer
How to get a code reference from a object method using Moo
I'm using Moo (OO) under Perl v5.26 and Linux.
I'm writing tests and have some kind of runtime dictionary in an object to store states of an application. I want to test if a defined code reference points to the adequate method in that object.
My…

huckfinn
- 644
- 6
- 23
1
vote
1 answer
passing a coderef to sort() problems
Using the following pieces of code, I get wildly different results.
$val{"ENOTE"} = 05;
$val{"WATCH"} = 10;
my %sortFunc = ();
my $sortFunc = sub {
my ($va, $vb);
$va = $val{$$a{"etype"} . ":" . $$a{"emsg"}} // $val{$$a{"etype"}};
$vb…

Erik Bennett
- 1,049
- 6
- 15
1
vote
5 answers
Is it possible to define anonymous subroutines in a hash constructor in Perl?
Is it possible to define anonymous subroutines in a hash constructor in Perl?
I'm trying to do something like this:
my %array = { one => sub { print "first $_[0]" },
two => sub { print "next $_[0]" },
three => sub {…

Jeremy Bourque
- 3,533
- 1
- 21
- 18
0
votes
1 answer
Error: "Cannot copy to ARRAY in scalar assignment"
The situation is that I am in a sub trying to use the caller's $a and $b, similar to what sort does. Most of the time it works, but while running unit tests I noticed in one case, inside one package, it did not work. I use caller to get the package…

user62177541
- 368
- 3
- 14
-2
votes
2 answers
C++ 4 In a row AlphaBeta algorithm is not very smart
I'm making an AI controlled alpha-beta algorithm for a school project, but my algorithm is very inconsistent. Sometimes it blocks all my moves successfully, and sometimes it just ignores my 3 in a row as seen here. How could this happen and how can…
user9558044