Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Perl equivalent of PHP's preg_callback

Tags:

regex

php

perl

Do we have a preg_callback equivalent in Perl ?

Lets say I want to match something and replace it with the return value of the function that is called with the matched thing.

like image 413
Joseph Avatar asked May 10 '10 09:05

Joseph


1 Answers

Use s///e - evaluation modifier and you can put arbitrary perl codes in second part.

$x = "this is a test";
$x =~ s/(test)/reverse($1)/eg;
print $x;

//this is a tset

ref: http://perldoc.perl.org/perlretut.html#Search-and-replace

like image 69
YOU Avatar answered Sep 22 '22 14:09

YOU