Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Perl > Perl Misc > Random Qs: overloading ==; import

Reply
Thread Tools

Random Qs: overloading ==; import

 
 
kj
Guest
Posts: n/a
 
      04-11-2004


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.
 
Reply With Quote
 
 
 
 
Anno Siegel
Guest
Posts: n/a
 
      04-11-2004
kj <(E-Mail Removed)> 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
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Python 2.7.3 tempfile.py, ln34, from random import Random as _Random dbv Python 4 03-25-2013 12:54 PM
Math.random() and Math.round(Math.random()) and Math.floor(Math.random()*2) VK Javascript 15 05-02-2010 03:43 PM
random.random(), random not defined!? globalrev Python 4 04-20-2008 08:12 AM
Overloading __init__ & Function overloading Iyer, Prasad C Python 3 09-30-2005 02:17 PM
Re: Overloading __init__ & Function overloading Fredrik Lundh Python 0 09-30-2005 01:53 PM



Advertisments