Random Qs: overloading ==; import

Discussion in 'Perl Misc' started by kj, Apr 11, 2004.

  1. kj

    kj Guest

    I recently came across the following (somewhat tongue-in-cheek) Perl
    code.

    #!/usr/bin/perl -w
    use strict;

    BEGIN
    {
    package True;

    use overload
    'eq' => \&equals,
    '==' => \&equals,
    bool => sub { !!1 },
    '!' => sub { False->new() },
    ;

    use base 'Exporter';
    our @EXPORT = qw( &TRUE );

    sub new
    {
    my ($class) = @_;
    my $self = 1;
    return bless \$self, $class;
    }

    sub TRUE
    {
    return True->new();
    }

    sub equals
    {
    my ($x, $y, $swap) = @_;

    $y ? True->new() : False->new();
    }
    }

    BEGIN
    {
    package False;

    use overload
    'eq' => \&equals,
    '==' => \&equals,
    bool => sub { !!0 },
    '!' => sub { True->new() },
    ;

    use base 'Exporter';
    our @EXPORT = qw( FALSE );

    sub new
    {
    my ($class) = @_;
    my $self = 0;
    return bless \$self, $class;
    }

    sub FALSE
    {
    return False->new();
    }

    sub equals
    {
    my ($x, $y, $swap) = @_;

    return $y ? False->new() : True->new();
    }
    }

    BEGIN
    {
    import True;
    import False;
    }

    my $n;
    BEGIN { $n = 0 }
    use Test;
    plan tests => $n;

    BEGIN { $n += 2 }
    ok TRUE;
    ok not FALSE;

    # several more tests omitted from the original

    __END__

    I have some questions about this code:

    1. Why do the equals subs have a third (unused) argument $swap?

    2. In this case, what would be the difference between "import True;"
    and "use True;"? Is "import True;" equivalent to
    "True->import();"? And why are the import statements placed inside
    a BEGIN block?

    Thanks,

    kj


    --
    NOTE: In my address everything before the period is backwards.
     
    kj, Apr 11, 2004
    #1
    1. Advertising

  2. kj

    Anno Siegel Guest

    kj <> wrote in comp.lang.perl.misc:
    >
    >
    > I recently came across the following (somewhat tongue-in-cheek) Perl
    > code.
    >
    > #!/usr/bin/perl -w
    > use strict;
    >
    > BEGIN
    > {
    > package True;


    [...]

    > sub equals
    > {
    > my ($x, $y, $swap) = @_;
    >
    > $y ? True->new() : False->new();
    > }
    > }


    [...]

    BEGIN {
    import True;
    import False;
    }

    [...]

    > I have some questions about this code:
    >
    > 1. Why do the equals subs have a third (unused) argument $swap?


    See perldoc overload. The third parameter is handed in by the overload
    mechanism. It indicates that the agruments have been swapped against
    the original order. This happens when the first operand of an overloaded
    operator is unblessed. Since the result of 'equals' doesn't depend
    on the order of operands, it can be ignored.

    > 2. In this case, what would be the difference between "import True;"
    > and "use True;"?


    See "perldoc use".

    "use" would try to load "True.pm" and fail, so only "import()" is used.

    > Is "import True;" equivalent to
    > "True->import();"?


    Mostly. See "perldoc perlobj".

    > And why are the import statements placed inside
    > a BEGIN block?


    As you have guessed, they replace "use" statements. Since "use" calls
    ->import at compile time, so should the replacement.

    Your questions have little to do with the specifics of the True and False
    classes (most of which I snipped). These techniques apply when modules
    that would normally live in an external file are incorporated into the main
    program.

    Anno
     
    Anno Siegel, Apr 11, 2004
    #2
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Iyer, Prasad C

    Overloading __init__ & Function overloading

    Iyer, Prasad C, Sep 30, 2005, in forum: Python
    Replies:
    3
    Views:
    6,451
    Fredrik Lundh
    Sep 30, 2005
  2. Fredrik Lundh
    Replies:
    0
    Views:
    479
    Fredrik Lundh
    Sep 30, 2005
  3. globalrev
    Replies:
    4
    Views:
    824
    Gabriel Genellina
    Apr 20, 2008
  4. VK
    Replies:
    15
    Views:
    1,339
    Dr J R Stockton
    May 2, 2010
  5. dbv
    Replies:
    4
    Views:
    241
Loading...

Share This Page