Home > Archive > PERL Beginners > June 2005 > HOw to pass patterns as function argments?
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 to pass patterns as function argments?
|
|
| Siegfried Heintze 2005-06-05, 8:55 pm |
| I have a fragment of code (consisting of a while loop) that removes all the
keywords in string and puts them at the end. I would like to extract this
while loop into it's own function and have patterns as function arguments.
What should I pass to the function? Strings?
Strategy A:
sub abc { my $s = shift; my $old = shift; my $new = shift; while ($s =~
s/$old/$new/g) { .... }
&abc(" long string here ", "here", "hear");
or
Strategy B
# not sure how to write the sub...
&abc(" long string here", /here/, /hear/);
Thanks again,
Siegfried
| |
| Offer Kaye 2005-06-06, 8:55 am |
| On 6/5/05, Siegfried Heintze wrote:
> I have a fragment of code (consisting of a while loop) that removes all t=
he
> keywords in string and puts them at the end. I would like to extract this
> while loop into it's own function and have patterns as function arguments=
..
>=20
> What should I pass to the function? Strings?
>=20
You can pass strings without a problem.
For effeciency, you can use the qr// operator. See "perldoc perlop":
http://perldoc.perl.org/perlop.html#qr%2fSTRING%2fimosx
Please note however that the second part in the s/// operator is *not*
treated as an RE, only the first part:
s/PATTERN/REPLACEMENT/
PATTERN is a regexp.
REPLACEMENT is treated as double-quoted text (delimiter dependent)
unless you use the "e" modifier, in which case REPLACEMENT will be
eval'ed as a Perl expression.
HTH,
--=20
Offer Kaye
|
|
|
|
|