Beefy Boxes and Bandwidth Generously Provided by pair Networks
Clear questions and runnable code
get the best and fastest answer
 
PerlMonks  

japhygesis

by japhy (Canon)
on Jun 21, 2001 at 20:19 UTC ( [id://90442]=perlmeditation: 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

Replies are listed 'Best First'.
Re: japhygesis
by Abigail (Deacon) on Jun 22, 2001 at 05:51 UTC
    What follows is a module I wrote quite some time ago (as a proof of concept) that "fixes" index in a different way. It returns undef if there is no match, and the position of the match otherwise - however, if it matches at the beginning of the string, "0 but true" is returned.
    package Perl; use Exporter; use vars qw /@ISA @EXPORT @EXPORT_OK %EXPORT_TAGS/; @ISA = qw /Exporter/; @EXPORT = qw /index/; @EXPORT_OK = qw //; %EXPORT_TAGS = (); sub index { my $result; $result = 2 == @_ ? CORE::index ($_ [0], $_ [1]) : CORE::index ($_ [0], $_ [1], $_ [2]); $result < 0 ? undef : $result || "00"; } 1; __END__

    -- Abigail

      Nice, too few people realise that you can just return a string to evaluate as zero, but still be true. I often return things like "0 but true" when I want the 0 value to be true; that way you can return 0 on errors too. A little trick I picked up was if you need to print the value, you can just use "0\0" It's 0 plus the null character, so it's true, but it only prints 0.

      The 15 year old, freshman programmer,
      Stephen Rawls

        #!/usr/bin/perl -w print 0+"0 but true",$/; print 0+"0\0",$/; __END__ Argument "0" isn't numeric in addition (+) at - line 3 0 0

        Not only does it generate warnings, but they are confusing warnings. I'd probably use "00" over "0\0" if you wanted something that printed out nicer. (:

                - tye (but my friends call me "Tye")

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlmeditation [id://90442]
Approved by root
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others imbibing at the Monastery: (4)
As of 2024-03-29 00:49 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found