Sed - A sed(1)-like stream editor
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
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;
# 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}; }
darren chamberlain <darren@cpan.org>
To install Sed, copy and paste the appropriate command in to your terminal.
cpanm
cpanm Sed
CPAN shell
perl -MCPAN -e shell install Sed
For more information on module installation, please visit the detailed CPAN module installation guide.