The London Perl and Raku Workshop takes place on 26th Oct 2024. If your company depends on Perl, please consider sponsoring and/or attending.


Taint::Util - Test for and flip the taint flag without regex matches or eval


    #!/usr/bin/env perl -T
    use Taint::Util;

    # eek!
    untaint $ENV{PATH};

    # $sv now tainted under taint mode (-T)
    taint(my $sv = "hlagh");

    # Untaint $sv again
    untaint $sv if tainted $sv;


Wraps perl's internal routines for checking and setting the taint flag and thus does not rely on regular expressions for untainting or odd tricks involving eval and kill for checking whether data is tainted, instead it checks and flips a flag on the scalar in-place.



Returns a boolean indicating whether a scalar is tainted. Always false when not under taint mode.

taint & untaint

Taints or untaints given values, arrays will be flattened and their elements tainted, likewise with the values of hashes (keys can't be tainted, see perlsec). Returns no value (which evaluates to false).

    untaint(%ENV);                  # Untaints the environment
    taint(my @hlagh = qw(a o e u)); # elements of @hlagh now tainted

References (being scalars) can also be tainted, a stringified reference reference raises an error where a tainted scalar would:

    taint(my $ar = \@hlagh);
    system echo => $ar;      # err: Insecure dependency in system

This feature is used by perl internally to taint the blessed object qr// stringifies to.

    taint(my $str = "oh noes");
    my $re = qr/$str/;
    system echo => $re;      # err: Insecure dependency in system

This does not mean that tainted blessed objects with overloaded stringification via overload need return a tainted object since those objects may return a non-tainted scalar when stringified (see t/reftaint.t for an example). The internal handling of qr// however ensures that this holds true.

File handles can also be tainted, but this probably useless as the handle itself and not lines retrieved from it will be tainted.

    taint(*DATA);    # *DATA tainted
    my $ln = <DATA>; # $ln not tainted


Exports tainted, taint and untaint by default. Individual functions can be exported by specifying them in the use list, to export none use ().


I wrote this when implementing re::engine::Plugin so that someone writing a custom regex engine with it wouldn't have to rely on perl regexps for untainting capture variables, which would be a bit odd.




Ævar Arnfjörð Bjarmason <>


Copyright 2007 Ævar Arnfjörð Bjarmason.

This program is free software; you can redistribute it and/or modify it under the same terms as Perl itself.