twigils - Perl 6 style twigils for Perl 5
use twigils; intro_twigil_my_var $!foo; $!foo = 42; say $!foo;
This module implements Perl 6 style twigils for Perl 5.
Twigils are similar to Perl's sigils ($, @, and %, most importantly), but consist of two characters. This module doesn't give any particular meaning to any twigils and leaves that as the user's responsibility.
$
@
%
intro_twigil_my_var $!foo;
Introduces a new lexical twigil variable. Similar to perl's built-in my keyword.
my
intro_twigil_state_var $!foo;
Introduces a new lexical twigil state variable. Similar to perl's built-in state keyword.
state
intro_twigil_our_var $!foo;
Introduces a new lexical twigil variable as an alias to a package variable. Similar to perl's built-in our keyword.
our
This is a ALPHA release made mostly to make it easier for the p5-mop project to experiment with using twigils. I don't recommend anyone using this module for production code. See also: "CAVEATS".
Special punctuation variables and alphanumeric infix operators
Code such as $.eq 42 would normally be interpreted as comparing the contents of the special variable $. with the constant 42 using the eq infix operator. However, in the presence of a twigil variable who's name consists of a special variable name followed by the name of an infix operator (e.g. $.eq) an expression like $.eq 42 will be interpreted as a lookup of the variable $.eq followed by a constant 42, which will result in a syntax error. To disambiguate between these two possible interpretations, use extra whitespace between the special variable and the infix operator, i.e. $. eq 42.
$.eq 42
$.
42
eq
$.eq
$. eq 42
Spaces between twigil and the variable identifier are forbidden
As a consequence of the above caveat, it's not possible to use any whitespace between the twigil and the variable identifier, as is possible with perls built-in lexical variables: $ foo references the variable $foo. $! foo will most likely cause a compile time error.
$ foo
$foo
$! foo
Long-hand dereferencing syntax is required
When storing references in twigil variables, the long-hand circumfix dereferencing notation has to be used. @$!foo doesn't cause the twigil variable $!foo to be dereferenced as an array. @{ $!foo }, however, does.
@$!foo
$!foo
@{ $!foo }
Issues when interpolating in strings
Interpolating twigil variables in strings, such as in my $str = "foo: $.foo";, currently only works reliably for plain scalar twigil variables. Interpolating twigil arrays will not work as expected when trying to interpolate the entire array, a slice of the array, or even just a single element of it. Postfix dereferencing, such as my $str = "$.hash_ref-{foo}"> isn't currently supported either.
my $str = "foo: $.foo";
my $str = "$.hash_ref-
Florian Ragwitz <rafl@debian.org>
This software is copyright (c) 2013 by Florian Ragwitz.
This is free software; you can redistribute it and/or modify it under the same terms as the Perl 5 programming language system itself.
To install twigils, copy and paste the appropriate command in to your terminal.
cpanm
cpanm twigils
CPAN shell
perl -MCPAN -e shell install twigils
For more information on module installation, please visit the detailed CPAN module installation guide.