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

Re: Code Optimization v5.8.1

by flyingmoose (Priest)
on May 20, 2004 at 19:04 UTC ( [id://355048]=note: print w/replies, xml ) Need Help??


in reply to Code Optimization v5.8.1

I agree with simon and combatsquirrel in the way the program should be solved. In addition, you have recieved some good advice on picking up meryln's book ("Learning Perl" published by O'Reilly), (etc) to improve your style. Being on a boring telecom, I'll give your code a quick review though for some of the misconceptions you would like pointed out. Others can add more. Perl is weird in that "Perlishness" is kind of a zen, so writing baby-perl (as either Merlyn or Larry or Tom put it) is acceptable as you attempt to write more perl in the style of the perl idiom.

In my opinion (and all things are always subject to debate):

  • You use last and other tools to exit loops when your loop structure could be made simpler to eliminate this hopping around. Code feels a bit goto-ish. Consider subroutines if you want extra clarity.
  • Need to use 'my' to declare variables with scope rather than globally. Definitely enable 'use strict' and 'use warnings'. There are numerous reasons for this, which the books can explain in great detail.
  • Program should probably process STDIN and output to STDOUT given the problem description, rather than being an interactive tool
  • Stylistic, but no need to single quote items on the left side of '=>'
  • If you want to clear a variable, use 'undef $var' instead of sending it the size of an empty array with '$var=()'. If you write good code, you'll rarely need to clear a variable, scoping means you don't need globals. Globals are usually a sign of a design flaw.
  • Check for exit should probably read /yes|y/i instead of using what looks to be character classes... again, pick up Learning Perl or Programming Perl -- preferably both, as they cover this in depth
  • No reason to assign a string to $final if you can just print a message...again, this is a sign your loop logic needs to be refactored

These are just a bunch of random comments, but you were asking for them, so I thought I would share. As you grow as a Perl programmer you will learn new tweaks and stuff, and your code will grow more elegant and easy to understand. Enjoy the process, as Perl is a really fun language and allows for some neat constructs. Don't be worried that people are pointing out ways you can improve, this monastery is an incredible resource. Most of all, realize Perl is fun!

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others examining the Monastery: (5)
As of 2024-04-23 15:28 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found