Home > Archive > PERL Miscellaneous > November 2007 > How can you embed a function inside a replacement operator ?
You are viewing an archived Text-only version of the thread.
To view this thread in it's original format and/or if you want to reply to
this thread please [click here]
| Author |
How can you embed a function inside a replacement operator ?
|
|
| neilsolent 2007-11-27, 7:10 pm |
| Hi
Sorry if this is a basic question.
How can you embed a function inside a replacement operator ?
e.g.:
####################################
sub conv()
{
return $_[0] * 2;
}
$test = 'abc125abc';
$test =~ s/(\d+)/&conv($1)/g;
print $test . '\n';
####################################
The output of this script is:
abc&conv(123)abc
Whereas I would like it to be:
abc250abc
Many thanks,
Neil
| |
| Peter Makholm 2007-11-27, 7:10 pm |
| neilsolent <neil@solenttechnology.co.uk> writes:
> Sorry if this is a basic question.
> How can you embed a function inside a replacement operator ?
Look at the /e modifier. It is documented in 'perldoc perlop'.
//Makholm
| |
| nolo contendere 2007-11-27, 7:10 pm |
| On Nov 27, 10:08 am, neilsolent <n...@solenttechnology.co.uk> wrote:
> Hi
> Sorry if this is a basic question.
> How can you embed a function inside a replacement operator ?
>
> e.g.:
> ####################################
> sub conv()
> {
> return $_[0] * 2;
>
> }
>
> $test = 'abc125abc';
>
> $test =~ s/(\d+)/&conv($1)/g;
>
> print $test . '\n';
> ####################################
>
> The output of this script is:
>
> abc&conv(123)abc
>
> Whereas I would like it to be:
>
> abc250abc
>
my $test = 'abc125def';
$test =~ s/(\d+)/conv($1)/ge;
print $test, "\n";
sub conv {
my ( $val ) = @_;
return $val * 2;
}
__OUTPUT__
bash-2.03$ ./replace_func.pl
abc250def
bash-2.03$
| |
| neilsolent 2007-11-27, 7:10 pm |
| On 27 Nov, 15:12, Peter Makholm <pe...@makholm.net> wrote:
> neilsolent <n...@solenttechnology.co.uk> writes:
>
> Look at the /e modifier. It is documented in 'perldoc perlop'.
>
> //Makholm
Thanks for that guys, thought it would be simple. I did do a search
first (honest!)
| |
| nolo contendere 2007-11-27, 7:10 pm |
| On Nov 27, 10:40 am, neilsolent <n...@solenttechnology.co.uk> wrote:
> On 27 Nov, 15:12, Peter Makholm <pe...@makholm.net> wrote:
>
>
>
>
> Thanks for that guys, thought it would be simple. I did do a search
> first (honest!)
np. sometimes it's hard to phrase your question for a search engine,
especially if you lack the correct key word. also, your post was
exemplary in that you posed a cogent question, showed your attempt
that was as short as possible and as long as it needed to be, showed
your results, and showed what you expected.
|
|
|
|
|