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
In reply to japhygesis
by japhy
Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
Read Where should I post X? if you're not absolutely sure you're posting in the right place.
Please read these before you post! —
Posts may use any of the Perl Monks Approved HTML tags:
- a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
| |
For: |
|
Use: |
| & | | & |
| < | | < |
| > | | > |
| [ | | [ |
| ] | | ] |
Link using PerlMonks shortcuts! What shortcuts can I use for linking?
See Writeup Formatting Tips and other pages linked from there for more info.