File Coverage

blib/lib/Perl6/Attributes.pm
Criterion Covered Total %
statement 15 15 100.0
branch 2 2 100.0
condition n/a
subroutine 4 4 100.0
pod n/a
total 21 21 100.0


line stmt bran cond sub pod time code
1             package Perl6::Attributes;
2              
3 1     1   26450 use 5.006001;
  1         4  
  1         41  
4 1     1   5 use strict;
  1         2  
  1         33  
5 1     1   5 no warnings;
  1         6  
  1         179  
6              
7             our $VERSION;
8             $VERSION = '0.04';
9              
10             use Filter::Simple sub {
11 1         924 s/([\$@%&])\.(\w+)/
12 5 100       30 $1 eq '$' ? "\$self->{'$2'}" : "$1\{\$self->{'$2'}\}"/ge;
13 1         10 s[\./(\w+)][\$self->$1]g;
14 1     1   990 };
  1         29249  
  1         8  
15              
16             =head1 NAME
17              
18             Perl6::Attributes - Perl 6-like member variable syntax
19              
20             =head1 SYNOPSIS
21              
22             package Foo;
23             use Perl6::Attributes;
24            
25             sub new {
26             my ($class) = @_;
27             bless {
28             a => 1,
29             b => [ 2, 3, 4 ],
30             c => { hello => "World" },
31             } => ref $class || $class;
32             }
33              
34             sub example {
35             my ($self) = @_;
36             $.a; # 1
37             $.b[2]; # 4
38             @.b; # 2 3 4
39             $#.b; # 3
40             $.c{hello}; # World
41             keys %.c; # hello
42             print "I get the idea";
43             }
44              
45             =head1 DESCRIPTION
46              
47             I found myself annoyed when I wrote the following code in one of my recent
48             projects:
49              
50             sub populate {
51             my ($self, $n) = @_;
52             for (1..$n) {
53             push @{$self->{organisms}}, Organism->new(rand($self->{width}), rand($self->{height}));
54             }
55             }
56              
57             Three C<$self>s in one line! And it's really not encoding any information,
58             it's just clutter that results from Perl's lack of I object-oriented
59             support. However, Using the magic of source filters, we can now write it:
60              
61             sub populate {
62             my ($self, $n) = @_;
63             for (1..$n) {
64             push @.organisms, Organism->new(rand($.width), rand($.height));
65             }
66             }
67              
68             Perl6::Attributes takes the Perl 6 secondary sigil C<.> and translates it into
69             a hash access on C<$self>. No, it doesn't support other names for your
70             invocant (but it could very easily; I'm just lazy), and no, it doesn't support
71             objects written by crazy people based on array, scalar, or (!) glob references.
72              
73             You still inflect the primary sigil, unlike in Perl 6. See L
74             for a way to use Perl 6's uninflected sigils... but don't expect it to work
75             with this module.
76              
77             There's also a nice little "feature" that you get for trading the ability to
78             name your variables the same with different sigils (by the way, you can't do
79             that). Say $self->{foo} is an array ref:
80              
81             @.foo; # the array itself
82             $.foo; # the reference
83              
84             Which means that even if you're using an array referentially, you can usually
85             avoid writing those pesky C<@{}>s everywhere.
86              
87             Perl6::Attributes now also translates C<./method> and C<./method(args)> to
88             C<$self->method> and C<$self->method(args)>.
89              
90             =head1 SEE ALSO
91              
92             L, L
93              
94             =head1 AUTHOR
95              
96             Luke Palmer
97              
98             =head1 COPYRIGHT
99              
100             Copyright (C) 2005 by Luke Palmer
101              
102             This library is free software; you can redistribute it and/or modify
103             it under the same terms as Perl itself, either Perl version 5.8.3 or,
104             at your option, any later version of Perl 5 you may have available.