writing a function like map, sort, grep

J

John

Is there a way to write a function that ban be run like the
aforementioned ones, i.e.

Foo BLOCK LIST

I want to do this because I sometimes want LIST to be a real list, and
sometimes a function. I know there are other ways to do it, I was just
curious if something like this can be implemented easily.

TIA, John
 
J

John

John said:
Is there a way to write a function that ban be run like the
aforementioned ones, i.e.

Foo BLOCK LIST

I want to do this because I sometimes want LIST to be a real list, and
sometimes a function. I know there are other ways to do it, I was just
curious if something like this can be implemented easily.

TIA, John

I don't mean a function, I mean a CODE reference, which I could
determine inside Foo (hopefully).
 
G

Gunnar Hjalmarsson

John said:
Is there a way to write a function that ban be run like the
aforementioned ones, i.e.

Foo BLOCK LIST

You can use prototypes

sub Foo(&@)
 
M

Matija Papec

X-Ftn-To: John

John said:
Is there a way to write a function that ban be run like the
aforementioned ones, i.e.

Foo BLOCK LIST

I want to do this because I sometimes want LIST to be a real list, and
sometimes a function. I know there are other ways to do it, I was just
curious if something like this can be implemented easily.

It seems that it can, I saw some example using prototypes,

sub apply (&@) {
my $sub = shift;
$sub->() for my @ret = @_;
@ret;
}

print
$str = join "", apply {tr/abc/def/; s/d/123/g} qw/jen dva/;
 
B

Ben Morrow

Quoth Gunnar Hjalmarsson said:
You can use prototypes

sub Foo(&@)

Note that this will *not* allow the foo EXPR, LIST usage of map, grep,
nor the named or missing sub of sort (as is reflected by the fact that
prototype returns undef for all of these).

Ben
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,767
Messages
2,569,572
Members
45,046
Latest member
Gavizuho

Latest Threads

Top