Hello,
You maybe want to pass a subroutine's reference to another subroutine.I would modify your codes as follow:

use strict;
sub foo {
  my $p1 = shift ;
  my $p2 = shift ;

  print "Param1 = $p1\n" ;
  &$p2 ;
}

sub bar {
   print "hello\n";
}

foo("test",\&bar);


It execute and get the results as:

Param1 = test
hello


From: "Muttley Meen" <[EMAIL PROTECTED]>
To: beginners@perl.org
Subject: How to pass a block of code, as a parameter to function.
Date: Fri, 23 Jun 2006 17:19:09 +0300

I searched around for a construct to permit for a block
of code to be passed as parameter, but with no success.

The thing that I'm after is this :
<code>
sub foo {
   $p1 = shift ;
   $p2 = shift ;

   print "Param1 = $p1\n" ;
   eval $p2 ;
}


foo("test") {
   print "depth 1\n" ;
}
</code>

<output>
   Param1 = test
   depth 1.
</output>

The closest thing I managed, was to pass the block as an array,
but the syntax gets strange and hard to follow.

foo("test", [ print "depth 1\n" ] )

Any suggestions ?

--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
<http://learn.perl.org/> <http://learn.perl.org/first-response>





--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
<http://learn.perl.org/> <http://learn.perl.org/first-response>


Reply via email to