Data::PostfixDeref

Allow ->[] ->{} as an alternative to @{ } %{ }
Download

Data::PostfixDeref Ranking & Summary

Advertisement

  • Rating:
  • License:
  • Perl Artistic License
  • Price:
  • FREE
  • Publisher Name:
  • Ben Morrow
  • Publisher web site:
  • http://search.cpan.org/~bmorrow/

Data::PostfixDeref Tags


Data::PostfixDeref Description

Allow ->[] ->{} as an alternative to @{ } %{ } Data::PostfixDeref is a Perl module that installs a hook into the Perl parser, that allows the syntax: $x->[];as an alternative to: @{ $x-> };and similarly $x->{} for %{ $x-> }. The idea is to make expressions like @{ $obj->{foo}{bar}{baz} }less unwieldy. These expressions can be used anywhere the equivalent @{ } expression would have been valid; in particular, they can be passed to (\@)-prototyped functions, and builtins like keys and push.Disallowed syntaxAny further subscripts, such as $x->[]will elict the error 'Additional subscripts after ->[] are forbidden' (but see "TODO" below).Attempting to interpolate a hash into a string with "$x->{}"will fail with 'Can't interpolate hash'.Switching it offThe hooks installed can be removed with no Data::PostfixDeref;Note that all code compiled while the hooks are in effect will get the new syntax, even code in different packages or different files. Also note that once the hooks are removed, string-evals won't allow the syntax, even if the surrounding code would.SYNOPSIS use Data::PostfixDeref; my $x = { a => } ] }; print for $x->{a}{b}[]; print for keys $x->{a}{}; push $x->{a}[], {c => 4}; Requirements: · Perl Limitations: · Since we don't actually replace the Perl parser, the new syntax is not as general as it might be. The only cases that will work are · Directly after another subscript, like · $x->[] · * Directly after a list slice, like · (, )[] · Directly after a sub call (with or without parameters), like · get_aref($x, $y)->[] · Directly after a method call (with or without parameters), like · $obj->get_aref->[] · In particular, neither · $aref->[] · with no intervening subscript nor more complicated expressions like · ($firstaref || $secondaref)->[] · will work (they will still be considered syntax errors); the first due to an artefact of the Perl parser (specifically, that CHECKOP is never called for OP_PADSV), and the second because the question of precedence makes it impossible without being properly integrated into the yacc parser.


Data::PostfixDeref Related Software