Beefy Boxes and Bandwidth Generously Provided by pair Networks
The stupid question is the question not asked
 
PerlMonks  

comment on

( [id://3333]=superdoc: print w/replies, xml ) Need Help??
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

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":



  • Are you posting in the right place? Check out Where do I post X? to know for sure.
  • Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
    <code> <a> <b> <big> <blockquote> <br /> <dd> <dl> <dt> <em> <font> <h1> <h2> <h3> <h4> <h5> <h6> <hr /> <i> <li> <nbsp> <ol> <p> <small> <strike> <strong> <sub> <sup> <table> <td> <th> <tr> <tt> <u> <ul>
  • Snippets of code should be wrapped in <code> tags not <pre> tags. In fact, <pre> tags should generally be avoided. If they must be used, extreme care should be taken to ensure that their contents do not have long lines (<70 chars), in order to prevent horizontal scrolling (and possible janitor intervention).
  • Want more info? How to link or How to display code and escape characters are good places to start.
Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others perusing the Monastery: (5)
As of 2024-04-23 22:43 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found