I have the following subroutine in Perl to substitute "abc" for "xyz" in a string:
sub mySubst {
my ($str) = @_;
$str =~ s|abc|xyz|ig;
return $str;
}
It works, but seems way too verbose for Perl. How can I tighten it up?
What you have is fine.
shift
would also work, but changes @_ (may or may not be what you want.) There's a discussion on PerlMonks about shift vs @_ that you might be interested in.local $_ = shift;
or local ($_) = @_;
which doesn't add much.)return
. It's a warm fuzzy.Go with it. I think you're on the right track.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With