Is there a perl idiom which is the functional equivalent of calling a subroutine from within the sub

Posted by Thomas L Holaday on Stack Overflow See other posts from Stack Overflow or by Thomas L Holaday
Published on 2010-04-01T00:29:05Z Indexed on 2010/04/01 0:33 UTC
Read the original article Hit count: 486

Perl allows ...

$a = "fee";
$result = 1 + f($a) ; # invokes f with the arugment $a

but disallows, or rather doesn't do what I want ...

s/((fee)|(fie)|(foe)|(foo))/f($1)/ ; # does not invoke f with the argument $1

The desired-end-result is a way to effect a substitution geared off what the regex matched.

Do I have to write ...

sub lala {
  my $haha = shift;
  return $haha . $haha;
}
my $a = "the giant says foe" ;
$a =~ m/((fee)|(fie)|(foe)|(foo))/;
my $result = lala($1);
$a =~ s/$1/$result/;
print "$a\n";

... ?

© Stack Overflow or respective owner

Related posts about perl

Related posts about substitution