Beefy Boxes and Bandwidth Generously Provided by pair Networks
P is for Practical
 
PerlMonks  

Perl allows you to change the definition of false to make it true. (And vice versa.)

by tobyink (Abbot)
on Dec 12, 2012 at 18:43 UTC ( #1008567=perlmeditation: print w/ replies, xml ) Need Help??

For example:

use 5.010; use strict; use warnings; BEGIN { # Allow false to be modified. &Internals::SvREADONLY(\!!0, 0); # Modify false to be equal to 1. ${\!!0} = 1; # Disallow false to be modified again. &Internals::SvREADONLY(\!!0, 1); }; say "left is right!" if "left" eq "right"; say "up is down!" if "up" eq "down"; say "black is white!" if "black" eq "white"; say "maths is flawed!" if 7 > 100;
perl -E'sub Monkey::do{say$_,for@_,do{($monkey=[caller(0)]->[3])=~s{::}{ }and$monkey}}"Monkey say"->Monkey::do'

Comment on Perl allows you to change the definition of false to make it true. (And vice versa.)
Download Code
Re: Perl allows you to change the definition of false to make it true. (And vice versa.)
by BrowserUk (Pope) on Dec 12, 2012 at 19:08 UTC

    Cool! But there is only one possible response :)

    BEWARE: This module is DANGEROUS! ...

    ANYTHING might happen! Hell might break loose! :-)

    YOU HAVE BEEN WARNED!


    With the rise and rise of 'Social' network sites: 'Computers are making people easier to use everyday'
    Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
    "Science is about questioning the status quo. Questioning authority".
    In the absence of evidence, opinion is indistinguishable from prejudice.

    RIP Neil Armstrong

      I discovered the trick perusing boolean which uses it to good effect. It makes boolean expressions such as "foo" eq "bar" return boolean objects (which in turn overload boolification). The net effect is to give Perl a boolean type.

      perl -E'sub Monkey::do{say$_,for@_,do{($monkey=[caller(0)]->[3])=~s{::}{ }and$monkey}}"Monkey say"->Monkey::do'
        boolean which uses it to good effect. It makes boolean expressions such as "foo" eq "bar" return boolean objects

        Sounds like a great way to burn cycles for no good reason.

        All someone needs to do now is wrap boolean over in a Moose wrapper and we can all compute like it was 1984 :)


        With the rise and rise of 'Social' network sites: 'Computers are making people easier to use everyday'
        Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
        "Science is about questioning the status quo. Questioning authority".
        In the absence of evidence, opinion is indistinguishable from prejudice.

        RIP Neil Armstrong

        do &maybe if boolean($value)->isTrue;

        Surely he meant:

        do &maybe if boolean( true eq boolean($value)->isTrue() )->isTrue();

        (:

        - tye        

Re: Perl allows you to change the definition of false to make it true. (And vice versa.)
by LanX (Canon) on Dec 12, 2012 at 22:50 UTC
    And as usual it's far easier in Python! ;-)

    >python Python 2.5.2 (r252:60911, Jan 20 2010, 23:16:55) [GCC 4.3.2] on linux2 >>> False, True = True, False >>> False True >>> True False

    Python rules obfuscation!

    Cheers Rolf

      Python allows you to redefine the "constants" True and False, but that doesn't change the behaviour of...

      >>> "foo"=="bar" False
      perl -E'sub Monkey::do{say$_,for@_,do{($monkey=[caller(0)]->[3])=~s{::}{ }and$monkey}}"Monkey say"->Monkey::do'
        So Python is easier to brake, but is incomplete in this attempt? ;-)

        Cheers Rolf

Re: Perl allows you to change the definition of false to make it true. (And vice versa.)
by Anonymous Monk on Dec 12, 2012 at 23:01 UTC
    True ... true ...
Re: Perl allows you to change the definition of false to make it true. (And vice versa.)
by bulk88 (Priest) on Dec 13, 2012 at 04:41 UTC
    Blessing false (PL_sv_no) would also be impressive.
Re: Perl allows you to change the definition of false to make it true. (And vice versa.)
by Tux (Monsignor) on Dec 13, 2012 at 13:47 UTC

    See Juerd's talk "When undef isn't". When it doesn't show, disable CSS.


    Enjoy, Have FUN! H.Merijn
Re: Perl allows you to change the definition of false to make it true. (And vice versa.)
by Anonymous Monk on Dec 13, 2012 at 20:43 UTC
    Whodathunkit that Perl would turn out to be a politician? ;-)
Re: Perl allows you to change the definition of false to make it true. (And vice versa.)
by LanX (Canon) on Dec 13, 2012 at 22:48 UTC
    Actually I can see useful applications for this.

    When trying to convert (and not just interpret) other languages like LISP or JS to Perl one of the biggest problems are implicit typeconversions at edgecases.

    (Thoroughly catching all those edgecases slows such conversions ridiculously down!)

    For instance this way, one could try to patch booleans in a way that they stringify to "false" and "true" like in JS.

    Thanks for showing! =)

    Cheers Rolf

      This is what boolean does.

      perl -E'sub Monkey::do{say$_,for@_,do{($monkey=[caller(0)]->[3])=~s{::}{ }and$monkey}}"Monkey say"->Monkey::do'
        > > ... For instance this way, one could try to patch booleans in a way that they stringify to "false" and "true" like in JS. ...

        > This is what boolean does.

        well, not yet:

        JS-Console

        >>> true true >>> "stringification: " + (1==1) "stringification: true"

        use v5.10.0; use boolean; say true; say "stringification:" . (1==1);

        output:

        /usr/bin/perl -w /tmp/boolean.pl 1 stringification:1

        but using dualvars could help.

        UPDATE:

        kind of

        use Scalar::Util qw/dualvar/; my $false = dualvar 0, "false"; my $true = dualvar 1, "true"; say $true; say "stringification:" . $true; say "numification: " . (0+$true); say $false; say "stringification: " . $false; say "numification: " . (0+$false);

        Cheers Rolf

Re: Perl allows you to change the definition of false to make it true. (And vice versa.)
by Anonymous Monk on Dec 14, 2012 at 14:26 UTC
    This should please the comp.lang.c old timers:
    use strict; use warnings; use 5.010; BEGIN { &Internals::SvREADONLY(\undef, 0); ${\undef} = "Demons are coming out of my nose!"; };
    I wonder if you can redefine other read-only values? I could not get the following to work.
    BEGIN { &Internals::SvREADONLY(\'blue', 0); ${\'blue'} = 'turtle'; &Internals::SvREADONLY(\5, 0); ${\5} = 5.000000000001; };
    (I have no idea why you would want to do this apart from JAPH or intentional sabotage)

      AFAIK, only true, false and undef. I thought perhaps "0 but true" might work (as that has special handling - it doesn't warn if used in numeric context) but no joy.

      perl -E'sub Monkey::do{say$_,for@_,do{($monkey=[caller(0)]->[3])=~s{::}{ }and$monkey}}"Monkey say"->Monkey::do'
Re: Perl allows you to change the definition of false to make it true. (And vice versa.)
by grondilu (Pilgrim) on Dec 16, 2012 at 14:44 UTC

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlmeditation [id://1008567]
Approved by muba
Front-paged by Arunbear
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others taking refuge in the Monastery: (9)
As of 2014-11-23 07:49 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    My preferred Perl binaries come from:














    Results (129 votes), past polls