.\" Automatically generated by Pod::Man 4.14 (Pod::Simple 3.40) .\" .\" Standard preamble: .\" ======================================================================== .de Sp \" Vertical space (when we can't use .PP) .if t .sp .5v .if n .sp .. .de Vb \" Begin verbatim text .ft CW .nf .ne \\$1 .. .de Ve \" End verbatim text .ft R .fi .. .\" Set up some character translations and predefined strings. \*(-- will .\" give an unbreakable dash, \*(PI will give pi, \*(L" will give a left .\" double quote, and \*(R" will give a right double quote. \*(C+ will .\" give a nicer C++. Capital omega is used to do unbreakable dashes and .\" therefore won't be available. \*(C` and \*(C' expand to `' in nroff, .\" nothing in troff, for use with C<>. .tr \(*W- .ds C+ C\v'-.1v'\h'-1p'\s-2+\h'-1p'+\s0\v'.1v'\h'-1p' .ie n \{\ . ds -- \(*W- . ds PI pi . if (\n(.H=4u)&(1m=24u) .ds -- \(*W\h'-12u'\(*W\h'-12u'-\" diablo 10 pitch . if (\n(.H=4u)&(1m=20u) .ds -- \(*W\h'-12u'\(*W\h'-8u'-\" diablo 12 pitch . ds L" "" . ds R" "" . ds C` "" . ds C' "" 'br\} .el\{\ . ds -- \|\(em\| . ds PI \(*p . ds L" `` . ds R" '' . ds C` . ds C' 'br\} .\" .\" Escape single quotes in literal strings from groff's Unicode transform. .ie \n(.g .ds Aq \(aq .el .ds Aq ' .\" .\" If the F register is >0, we'll generate index entries on stderr for .\" titles (.TH), headers (.SH), subsections (.SS), items (.Ip), and index .\" entries marked with X<> in POD. Of course, you'll have to process the .\" output yourself in some meaningful fashion. .\" .\" Avoid warning from groff about undefined register 'F'. .de IX .. .nr rF 0 .if \n(.g .if rF .nr rF 1 .if (\n(rF:(\n(.g==0)) \{\ . if \nF \{\ . de IX . tm Index:\\$1\t\\n%\t"\\$2" .. . if !\nF==2 \{\ . nr % 0 . nr F 2 . \} . \} .\} .rr rF .\" ======================================================================== .\" .IX Title "Refactor 3pm" .TH Refactor 3pm "2021-01-04" "perl v5.32.0" "User Contributed Perl Documentation" .\" For nroff, turn off justification. Always turn off hyphenation; it makes .\" way too many mistakes in technical documents. .if n .ad l .nh .SH "NAME" Devel::Refactor \- Perl extension for refactoring Perl code. .SH "VERSION" .IX Header "VERSION" \&\f(CW$Revision:\fR $ This is the \s-1CVS\s0 revision number. .SH "SYNOPSIS" .IX Header "SYNOPSIS" .Vb 1 \& use Devel::Refactor; \& \& my $refactory = Devel::Refactor\->new; \& \& my ($new_sub_call,$new_sub_code) = \& $refactory\->extract_subroutine($sub_name, $code_snippet); \& \& my $files_to_change = $refactory\->rename_subroutine(\*(Aq./path/to/dir\*(Aq, \& \*(AqoldSubName\*(Aq,\*(AqnewSubName\*(Aq); \& # $files_to_change is a hashref where keys are file names, and values are \& # arrays of hashes with line_number => new_text .Ve .SH "ABSTRACT" .IX Header "ABSTRACT" Perl module that facilitates refactoring Perl code. .SH "DESCRIPTION" .IX Header "DESCRIPTION" The \fBDevel::Refactor\fR module is for code refactoring. .PP While \fBDevel::Refactor\fR may be used from Perl programs, it is also designed to be used with the \fB\s-1EPIC\s0\fR plug-in for the \fBeclipse\fR integrated development environment. .SH "CLASS METHODS" .IX Header "CLASS METHODS" Just the constructor for now. .SS "new" .IX Subsection "new" Returns a new \fBDevel::Refactor\fR object. .SH "PUBLIC OBJECT METHODS" .IX Header "PUBLIC OBJECT METHODS" Call on a object returned by \fBnew()\fR. .SS "extract_subroutine($new_name,$old_code [,$syntax_check])" .IX Subsection "extract_subroutine($new_name,$old_code [,$syntax_check])" Pass it a snippet of Perl code that belongs in its own subroutine as well as a name for that sub. It figures out which variables need to be passed into the sub, and which variables might be passed back. It then produces the sub along with a call to the sub. .PP Hashes and arrays within the code snippet are converted to hashrefs and arrayrefs. .PP If the \fIsyntax_check\fR argument is true then a sytax check is performed on the refactored code. .PP Example: .PP .Vb 12 \& $new_name = \*(AqnewSub\*(Aq; \& $old_code = <<\*(Aqeos\*(Aq; \& my @results; \& my %hash; \& my $date = localtime; \& $hash{foo} = \*(Aqvalue 1\*(Aq; \& $hash{bar} = \*(Aqvalue 2\*(Aq; \& for my $loopvar (@array) { \& print "Checking $loopvar\en"; \& push @results, $hash{$loopvar} || \*(Aq\*(Aq; \& } \& eos \& \& ($new_sub_call,$new_code) = $refactory\->extract_subroutine($new_name,$old_code); \& # $new_sub_call is \*(Aqmy ($date, $hash, $results) = newSub (\e@array);\*(Aq \& # $new_code is \& # sub newSub { \& # my $array = shift; \& # \& # my @results; \& # my %hash; \& # my $date = localtime; \& # $hash{foo} = \*(Aqvalue 1\*(Aq; \& # $hash{bar} = \*(Aqvalue 2\*(Aq; \& # for my $loopvar (@$array) { \& # print "Checking $loopvar\en"; \& # push @results, $hash{$loopvar} || \*(Aq\*(Aq; \& # } \& # \& # \& # return ($date, \e%hash, \e@results); \& # } .Ve .PP Included in the examples directory is a script for use in \s-1KDE\s0 under Linux. The script gets its code snippet from the \s-1KDE\s0 clipboard and returns the transformed code the same way. The new sub name is prompted for via \s-1STDIN.\s0 .SS "rename_subroutine($where,$old_name,$new_name,[$max_depth])" .IX Subsection "rename_subroutine($where,$old_name,$new_name,[$max_depth])" \&\fIwhere\fR is one of: path-to-file path-to-directory .PP If \fIwhere\fR is a directory then all Perl files (default is \f(CW\*(C`.pl\*(C'\fR, \f(CW\*(C`.pm\*(C'\fR, and \f(CW\*(C`.pod\*(C'\fR See the \fBperl_file_extensions\fR method.) in that directory and its' descendents (to \fImax_depth\fR deep,) are searched. .PP Default for \fImax_depth\fR is 0 \*(-- just the directory itself; \&\fImax_depth\fR of 1 means the specified directory, and it's immeadiate sub-directories; \fImax_depth\fR of 2 means the specified directory, it's sub-directories, and their sub-directrories, and so forth. If you want to scan very deep, use a high number like 99. .PP If no matches are found then returns \fIundef\fR, otherwise: .PP Returns a hashref that tells you which files you might want to change, and for each file gives you the line numbers and proposed new text for that line. The hashref looks like this, where \fIold_name\fR was found on two lines in the first file and on one line in the second file: .PP .Vb 9 \& { \& ./path/to/file1.pl => [ \& { 11 => "if (myClass\->newName($x)) {\en" }, \& { 27 => "my $result = myClass\->newName($foo);\en"}, \& ], \& ./path/to/file2.pm => [ \& { 235 => "sub newName {\en"}, \& ], \& } .Ve .PP The keys are paths to individual files. The values are arraryrefs containing hashrefs where the keys are the line numbers where \fIold_name\fR was found and the values are the proposed new line, with \fIold_name\fR changed to \fInew_name\fR. .SS "is_perlfile($filename)" .IX Subsection "is_perlfile($filename)" Takes a filename or path and returns true if the file has one of the extensions in \fBperl_file_extensions\fR, otherwise returns false. .SH "OBJECT ACCESSORS" .IX Header "OBJECT ACCESSORS" These object methods return various data structures that may be stored in a \fBDevel::Refactor\fR object. In some cases the method also allows setting the property, e.g. \fBperl_file_extensions\fR. .SS "get_new_code" .IX Subsection "get_new_code" Returns the \fIreturn_snippet\fR object property. .SS "get_eval_results" .IX Subsection "get_eval_results" Returns the \fIeval_err\fR object property. .SS "get_sub_call" .IX Subsection "get_sub_call" Returns the \fIreturn_sub_call\fR object property. .SS "get_scalars" .IX Subsection "get_scalars" Returns an array of the keys from \fIscalar_vars\fR object property. .SS "get_arrays" .IX Subsection "get_arrays" Returns an array of the keys from the \fIarray_vars\fR object property. .SS "get_hashes" .IX Subsection "get_hashes" Returns an array of the keys from the \fIhash_vars\fR object property. .SS "get_local_scalars" .IX Subsection "get_local_scalars" Returns an array of the keys from the \fIlocal_scalars\fR object property. .SS "get_local_arrays" .IX Subsection "get_local_arrays" Returns an array of the keys from the \fIlocal_arrays\fR object property. .SS "get_local_hashes" .IX Subsection "get_local_hashes" Returns an array of the keys from the \fIlocal_hashes\fR object property. .SS "perl_file_extensions([$arrayref|$hashref])" .IX Subsection "perl_file_extensions([$arrayref|$hashref])" Returns a hashref where the keys are regular expressions that match filename extensions that we think are for Perl files. Default are \f(CW\*(C`.pl\*(C'\fR, \&\f(CW\*(C`.pm\*(C'\fR, and \f(CW\*(C`.pod\*(C'\fR .PP If passed a hashref then it replaces the current values for this object. The keys should be regular expressions, e.g. \f(CW\*(C`\e.cgi$\*(C'\fR. .PP If passed an arrayref then the list of values are added as valid Perl filename extensions. The list should be filename extensions, \s-1NOT\s0 regular expressions, For example: .PP .Vb 10 \& my @additonal_filetypes = qw( .ipl .cgi ); \& my $new_hash = $refactory\->perl_file_extensions(\e@additional_filetypes); \& # $new_hash = { \& # \*(Aq\e.pl$\*(Aq => 1, \& # \*(Aq\e.pm$\*(Aq => 1, \& # \*(Aq\e.pod$\*(Aq => 1, \& # \*(Aq\e.ipl$\*(Aq => 1, \& # \*(Aq\e.cgi$\*(Aq => 1, \& # \*(Aq\e.t$\*(Aq => 1, \& # } .Ve .SH "TODO LIST" .IX Header "TODO LIST" .IP "Come up with a more uniform approach to \fB\s-1ACCESSORS\s0\fR." 2 .IX Item "Come up with a more uniform approach to ACCESSORS." .PD 0 .IP "Add more refactoring features, such as \fIadd_parameter\fR." 2 .IX Item "Add more refactoring features, such as add_parameter." .IP "Add a \s-1SEE ALSO\s0 section with URLs for eclipse/EPIC, refactoring.com, etc." 2 .IX Item "Add a SEE ALSO section with URLs for eclipse/EPIC, refactoring.com, etc." .PD .SH "AUTHOR" .IX Header "AUTHOR" Scott Sotka, .SH "COPYRIGHT AND LICENSE" .IX Header "COPYRIGHT AND LICENSE" Copyright 2005 by Scott Sotka .PP This library is free software; you can redistribute it and/or modify it under the same terms as Perl itself.