开发者

Method to understand existing perl code? [closed]

开发者 https://www.devze.com 2023-02-10 22:48 出处:网络
Closed. This question needs to be more focused. It is not currently accepting answers. Want to improve this question? Update the question so it focuses on one problem only by editing this
Closed. This question needs to be more focused. It is not currently accepting answers.

Want to improve this question? Update the question so it focuses on one problem only by editing this post.

Closed 4 years ago.

Improve this question 开发者_如何学运维

I've got some perl code (about 300-500 lines) that I've got to get working again. I've got limited experience programming, and normally if I'm coding I just find the best solution that make sense to me as is. In this case, I've got to use this code because it's built for an existing legacy system, for which the code is the documentation for the wiring, logic and rendering of the system. It's not a lot of code, but I also can't post even chunks of code or data to get help. What's the best method of understanding the syntax, what it's doing, how the code is wired, how the logic is model, etc.

Questions, feedback, comments -- just comment, thanks!!


See the book Perl Medic, Transforming Legacy Code, by Peter Scott, from 2004.
A few review notes, including a table of contents, are listed at this perlmonks node.
A brief review is here.

The book covers many techniques in enough depth to learn & apply them to your problem. If you only have a little time, scan the whole book, then I recommend Chapter 3 on testing, Chapter 4 on rewriting, and Chapter 11, A Case Study (30+ pages).

If you don't use this book, at least use perldoc to learn Test::More and related modules. Having useful tests that exercise the original and modified code will build your confidence in making changes, because you can see when a specific change causes a test to fail.

Update.

See this book for more detailed data on Perl's test tools than given in Perl Medic: Perl Testing: A Developer's Notebook, by Ian Langworth & chromatic, 2006.


Some time ago I read nice text about understanding large project quickly on perlmonks - Swallowing an elephant in 10 easy steps. There are many useful suggestions that can pay off.


Have a look for tools like perltidy which will regularize the formatting.

Also consider running the code in the debugger, and stepping through line by line. See perldoc perldebug for details.


My advice would be to start with the functions... go through the code, find everywhere a function is used and determine if it is a regular perl function that is universal, or if it is a custom function. Then search the code and find where all of the custom functions are created and determine what each individual function does.

Add comments to the code as you go, once you figure out what a function is doing, add a comment.

That alone should give you a good head start.

In more nitty gritty, you may want to go through after that and label/comment/make note of what each perl variable is used for/what it is the first time it is used.

That should get you well on your way to figuring the code out... and if there is a function or something else you can't figure out, search the web using the wonderful Google... or post here not necessarily with the exact code, as you said you can't, but with a general idea of what it is doing.

Also, one thing I forgot to mention, is to find any loops, whether while, for, etc and determine what is running inside of them and what they are being looped through.


There have been some good suggestions already, another one is the B::Deparse module, which attempts to rewrite your code (often) in a longer clearer way.

For example when you run perl -MO=Deparse ob.pl on the file ob.pl containing this obfuscated code:

@P=split//,".URRUU\c8R";@d=split//,"\nrekcah xinU / lreP rehtona tsuJ";sub p{
@p{"r$p","u$p"}=(P,P);pipe"r$p","u$p";++$p;($q*=2)+=$f=!fork;map{$P=$P[$f^ord
($p{$_})&6];$p{$_}=/ ^$P/ix?$P:close$_}keys%p}p;p;p;p;p;map{$p{$_}=~/^[P.]/&&
close$_}%p;wait until$?;map{/^r/&&<$_>}%p;$_=$d[$q];sleep rand(2)if/\S/;print

You get:

@P = split(??, '.URRUUxR', 0);
@d = split(??, "\nrekcah xinU / lreP rehtona tsuJ", 0);
sub p {
    @p{"r$p", "u$p"} = ('P', 'P');
    pipe "r$p", "u$p";
    ++$p;
    ($q *= 2) += $f = !fork;
    map {$P = $P[$f ^ ord $p{$_} & 6];
    $p{$_} = / ^$P/xi ? $P : close $_;} keys %p;
}
p ;
p ;
p ;
p ;
p ;
map {close $_ if $p{$_} =~ /^[P.]/;} %p;
wait until $?;
map {<$_> if /^r/;} %p;
$_ = $d[$q];
sleep rand 2 if /\S/;
print $_;
ob.pl syntax OK

Which is (possibly) better; I don't know, maybe not. Worth a try anyway.

Edit: you can get even a little more info by changing the command to perl -MO=Deparse,-p ob.pl which puts in explicit parentheses which can help understand operator precedence.


Aren't there any comments in the program that can let you know what each line or each function is doing?

I would go through the code from start to finish. Using a pen and lots of paper. This were the author of the program should have written test cases, so that new programmers, like yourself, can understand how the code works. Not many programmers write test cases.

0

精彩评论

暂无评论...
验证码 换一张
取 消