NAME

Sed - A sed(1)-like stream editor

SYNOPSIS

my $a = "Hello, world";
my $b = sed { s/l/0/g } $a;
print "'$a' => '$b'";

'Hello, world' => 'He00o, wor0d'

# Comparison of map and sed:
my $a = "Hello, world";
my $b = map { s/l/0/g } $a;
print "('$a', '$b')\n";

# prints: ('He00o, wor0d', '1')

my $a = "Hello, world";
my $b = sed { s/l/0/g } $a;
print "('$a', '$b')\n";

# prints: ('Hello, world', 'He00o, wor0d')

my $phone_num = "213-555-1212";
my $clean_num = sed { tr/0-9//cd } $phone_num;
print $clean_num;

# prints: 2135551212

DESCRIPTION

Sed implements a stream editor (sed), like the standard Unix utility of the same name. sed is called with a regular expression (see below) in curly braces as its first argument and a scalar as its second argument. A local copy of the scalar is made and the subroutine is applied to it (the original scalar is not modified).

The regular expression can be of the s/// or tr/// forms, and must be enclosed within { }. For example:

$b = sed { s/\[%\s*\(.*)?\s*%\]/$defined{$1}/g } $a;

$d = sed { tr/a-zA-Z0-9//cd } $c;

# From Tom Christiansen's striphtml:
$f = sed {
        s{ <
            (?:
                [^>'"] *
                    |
                ".*?"
                    |
                '.*?'
            ) +
        >
        }{}gsx;
    } $e;

EXAMPLES

# This is the use for which I originally conceived Sed:
package Foo;
use Sed;
use vars '$AUTOLOAD';

sub AUTOLOAD {
    my $self = shift;
    my $autoload = sed { s/.*::// } $AUTOLOAD;
    return $self->{$autoload};
}

AUTHOR

darren chamberlain <darren@cpan.org>