Skip to content

Instantly share code, notes, and snippets.

@schwern
Created March 17, 2010 21:40
Show Gist options
  • Select an option

  • Save schwern/335741 to your computer and use it in GitHub Desktop.

Select an option

Save schwern/335741 to your computer and use it in GitHub Desktop.
#!/usr/bin/perl
use v5.10;
use Test::More;
# Can you make this test pass just by changing UNIVERSAL::id and other
# dirty tricks?
#
# You cannot store anything in the object.
# You can make no assumptions about the structure of the object.
# You can not alter or add methods to Foo.
# You can assume 5.10.
# Ideally it should use < O(n) memory (where n is the number of objects created)
{
package UNIVERSAL;
# This demonstration is known not to work, references addresses
# are reused by Perl.
use Scalar::Util ();
sub id { Scalar::Util::refaddr $_[0] }
}
# You cannot change anything below this point
{
package Foo;
use Scalar::Util qw(refaddr);
sub new {
my $class = shift;
my $string = shift;
return bless [$string], $class;
}
sub set {
my $self = shift;
$self->[0] = shift;
}
sub DESTROY {}
}
my %seen;
for(1..3) {
my $obj = Foo->new("wibble");
my $id = $obj->id;
# perl will often reuse references
ok !$seen{$id}, "$id not yet seen";
$seen{$id}++;
$obj->set(42);
is $obj->id, $id, "id not effected by object content";
}
done_testing();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment