in reply to What's your favourite method of untainting?

None of the supplied options...

my $foo; if ($bar =~ m!(acceptable_value|allowed_value)!) { $foo = $1; } else { die 'Invalid value of tainted $bar: ' . $bar; }

Replies are listed 'Best First'.
Re^2: What's your favourite method of untainting?
by Anonymous Monk on Nov 10, 2023 at 05:11 UTC
    Do you write code like that?
    my $foo = $bar =~ m!(acceptable_value|allowed_value)! ? $1 : die 'Invalid value of tainted $bar: ' . $bar;
    (though I'm not sure why you have $bar twice in the error message...)
      ... : die 'Invalid value of tainted $bar: ' . $bar;
      (though I'm not sure why you have $bar twice in the error message...)

      First $bar is within single quotes, so not interpolated; second $bar is interpolated. die message will look like this:

      Invalid value of tainted $bar: tainted_bar_value ...

      — Ken

      Do you write code like that?

      Yes...

      It is clear exactly what it does and it's easy to maintain even by a non-Perl expert (perhaps that should be a Perl non-expert!)