Random Qs: overloading ==; import

K

kj

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
 
A

Anno Siegel

kj said:
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
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,053
Latest member
BrodieSola

Latest Threads

Top