How can I create a Perl subroutine that accepts more than one block?

You could just eval the blocks

use experimental 'signatures';
use feature 'say'; 

sub doublethecode ($block1, $block2) { eval { $block1; }; eval { $block2;} } ;
doublethecode \&{ print "OH " }, \&{ say for qw/HAI YOU LOLCAT :-P/  }; 

output:

OH, HAI
YOU
LOLCAT
:-P

Then just ignore the \& and you're there.

As of v5.20 You can use signatures and :prototype attributes together. The signatures feature gives a more "common" syntax and a minimalist sort of argument checking. Maybe one day a future perl will have a "builtin" (optional, highly flexible) type system of some kind to go with it, but prototypes aren't anything like that right now. So something like:

sub doublethecode :prototype(\&\&) ($cr1, $cr2) { $cr1 ; $cr2 ; }

is not what it seems. Since prototypes, signatures and signatures with :prototype(&&) may not be giving you what think you're getting, this might be good enough:

sub doublethecode { eval { shift };  eval{ shift } } ;
doublethecode &{ print "perl" }, &{ print "6" }

output:

perl6

To make sure perl doesn't think {} is a hash, the & is necessary. But actually ... what is wrong with using anonymous subroutines sub { } here?


I hope you realise that this is just code seasoning, and all you are achieving is a tidier syntax at the expense of clarity?

Perl won't allow you to pass more than one bare block to a subroutine, but the second actual parameter could be a call to a subroutine that also takes a single block and simply returns the code reference.

This program demonstrates. Note that I have chosen please and also as names for the subroutines. But you must use something that is both appropriate to your own code's functionality and very unlikely to clash with forthcoming extensions to the core language.

use strict;
use warnings;

sub please(&$) {
  my ($code1, $code2) = @_;
  $code1->();
  $code2->();
}

sub also(&) {
  $_[0];
}

please { print "aaa\n" } also { print "bbb\n" };

output

aaa
bbb

works for me...

sub example2  {
   my $code_ref = shift;
   my $code_ref2 = shift;
   $code_ref->();
   $code_ref2->();
}
example2 ( sub { print "One\n" }, sub { print "Hello\n" });

Just for the purposes of TMTOWTDI here is a method that works in somewhat the way that the OP requested

First, make a source filter

package poop;

use Filter::Util::Call;

sub import {
    my ($type) = @_;
    my ($ref)  = [];
    filter_add( bless $ref );
}

sub filter {
    my ($self) = @_;
    my ($status);
if (( $status = filter_read() ) > 0) { 
        if(!/sub/ && /example2/) {
            s/\{/sub \{/g;
            }
        }

    $status;
}
1;

Second the filter must be used

use poop;

sub example2  {
   my $code_ref = shift;
   my $code_ref2 = shift;
   $code_ref->();
   $code_ref2->();
}
example2 ( { print "One\n" }, { print "Hello\n" });

ps. this is horrific and noone would wish to see this actually in production