Skip Menu |

This queue is for tickets about the self CPAN distribution.

Report information
The Basics
Id: 30431
Status: resolved
Worked: 10 min
Priority: 0/
Queue: self

People
Owner: GUGOD [...] cpan.org
Requestors: MARKSTOS [...] cpan.org
Cc:
AdminCc:

Bug Information
Severity: Wishlist
Broken in: (no value)
Fixed in: 0.10



Subject: PATCH: additional tests
Attached is a patch with more tests for additional cases: - self being used by both a parent and child modules - self being used by two unrelated modules at the same time, to test both really do have a "private" self object. All the new tests passed. And this could go in a separate 'wish' ticket, but I think it would be great if the name of 'self' could be renamed. This feature would be 'free' if you used Sub::Exporter instead of Exporter::Lite. See: http://search.cpan.org/dist/Sub-Exporter/lib/Sub/Exporter/Tutorial.pod#Renaming_Your_Imports
Subject: more_tests.patch
diff -rN -u self-0.11-old/t/0simple.t self-0.11-new/t/0simple.t --- self-0.11-old/t/0simple.t 2007-10-28 06:21:27.000000000 -0400 +++ self-0.11-new/t/0simple.t 2007-11-02 13:26:19.000000000 -0400 @@ -1,16 +1,36 @@ use lib 't/lib'; use Counter; -use Test::More tests => 3; +use Test::More tests => 9; -my $o = Counter->new; - -is($o->out, 0); - -$o->inc; - -is($o->out, 1); - -$o->set(5); - -is($o->out, 5); +{ + my $o = Counter->new; + is($o->out, 0); + $o->inc; + + is($o->out, 1); + $o->set(5); + + is($o->out, 5); +} + +{ + my $o = ChildofCounter->new; + is($o->out, 0); + $o->inc; + + is($o->out, 1); + $o->set(5); + + is($o->out, 5); +} +{ + my $o = SecondCounter->new; + is($o->out, 0); + $o->inc; + + is($o->out, 1); + $o->set(5); + + is($o->out, 5); +} diff -rN -u self-0.11-old/t/lib/Counter.pm self-0.11-new/t/lib/Counter.pm --- self-0.11-old/t/lib/Counter.pm 2007-10-28 06:40:42.000000000 -0400 +++ self-0.11-new/t/lib/Counter.pm 2007-11-02 13:25:38.000000000 -0400 @@ -2,7 +2,32 @@ use warnings; package Counter; +use self; + +sub new { + my $class = shift; + return bless { + v => 0 + }, $class; +} + +sub set { + my ($v) = args; + self->{v} = $v; +} + +sub out { + self->{v}; +} + +sub inc { + self->{v}++; +} + +package ChildofCounter; +use base 'Counter'; +package SecondCounter; use self; sub new {
Subject: PATCH: benchmark
From: MARKSTOS [...] cpan.org
Attached is a benchmark you may wish to include in the distribution, comparing 'self.pm' to raw hash access. The result for me what that it is slower (not surprising), but possibly in a way that doesn't matter. I could still call a method using self.pm 50,000 times per second on a slower laptop! I suspect the bottleneck in an application would likely be elsewhere. Mark
#!/usr/bin/perl # Benchmark using 'self.pm' vs. "my $self = shift" # Result: 'self.pm' is slower, but is still very, very fast. use strict; use lib './lib'; package Compare; use self; sub new { my $class = shift; return bless { v => 1, }, $class; } sub with { self->{v} } sub wo { my $self = shift; $self->{v} } package main; my $obj = Compare->new; cmpthese(100000, { 'With' => sub { $obj->with }, 'Without' => sub { $obj->wo }, });
Subject: PATCH: using the more flexible Sub::Exporter
From: MARKSTOS [...] cpan.org
If you are interested in using Sub::Exporter as I suggested, here's was the exporting code would look like: use Sub::Exporter -setup => { exports => [ qw(self args) ], groups => { default => [ -all ], } }; ### And here's what the code would look like *if* someone wanted to rename 'self': use self 'args', 'self' => { -as => 'c' }; ###### (Otherwise, you can just continue to 'use self', and get the current behavior. Mark
Mark, I like your suggestion that "self" can be renamed to other words. I looked into Sub::Exporter for a moment and it's quite easy to make it work. Thanks for contributing on "self", and I decided to take your suggestions here and make a new release for these features. :) Cheers, Kang-min Liu
The tests in the patch will be in next release, so long with those failing tests for using "self" in eval, which are marked as TODO. (Although not sure how to do it yet).