grinder has asked for the wisdom of the Perl Monks concerning the following question:

Here's a quickie: this came up on perl5-porters yesterday: someone filed what they thought was a bug, but was bitten by the fact that a capture that does not match returns undef, rather than the empty string. One way around it is to just switch of warnings when substituting values containing undef:

use strict; use warnings; use Test::More tests => 6; is(munge('R'), 'text', 'R'); is(munge('R1'), '1text', 'R1'); is(munge('R2'), '2text', 'R2'); is(munge('R3'), 'text3', 'R3'); # this here is the trickiness is(munge('R13'), 'R13', 'R13 unchanged'); is(munge('R4'), 'R4', 'R4 unchanged'); sub munge { my $str = shift; no warnings; $str =~ s/^R(?:([12])|(3))?$/${1}text$2/; $str; }

Another alternative, even more icky in my books, is to say:

sub munge { my $str = shift; $str =~ s{^R(?:([12])|(3))?$} {(defined $1 ? $1 : '') . 'text' . (defined $2 ? $2 : '') +}e; $str; }

So I ask the monks... is there an elegant (clean, simple) way to do the substition without /e or no warnings? Note that the real tokens R, 1, 2... in real life are meta characters, so substr is ruled out, it must be a regexp, and so one just needs to make sure that $1 and $2 are always strings, empty or otherwise.

• another intruder with the mooring in the heart of the Perl

Replies are listed 'Best First'.
Re: Dealing with undefined captures in a substitution
by Limbic~Region (Chancellor) on Aug 03, 2007 at 12:36 UTC
    grinder,
    Well, the adjectives elegant, clean, and simple are highly subjective, but I often test before I destroy. In other words, I put the match portion in a conditional and then do the subsitution.

    This allows me to figure out what needs to be done in multiple steps (maintainability) rather than try and shove it all in a single expression that is hard to extend or possibly decipher by someone else.

    Cheers - L~R

Re: Dealing with undefined captures in a substitution
by RMGir (Prior) on Aug 03, 2007 at 12:31 UTC
    Using || is less painful than defined($1)?...
    sub munge { my $str = shift; $str =~ s/^R(?:([12])|(3))?$/(${1}||"")."text".($2||"")/e; $str; }
    Other than that, I don't know of other more elegant options.

    Edit:Anonymous Monk below is correct - _if_ our regex could capture a zero for $1 or $2, we'd have to do the defined()?: approach. But 0 can't match either capture here...


    Mike
      That is dangerous if you're possibly matching "0".
Re: Dealing with undefined captures in a substitution
by ikegami (Patriarch) on Aug 03, 2007 at 13:24 UTC
    no warnings 'uninitialized'; would be better.
Re: Dealing with undefined captures in a substitution
by moritz (Cardinal) on Aug 03, 2007 at 12:57 UTC
    If the sole purpose of a construct is to avoid a warning, I don't see why you shouldn't disable the warning explicitly. Only that warning, mind you, and in a small scope.