yitzle wrote:

Can you pass a subroutine as a string or pointer or store it inside a variable?
example (probably doesn't work, but that's not the point):

#!/usr/bin/perl

sub func1 {
  my $i = shift;
  return $i =~ /^[0-9]+$/;
}

sub func2 {
  my $i = shift;
  return $i =~ /^[a-z]+$/;
}

sub test {
  my ($function, $description, $var, undef) = @_;
  print $description . ": ";
  print {$function}($var) ? "Yes\n" : "No\n"; # Not even sure Perl
will allow this if operator like this...
}

my $input = <>;
test("func1", "Numeric", $input);
test("func2", "Lower", $input);

Almost!

use strict;
use warnings;

sub func1 {
 my $i = shift;
 return $i =~ /^[0-9]+$/;
}

sub func2 {
 my $i = shift;
 return $i =~ /^[a-z]+$/;
}

sub test {
 my ($function, $description, $var, undef) = @_;
 print $description . ": ";
 print $function->($var) ? "Yes\n" : "No\n";
}

my $input = <>;
test(\&func1, "Numeric", $input);
test(\&func2, "Lower", $input);


HTH,

Rob

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


Reply via email to