Beefy Boxes and Bandwidth Generously Provided by pair Networks
Pathologically Eclectic Rubbish Lister
 
PerlMonks  

Re: Converting to sub signatures -- PPI

by Discipulus (Canon)
on Jun 24, 2022 at 07:07 UTC ( [id://11145013]=note: print w/replies, xml ) Need Help??


in reply to Converting to sub signatures

Hello cavac,

this sounds very fun! For a zillion of XPD I'd write for you something better :)

No, seriously: parsing perl with regexes? "Only perl can parse Perl" see also On Parsing Perl

Above statements and link comes from PPI documentation.. and yes PPI works!

You can give a try to one of most underestimated CUFP of mines: Repeatedly edit a file hacking PPI::Cache and use it to parse PPI::Statement::Sub class in a perl document. Let see it at work:

perl ppi-editor.pl cache file last_hex_id_file.sto not found. # it search for p +revious runs in the hardcoded filename last_hex_id_file.sto Insert the full path of a new file to edit and press enter (or CTRL-C +to terminate) FILE PATH:./Exp.pm # <---- loading a + file loading from cache ok 'last_hex_id_file.sto' succesfully read: using 5a459de7e28b9b423d7c8d5 +ba988fbdb Which PPI class do you want to edit? PPI CLASS:PPI::Statement::Sub # <---- selecting + a PPI class to work on Each element of the type PPI::Statement::Sub will be proposed for edit + (the content). insert your new input terminating it with CTRL-Z on a empty line. use a bare ENTER to skip the current element STATEMENT: sub new{ my $class = shift; my $validated = _validate( $_[0] ); return bless { validated => $validated, stored => [], },$class; } CONTENT: sub new{ my $class = shift; my $validated = _validate( $_[0] ); return bless { validated => $validated, stored => [], },$class; } EDIT: .... # <---- and so on for every su +b statement storing cache hex_id: 5a459de7e28b9b423d7c8d5ba988fbdb in last_hex_id_ +file.sto Enter a filename if you want to save the current version (or ENTER to +skip) OUTPUT FILE:

You can use the above code as skeleton to automatically inspect every sub and then inspect each shift or assignement coming from @_ to change the sub adding signatures. Should be fun!

I suggest you a final diff log just to review what happened to your code: "unexpected is always a possiblity" :)

L*

UPDATE See PPI::Element to see the difference between statement and content ..in effect i forgot it :) but relaunching my program to inspect PPI::Token::Quote class will demonstrate the difference well enough:

perl ppi-editor.pl 'last_hex_id_file.sto' succesfully read: using 5a459de7e28b9b423d7c8d5 +ba988fbdb # it wants to reload previous edited file Which PPI class do you want to edit? + # if it finds last_hex_id_file.sto PPI CLASS:PPI::Token::Quote + # <-- search for Quote Each element of the type PPI::Token::Quote will be proposed for edit ( +the content). insert your new input terminating it with CTRL-Z on a empty line. use a bare ENTER to skip the current element STATEMENT: croak "No arg!" unless $_[0]; + # statement including the PPI::Element CONTENT: "No arg!" + # content of the PPI::Element itself EDIT:"No arguments!" + # a minimal change EDIT:^Z storing cache hex_id: 057d482fed857c338ec5ff25f2312900 in last_hex_id_ +file.sto Enter a filename if you want to save the current version (or ENTER to +skip) OUTPUT FILE:./Exp-bis.pm + # save to a new file to play nicely.. C>diff Exp.pm Exp-bis.pm + # check it out! 17c17 < croak "No arg!" unless $_[0]; --- > croak "No arguments!" unless $_[0];

There are no rules, there are no thumbs..
Reinvent the wheel, then learn The Wheel; may be one day you reinvent one of THE WHEELS.

Log In?
Username:
Password:

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

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

    No recent polls found