Yes, that is sort of what I meant... but not totally. Funny thing is,
I found out about the ability to use the $regexp variable on the left
side of the s/// shortly after originally posting my question. In all
of the research that I have done on this obstacle, I have found that
ability only mentioned in one place, perldoc perlop.
Here is the sample script you asked for:
############################################################
# Position and character would normally be determined
# by user input, but are hard-coded here for brevity
my $position = 2; # Is always a number, 1-4
my $character = "~"; # Can be any "special" char
# Build substitution for later use
my ( $pattern, $substitute );
if ( $position == 1 ) { # s/^(.*)$/~$1/
$pattern = qr/^(.*)$/;
$substitute = "$character\$1";
}
elsif ( $position == 2 ) { # s/^(.*)(\.[^.]+)$/$1~$2/
$pattern = qr/^(.*)(\.[^.]+)$/;
$substitute = "\$1$character\$2";
}
elsif ( $position == 3 ) { # s/^(.*\.)([^.]+)$/$1~$2/
$pattern = qr/^(.*\.)([^.]+)$/;
$substitute = "\$1$character\$2";
}
elsif ( $position == 4 ) { # s/^(.*)$/$1~/
$pattern = qr/^(.*)$/;
$substitute = "\$1$character";
}
else {
die "Invalid position: $position\n";
}
# Loop thousands of times
for ( 0..99_000 ) {
# Filename would be different every time
my $filename = "filename.plus.ext";
$filename =~ s/$pattern/$substitute/;
print "$filename\n";
}
############################################################
I've also tried it this way, with anonymous subroutines:
############################################################
<...snip...>
elsif ( $position == 2 ) {
$pattern = qr/^(.*)(\.[^.]+)$/;
$substitute = sub { return "\$1$character\$2" };
}
<...snip...>
for ( 0..99_000 ) {
# Filename would be different every time
my $filename = "filename.plus.ext";
$filename =~ s/$pattern/$substitute->()/e;
print "$filename\n";
}
############################################################
Unfortunately, both of these methods simply turn $filename into a
literal "$1~$2". In your "s/$regexp/something else/;" it's the
"something else" that's killing me.
Hopefully you can see what I am trying to do here, and why. I'm sure
that there is a way to do this, I just haven't been able to figure it
out yet. There's no idiot like a blind idiot. :P
Thank you,
-Brian
-----Original Message-----
Brian H. Oak <> wrote:
> I'm looking for a way to build a substitution (s///) once at the
> beginning of my program so that I don't have to evaluate a
> conditional and build the substitution each of the thousands of
times
> my program loops.
I don't understand what your question. Perhaps you could write a small
script that demonstrates what you are trying to do.
> I know about the $regexp = qr/pattern/ construct,
> but can't figure out how to do something equivalent with a
> substitution.
my $regexp = qr/pattern/;
s/$regexp/something else/;
Is that what you mean?
HTH
--
Brian Raven
_______________________________________________
ActivePerl mailing list
[email protected]
To unsubscribe: http://listserv.ActiveState.com/mailman/mysubs