I'm working on a new module that exports some interesting functions. These functions describe other functions or variables with a closure, so naturally I'd assume the syntax to be something as light and snazzy as this:
somefunction foo { $bar++ };
sub foo {... };
Or, if describing a variable:
somefunction $foo { $bar++ };
Turns out that you can't do this, as the documentation clearly states: An & requires an anonymous subroutine, which, if passed as the first argument, does not require the "sub" keyword or a subsequent comma.
Hopefully, my final syntax will appear like the following:
# for subroutines
somefunction foo => sub { $bar++}
# for lvalues
somefunction $foo => sub { $bar++ }
It's a little repetitive with the equal-arrows and sub keyword, but at least I've used prototypes (specifically, *&) to save the typing of excess parenthesis.
Semicolon (Score:2)
There's no escaping it, except maybe via source filtering. (Did I just hear something heavy falling onto the floor?)
Yes, I admit it, I've been thinking along the same lines a long time ago.
Re:Semicolon (Score:1)
And no, no source filtering. (The axe that fell missed you by a few inches. Whew!)
qw(Ian Langworth)
Re: (Score:1)
You can omit the
subkeyword only when the closure is the first thing in the parameter list.The usual workaround is a do-nothing function with only the closure as prototyped parameter:
Then you can say
or whatever. Though this is questionable in the sense that it has to be looked up in the documentation, whereas the not-so-pretty solution is plainly obvious to anyone who knows enough Perl.