| 1 | package Mason::Plugin::DollarDot; | ||
| 2 | $Mason::Plugin::DollarDot::VERSION = '2.23'; | ||
| 3 | use Moose; | ||
| 4 | with 'Mason::Plugin'; | ||
| 5 | |||
| 6 | __PACKAGE__->meta->make_immutable(); | ||
| 7 | |||
| 8 | 1; | ||
| 9 | |||
| 10 | __END__ | ||
| 11 | |||
| 12 | =pod | ||
| 13 | |||
| 14 | =head1 NAME | ||
| 15 | |||
| 16 | Mason::Plugin::DollarDot - Allow $. as substitution for $self-> and in | ||
| 17 | attribute names | ||
| 18 | |||
| 19 | =head1 SYNOPSIS | ||
| 20 | |||
| 21 | <%class> | ||
| 22 | has 'name'; | ||
| 23 | has 'date'; | ||
| 24 | </%class> | ||
| 25 | |||
| 26 | <%method greet> | ||
| 27 | Hello, <% $.name %>. Today is <% $.date %>. | ||
| 28 | </%method> | ||
| 29 | |||
| 30 | ... | ||
| 31 | % $.greet(); | ||
| 32 | |||
| 33 | <%init> | ||
| 34 | # Set the date | ||
| 35 | $.date(scalar(localtime)); | ||
| 36 | # or, if combined with LvalueAttributes | ||
| 37 | $.date = scalar(localtime); | ||
| 38 | </%init> | ||
| 39 | |||
| 40 | =head1 DESCRIPTION | ||
| 41 | |||
| 42 | This plugin substitutes C<< $.I<identifier> >> for C<< $self->I<identifier> >> | ||
| 43 | in all Perl code inside components, so that C<< $. >> can be used when | ||
| 44 | referring to attributes and calling methods. The actual regex is | ||
| 45 | |||
| 46 | s/ \$\.([^\W\d]\w*) / \$self->$1 /gx; | ||
| 47 | |||
| 48 | =head1 RATIONALE | ||
| 49 | |||
| 50 | In Mason 2, components have to write C<< $self-> >> a lot to refer to | ||
| 51 | attributes that were simple scalars in Mason 1. This eases the transition pain. | ||
| 52 | C<< $. >> was chosen because of its similar use in Perl 6. | ||
| 53 | |||
| 54 | This plugin falls under the heading of gratuitous source filtering, which the | ||
| 55 | author generally agrees is Evil. That said, this is a very limited filter, and | ||
| 56 | seems unlikely to break any legitimate Perl syntax other than use of the C<< $. | ||
| 57 | >> special variable (input line number). | ||
| 58 | |||
| 59 | =head1 BUGS | ||
| 60 | |||
| 61 | Will not interpolate as expected inside double quotes: | ||
| 62 | |||
| 63 | "My name is $.name" # nope | ||
| 64 | |||
| 65 | instead you have to do | ||
| 66 | |||
| 67 | "My name is " . $.name | ||
| 68 | |||
| 69 | =head1 SEE ALSO | ||
| 70 | |||
| 71 | L<Mason|Mason> | ||
| 72 | |||
| 73 | =head1 AUTHOR | ||
| 74 | |||
| 75 | Jonathan Swartz <swartz@pobox.com> | ||
| 76 | |||
| 77 | =head1 COPYRIGHT AND LICENSE | ||
| 78 | |||
| 79 | This software is copyright (c) 2012 by Jonathan Swartz. | ||
| 80 | |||
| 81 | This is free software; you can redistribute it and/or modify it under | ||
| 82 | the same terms as the Perl 5 programming language system itself. | ||
| 83 | |||
| 84 | =cut |