Perl 6, in our time.
;) I decided to take advantage of the 'bool' overloading context, and "fix"
index(),
rindex(), and
system().
#!/usr/bin/perl -wl
use TrueFalse;
BEGIN {
*CORE::GLOBAL::index = sub {
my $pos = (@_ == 3) ?
CORE::index($_[0], $_[1], $_[2]) :
CORE::index($_[0], $_[1]);
boolean $pos => ($pos == -1 ? false : true);
};
*CORE::GLOBAL::rindex = sub {
my $pos = (@_ == 3) ?
CORE::rindex($_[0], $_[1], $_[2]) :
CORE::rindex($_[0], $_[1]);
boolean $pos => ($pos == -1 ? false : true);
};
*CORE::GLOBAL::system = sub {
my $ret = system(@_);
boolean $ret => ($ret ? false : true);
}
}
print "j => $x" if $x = index "jeff", "j";
print "r => $x" if $x = rindex "jeff", "r";
system "ls" or warn "ls failed";
The code behind this is rather simple -- it's just the "fixing" of the functions that looks tricky.
package TrueFalse;
use overload (
'+0' => \&num,
'""' => \&num,
bool => \&bool,
fallback => 1,
);
require Exporter;
@ISA = qw( Exporter );
@EXPORT = qw( boolean true false );
use constant false => 0;
use constant true => 1;
sub new { bless [ @_[1,2] ], $_[0] }
sub bool { $_[0][1] }
sub num { $_[0][0] }
sub boolean { TrueFalse->new(@_) }
1;
japhy --
Perl and Regex Hacker