Beefy Boxes and Bandwidth Generously Provided by pair Networks
There's more than one way to do things
 
PerlMonks  

Re^2: RFC: "assignary" operator ?= : ( use Filter::PPI::Transform 'PPI::Transform::ConditionalAssignment'; )

by Anonymous Monk
on Dec 11, 2019 at 19:11 UTC ( [id://11109993]=note: print w/replies, xml ) Need Help??


in reply to Re: RFC: "assignary" operator ?= : ( source Filter::PPI::Transform )
in thread RFC: "assignary" operator ?= :

Proof of concept

#!/usr/bin/perl -- BEGIN{ package PPI::Transform::ConditionalAssignment; use strict; use warnings; use Params::Util qw{_STRING _INSTANCE}; use PPI::Transform (); use parent 'PPI::Transform'; our $VERSION = '0.1'; # VERSION sub document { my $self = shift; my $document = _INSTANCE(shift, 'PPI::Document') or return undef; my $elements = $document->find( sub { $_[1]->isa('PPI::Token::Operator') or return ''; $_[1]->content eq '?' or return ''; $_[1]->snext_sibling->isa('PPI::Token::Operator') or return '' +; $_[1]->snext_sibling->content eq '=' or return ''; return 1; } ); return undef unless defined $elements; return 0 unless $elements; my $changes = 0; foreach my $question ( @$elements ) { my $symbol = $question->sprevious_sibling->clone; my $equals = $question->snext_sibling; $equals->insert_after( $symbol ); $symbol->insert_after( $question->remove ); $changes++; } return $changes; } 1; $INC{'PPI/Transform/ConditionalAssignment.pm'}=__FILE__; } # BEGIN package main; use Filter::PPI::Transform 'PPI::Transform::ConditionalAssignment'; for my $foo ( 0 .. 1 ) { my $bar = $foo; $foo ?= 'one' : 'zero'; print "$foo\n"; $bar ?= ('one '.rand): ('zero '.rand); print "$bar\n"; } print "let me know " ? "should I stay" : "or should I go"; __END__ $ perl ppi-transform-conditional-assignment.pl zero zero 0.563323974609375 one one 0.086761474609375 should I stay
  • Comment on Re^2: RFC: "assignary" operator ?= : ( use Filter::PPI::Transform 'PPI::Transform::ConditionalAssignment'; )
  • Download Code

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://11109993]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others about the Monastery: (3)
As of 2024-04-25 22:22 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found