Perl6 search then replace with output of subroutineHow to replace a character by a newline in VimHow to replace all occurrences of a string in JavaScriptRegular expression search replace in Sublime Text 2Perl6: Capturing Windows newline in a string with regexType coercion in Perl6 class attributeHow to read gz file line by line in Perl6Cannot import Perl5 module using Inline::Perl5 into Perl6Perl6: getting array ref for Perl5 ModulePerl6: large gzipped files read line by linePerl6 crashes, “left argument in overloaded package Perl6::Object”
Why is the marginal distribution/marginal probability described as "marginal"?
How to check if comma list is empty?
History of the Frobenius Endomorphism?
Does this "yield your space to an ally" rule my 3.5 group uses appear anywhere in the official rules?
Why is Drogon so much better in battle than Rhaegal and Viserion?
How to rename multiple files in a directory at the same time
tikz drawing rectangle discretized with triangle lattices and its centroids
Why would company (decision makers) wait for someone to retire, rather than lay them off, when their role is no longer needed?
the correct order of manual install WP and SSL on server
What do the "optional" resistor and capacitor do in this circuit?
Do people who work at research institutes consider themselves "academics"?
Slice a list based on an index and items behind it in python
Is there any deeper thematic meaning to the white horse that Arya finds in The Bells (S08E05)?
Why did the metro bus stop at each railway crossing, despite no warning indicating a train was coming?
Why do galaxies collide?
Understanding Python syntax in lists vs series
Can anyone give me examples of the relative-determinative 'which'?
Which creature is depicted in this Xanathar's Guide illustration of a war mage?
Assembly writer vs compiler
Cuban Primes
Was the dragon prowess intentionally downplayed in S08E04?
Will the volt, ampere, ohm or other electrical units change on May 20th, 2019?
Does the Rogue's Reliable Talent feature work for thieves' tools, since the rogue is proficient in them?
Getting a similar picture (colours) on Manual Mode while using similar Auto Mode settings (T6 and 40D)
Perl6 search then replace with output of subroutine
How to replace a character by a newline in VimHow to replace all occurrences of a string in JavaScriptRegular expression search replace in Sublime Text 2Perl6: Capturing Windows newline in a string with regexType coercion in Perl6 class attributeHow to read gz file line by line in Perl6Cannot import Perl5 module using Inline::Perl5 into Perl6Perl6: getting array ref for Perl5 ModulePerl6: large gzipped files read line by linePerl6 crashes, “left argument in overloaded package Perl6::Object”
.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty height:90px;width:728px;box-sizing:border-box;
I've combed the docs but I can't seem to find how to do this in perl6.
In perl5 I would have done (just an example):
sub func ...
$str =~ s/needle/func($1)/e;
i.e. to replace 'needle' with the output of a call to 'func'
regex replace perl6 evaluation string-substitution
add a comment |
I've combed the docs but I can't seem to find how to do this in perl6.
In perl5 I would have done (just an example):
sub func ...
$str =~ s/needle/func($1)/e;
i.e. to replace 'needle' with the output of a call to 'func'
regex replace perl6 evaluation string-substitution
add a comment |
I've combed the docs but I can't seem to find how to do this in perl6.
In perl5 I would have done (just an example):
sub func ...
$str =~ s/needle/func($1)/e;
i.e. to replace 'needle' with the output of a call to 'func'
regex replace perl6 evaluation string-substitution
I've combed the docs but I can't seem to find how to do this in perl6.
In perl5 I would have done (just an example):
sub func ...
$str =~ s/needle/func($1)/e;
i.e. to replace 'needle' with the output of a call to 'func'
regex replace perl6 evaluation string-substitution
regex replace perl6 evaluation string-substitution
edited May 10 at 12:57
raiph
12.8k22445
12.8k22445
asked May 10 at 12:09
iPherianiPherian
669824
669824
add a comment |
add a comment |
2 Answers
2
active
oldest
votes
Ok so we'll start by making a function that just returns our input repeated 5 times
sub func($a) $a x 5 ;
Make our string
my $s = "Here is a needle";
And here's the replace
$s ~~ s/"needle"/func($/)/;
Couple of things to notice. As we just want to match a string we quote it. And our output is effectively a double quoted string so to run a function in it we use . No need for the
e
modifier as all strings allow for that kind of escaping.
The docs on substitution mention that the Match object is put in $/
so we pass that to our function. In this case the Match object when cast to a String just returns the matched string. And we get as our final result.
Here is a needleneedleneedleneedleneedle
1
"needle" can of course be an actual regex and it works fine with that too.
– Scimon
May 10 at 12:31
1
Hi Scimon. Nice quick clean answer."needle"
could also beneedle
, without the quotes. Presumably you chose/prefer to quote it for clarity?
– raiph
May 10 at 12:48
add a comment |
There is no e
modifier in Perl 6; instead, the right hand part is treated like a double-quoted string. The most direct way to call a function is therefore to stick an &
before the function name and use function call interpolation:
# An example function
sub func($value)
$value.uc
# Substitute calling it.
my $str = "I sew with a needle.";
$str ~~ s/(needle)/&func($0)/;
say $str;
Which results in "I sew with a NEEDLE.". Note also that captures are numbered from 0 in Perl 6, not 1. If you just want the whole captured string, pass $/
instead.
add a comment |
Your Answer
StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");
StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "1"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);
else
createEditor();
);
function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);
);
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f56077459%2fperl6-search-then-replace-with-output-of-subroutine%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
Ok so we'll start by making a function that just returns our input repeated 5 times
sub func($a) $a x 5 ;
Make our string
my $s = "Here is a needle";
And here's the replace
$s ~~ s/"needle"/func($/)/;
Couple of things to notice. As we just want to match a string we quote it. And our output is effectively a double quoted string so to run a function in it we use . No need for the
e
modifier as all strings allow for that kind of escaping.
The docs on substitution mention that the Match object is put in $/
so we pass that to our function. In this case the Match object when cast to a String just returns the matched string. And we get as our final result.
Here is a needleneedleneedleneedleneedle
1
"needle" can of course be an actual regex and it works fine with that too.
– Scimon
May 10 at 12:31
1
Hi Scimon. Nice quick clean answer."needle"
could also beneedle
, without the quotes. Presumably you chose/prefer to quote it for clarity?
– raiph
May 10 at 12:48
add a comment |
Ok so we'll start by making a function that just returns our input repeated 5 times
sub func($a) $a x 5 ;
Make our string
my $s = "Here is a needle";
And here's the replace
$s ~~ s/"needle"/func($/)/;
Couple of things to notice. As we just want to match a string we quote it. And our output is effectively a double quoted string so to run a function in it we use . No need for the
e
modifier as all strings allow for that kind of escaping.
The docs on substitution mention that the Match object is put in $/
so we pass that to our function. In this case the Match object when cast to a String just returns the matched string. And we get as our final result.
Here is a needleneedleneedleneedleneedle
1
"needle" can of course be an actual regex and it works fine with that too.
– Scimon
May 10 at 12:31
1
Hi Scimon. Nice quick clean answer."needle"
could also beneedle
, without the quotes. Presumably you chose/prefer to quote it for clarity?
– raiph
May 10 at 12:48
add a comment |
Ok so we'll start by making a function that just returns our input repeated 5 times
sub func($a) $a x 5 ;
Make our string
my $s = "Here is a needle";
And here's the replace
$s ~~ s/"needle"/func($/)/;
Couple of things to notice. As we just want to match a string we quote it. And our output is effectively a double quoted string so to run a function in it we use . No need for the
e
modifier as all strings allow for that kind of escaping.
The docs on substitution mention that the Match object is put in $/
so we pass that to our function. In this case the Match object when cast to a String just returns the matched string. And we get as our final result.
Here is a needleneedleneedleneedleneedle
Ok so we'll start by making a function that just returns our input repeated 5 times
sub func($a) $a x 5 ;
Make our string
my $s = "Here is a needle";
And here's the replace
$s ~~ s/"needle"/func($/)/;
Couple of things to notice. As we just want to match a string we quote it. And our output is effectively a double quoted string so to run a function in it we use . No need for the
e
modifier as all strings allow for that kind of escaping.
The docs on substitution mention that the Match object is put in $/
so we pass that to our function. In this case the Match object when cast to a String just returns the matched string. And we get as our final result.
Here is a needleneedleneedleneedleneedle
answered May 10 at 12:20
ScimonScimon
2,1971312
2,1971312
1
"needle" can of course be an actual regex and it works fine with that too.
– Scimon
May 10 at 12:31
1
Hi Scimon. Nice quick clean answer."needle"
could also beneedle
, without the quotes. Presumably you chose/prefer to quote it for clarity?
– raiph
May 10 at 12:48
add a comment |
1
"needle" can of course be an actual regex and it works fine with that too.
– Scimon
May 10 at 12:31
1
Hi Scimon. Nice quick clean answer."needle"
could also beneedle
, without the quotes. Presumably you chose/prefer to quote it for clarity?
– raiph
May 10 at 12:48
1
1
"needle" can of course be an actual regex and it works fine with that too.
– Scimon
May 10 at 12:31
"needle" can of course be an actual regex and it works fine with that too.
– Scimon
May 10 at 12:31
1
1
Hi Scimon. Nice quick clean answer.
"needle"
could also be needle
, without the quotes. Presumably you chose/prefer to quote it for clarity?– raiph
May 10 at 12:48
Hi Scimon. Nice quick clean answer.
"needle"
could also be needle
, without the quotes. Presumably you chose/prefer to quote it for clarity?– raiph
May 10 at 12:48
add a comment |
There is no e
modifier in Perl 6; instead, the right hand part is treated like a double-quoted string. The most direct way to call a function is therefore to stick an &
before the function name and use function call interpolation:
# An example function
sub func($value)
$value.uc
# Substitute calling it.
my $str = "I sew with a needle.";
$str ~~ s/(needle)/&func($0)/;
say $str;
Which results in "I sew with a NEEDLE.". Note also that captures are numbered from 0 in Perl 6, not 1. If you just want the whole captured string, pass $/
instead.
add a comment |
There is no e
modifier in Perl 6; instead, the right hand part is treated like a double-quoted string. The most direct way to call a function is therefore to stick an &
before the function name and use function call interpolation:
# An example function
sub func($value)
$value.uc
# Substitute calling it.
my $str = "I sew with a needle.";
$str ~~ s/(needle)/&func($0)/;
say $str;
Which results in "I sew with a NEEDLE.". Note also that captures are numbered from 0 in Perl 6, not 1. If you just want the whole captured string, pass $/
instead.
add a comment |
There is no e
modifier in Perl 6; instead, the right hand part is treated like a double-quoted string. The most direct way to call a function is therefore to stick an &
before the function name and use function call interpolation:
# An example function
sub func($value)
$value.uc
# Substitute calling it.
my $str = "I sew with a needle.";
$str ~~ s/(needle)/&func($0)/;
say $str;
Which results in "I sew with a NEEDLE.". Note also that captures are numbered from 0 in Perl 6, not 1. If you just want the whole captured string, pass $/
instead.
There is no e
modifier in Perl 6; instead, the right hand part is treated like a double-quoted string. The most direct way to call a function is therefore to stick an &
before the function name and use function call interpolation:
# An example function
sub func($value)
$value.uc
# Substitute calling it.
my $str = "I sew with a needle.";
$str ~~ s/(needle)/&func($0)/;
say $str;
Which results in "I sew with a NEEDLE.". Note also that captures are numbered from 0 in Perl 6, not 1. If you just want the whole captured string, pass $/
instead.
answered May 10 at 13:03
Jonathan WorthingtonJonathan Worthington
10.9k13257
10.9k13257
add a comment |
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f56077459%2fperl6-search-then-replace-with-output-of-subroutine%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown