#!/usr/bin/perl use strict; use warnings; package Foo; # un package finto... sub bar { my $code_ref = shift; my $str1 = shift; my $str2 = shift; return \&$code_ref( $str1, $str2 ); } # Una funzione finta, che si aspetta di ricevere una # reference ad una funzione che vuole un paio di stringhe # e restituisce qualcosa (uno scalare, in questo caso) package main; # Chiuso il package Foo, si passa a main # Che contiene... sub mySub1 { my $str1 = shift; my $str2 = shift; print "mySub1: $str1 $str2\n"; } # mySub1(), una sub che vuole solamente un paio di stringhe... sub mySub2 { my $array_ref = shift; my $str1 = shift; my $str2 = shift; print "mySub2: $str1 $str2 @$array_ref\n"; } # ... e mySub2(), che oltre alle stringhe vuole che le sia # passata una ref ad un array: # Il problema e`, a questo punto, costruire due ref da # passare a Foo::bar(), a partire dalle due funzioni, che # come e` stato detto hanno una signature differente... my $ref1 = \&mySub1; Foo::bar( $ref1, 'pippo', 'pluto' ); # per la prima funzione non c'e` problema... # Per la seconda funzione my $ref2; { my $array_ref = [1, 2, 3]; $ref2 = sub { mySub2( $array_ref, shift, shift ); }; } # $ref2 e` una closure, cioe` una sub anonima che fa # riferimento a variabili lessicali che erano visibili # al momento della sua creazione. In questo caso $array_ref. Foo::bar( $ref2, 'pippo', 'pluto' ); # Non sono ancora sicuro che le cose si facciano cosi`. # Leggi la doc (e il codice) di Attribute::Curried, oltre # a perldoc perlref (§"Function Templates")