Beefy Boxes and Bandwidth Generously Provided by pair Networks
Think about Loose Coupling
 
PerlMonks  

comment on

( #3333=superdoc: print w/replies, xml ) Need Help??
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!


In reply to Re: Code Optimization v5.8.1 by flyingmoose
in thread Code Optimization v5.8.1 by bluethundr

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":



  • Are you posting in the right place? Check out Where do I post X? to know for sure.
  • Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
    <code> <a> <b> <big> <blockquote> <br /> <dd> <dl> <dt> <em> <font> <h1> <h2> <h3> <h4> <h5> <h6> <hr /> <i> <li> <nbsp> <ol> <p> <small> <strike> <strong> <sub> <sup> <table> <td> <th> <tr> <tt> <u> <ul>
  • Snippets of code should be wrapped in <code> tags not <pre> tags. In fact, <pre> tags should generally be avoided. If they must be used, extreme care should be taken to ensure that their contents do not have long lines (<70 chars), in order to prevent horizontal scrolling (and possible janitor intervention).
  • Want more info? How to link or How to display code and escape characters are good places to start.
Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others lurking in the Monastery: (2)
As of 2023-02-09 05:22 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    I prefer not to run the latest version of Perl because:







    Results (44 votes). Check out past polls.

    Notices?