Skip to content

Commit

Permalink
Merge pull request shadow-dot-cat#15 from zmughal/13-postderef-direct…
Browse files Browse the repository at this point in the history
…-access

PostfixDeref: direct dereference
  • Loading branch information
zmughal committed Sep 2, 2022
2 parents 00862db + 7a839b8 commit 09ddf2e
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 15 deletions.
4 changes: 2 additions & 2 deletions lib/Babble/Plugin/PostfixDeref.pm
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,7 @@ sub transform_to_plain {
my $stripped = $1;
if ($stripped =~ /(\$\#?)\*$/) {
my $sigil = $1;
$term = '(map '.$sigil.'$_, '.$term.')[0]';
$term = $sigil.'{'.$term.'}';
if( $interpolate ) {
$term = "\@{[ $term ]}";
}
Expand All @@ -132,7 +132,7 @@ sub transform_to_plain {
if ($postfix) {
my ($sigil, $rest) = ($postfix =~ /^\s*->\s*([\@%])(.*)$/);
$rest = '' if $rest eq '*';
$term = '(map '.$sigil.'{$_}'.$rest.', '.$term.')';
$term = $sigil.'{'.$term.'}'.$rest;
if( $interpolate ) {
# NOTE This can be interpolated safely
# because:
Expand Down
26 changes: 13 additions & 13 deletions t/plugin-postfixderef.t
Original file line number Diff line number Diff line change
Expand Up @@ -7,29 +7,29 @@ my $pd = Babble::Plugin::PostfixDeref->new;

my @cand = (
[ 'my $x = $foo->$*; my @y = $bar->baz->@*;',
'my $x = (map $$_, $foo)[0]; my @y = (map @{$_}, $bar->baz);' ],
'my $x = ${$foo}; my @y = @{$bar->baz};' ],
[ 'my $x = ($foo->bar->$*)->baz->@*;',
'my $x = (map @{$_}, ((map $$_, $foo->bar)[0])->baz);' ],
'my $x = @{(${$foo->bar})->baz};' ],
[ 'my @val = $foo->@{qw(key names)};',
'my @val = (map @{$_}{qw(key names)}, $foo);' ],
'my @val = @{$foo}{qw(key names)};' ],
[ 'my $val = $foo[0];',
'my $val = $foo[0];' ],
[ 'my $val = $foo[$idx];',
'my $val = $foo[$idx];' ],
[ '$bar->{key0}{key1}',
'$bar->{key0}{key1}' ],
[ '$bar->{key0}{key1}->@*',
'(map @{$_}, $bar->{key0}{key1})' ],
'@{$bar->{key0}{key1}}' ],
[ '$bar->{key0}{key1}->@[@idx]',
'(map @{$_}[@idx], $bar->{key0}{key1})' ],
'@{$bar->{key0}{key1}}[@idx]' ],
[ 'my %val = $foo->%[@idx];',
'my %val = (map %{$_}[@idx], $foo);' ],
'my %val = %{$foo}[@idx];' ],
[ 'my %val = $foo->%{qw(key names)};',
'my %val = (map %{$_}{qw(key names)}, $foo);' ],
'my %val = %{$foo}{qw(key names)};' ],
[ 'qq{ $foo->@* }',
'qq{ @{[ (map @{$_}, $foo) ]} }' ],
'qq{ @{[ @{$foo} ]} }' ],
[ 'qq{ $foo->@{qw(key names)} }',
'qq{ @{[ (map @{$_}{qw(key names)}, $foo) ]} }' ],
'qq{ @{[ @{$foo}{qw(key names)} ]} }' ],

[ 'qq{ $foo }',
'qq{ $foo }' ],
Expand All @@ -39,15 +39,15 @@ my @cand = (
[ 'qq{ $foo->%* }',
'qq{ $foo->%* }' ],
[ 'qq{ $foo->%* $bar->@* }',
'qq{ $foo->%* @{[ (map @{$_}, $bar) ]} }' ],
'qq{ $foo->%* @{[ @{$bar} ]} }' ],

[ 'qq{ $foo->$* }',
'qq{ @{[ (map $$_, $foo)[0] ]} }' ],
'qq{ @{[ ${$foo} ]} }' ],

[ '$foo->$#*',
'(map $#$_, $foo)[0]' ],
'$#{$foo}' ],
[ 'qq{ $foo->$#* }',
'qq{ @{[ (map $#$_, $foo)[0] ]} }' ],
'qq{ @{[ $#{$foo} ]} }' ],
);

foreach my $cand (@cand) {
Expand Down

0 comments on commit 09ddf2e

Please sign in to comment.