Beefy Boxes and Bandwidth Generously Provided by pair Networks
XP is just a number
 
PerlMonks  

organizing program structure

by dr_jkl (Acolyte)
on Jul 22, 2010 at 21:42 UTC ( #850921=perlquestion: print w/ replies, xml ) Need Help??
dr_jkl has asked for the wisdom of the Perl Monks concerning the following question:

hi,

i'm new to perl and finding it a lot of fun as i write small programs to learn from. i have a question regarding program organization.

if i have a program that can perform a few different functions based on input, it seems sensible to me to use subroutines to direct the path of the program. except, i've been told i shouldn't use subroutines in perl programs but i'm not entirely sure why. it seems there's a general hatred of gotos and whatnot in perl, and i'd like to understand why.

i'd also like to know- if not subroutines, then what would you use in place of them?

thank you!

Comment on organizing program structure
Re: organizing program structure
by morgon (Deacon) on Jul 22, 2010 at 21:48 UTC
    i've been told i shouldn't use subroutines in perl programs

    You must have misunderstood that or someone was making fun of you. Do use subroutines (a lot),

    it seems there's a general hatred of gotos

    That is something else entirely.

    Use subroutines (it makes the code much clearer) but avoid gotos if you can (usually gotos make code harder to understand and change).

Re: organizing program structure
by roboticus (Canon) on Jul 22, 2010 at 21:48 UTC

    dr_jkl:

    Subroutines are definitely a good thing. It sounds like you just gotten some bad advice. Gotos are generally frowned on for various reasons. With subroutines and perls looping constructs, though, you probably won't need any goto statements.

    ...roboticus

Re: organizing program structure
by almut (Canon) on Jul 22, 2010 at 21:49 UTC
    i've been told i shouldn't use subroutines in perl programs

    Kind of unusual advice.  Maybe whoever said so was a fan of object oriented structuring and wanted to make a distinction between normal procedural suboutines and object methods?

    Anyhow, feel free to use them as you see fit.

    Or, in executive summary style:  subroutines are good, goto is bad ;)

      Or, in executive summary style: subroutines are good, goto is bad ;)

      E_TOOVERBOSE: s/ (are|is) / /g :-)

      --MidLifeXis

Re: organizing program structure
by ahmad (Hermit) on Jul 22, 2010 at 21:51 UTC

    I've never written any Perl script without subroutines (known as functions in other programming languages).

    You don't need to use goto, you define subroutine and call it like this:

    # define it sub Add { my ($x,$y) = @_; return $x + $y; } # call it print Add(5,3); # it will print out 8

    You may want to tell us what's the problem you are trying to solve so we can help you deciding what would work for you.

Re: organizing program structure
by BrowserUk (Pope) on Jul 22, 2010 at 21:51 UTC
    i've been told i shouldn't use subroutines in perl programs

    Whomever told you that, either doesn't know what they're talking about; or you've misunderstood them. The latter conclusion seems possible as you seem to equate subroutines with "gotos"; which are quite separate things.

    Perhaps you could write and post a ~20 line program that uses a subroutine. We might then be able to get to the bottom of the confusion.


    Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
    "Science is about questioning the status quo. Questioning authority".
    In the absence of evidence, opinion is indistinguishable from prejudice.
Re: organizing program structure
by jethro (Monsignor) on Jul 22, 2010 at 21:59 UTC

    You must have misheard. Look at some perl libraries or the scripts in the tutorial section of this site. You will find lots of subroutines.

    There is a general "hatred" of goto, yes. But advice to not use goto was already voiced more than 20 years ago when bad programming style was prevalent in the old programming language BASIC. No matter what language you will use (except for machine language) you will get warned about using goto. goto was the first symbol of bad programming and it still is an (almost sure) sign for bad code

    But that has nothing to do with subroutines. We love subroutines. Subroutines are your first line of defense against unmanagable unreadable code

      But advice to not use goto was already voiced more than 20 years ago when bad programming style was prevalent in the old programming language BASIC.
      One of the earliest "warnings" against the use of goto is Dijkstra's letter Go To Statement Considered Harmful (the title is by Niklaus Wirth, the editor of Communications of the ACM at the time - CACM is journal that printed Dijkstra's letter), published, IIRC, in 1968.

      Dijkstra's argument was that most uses of goto makes it impossible to do structured programming. Note also that Dijkstra would make the same objections against constructs we all love: return, last, redo, next, eval. Just like goto, they allow blocks to have more than one exit point.

      But that has nothing to do with subroutines.
      Well, if I weren't able to use subroutines, I'd probably use goto a lot more than I do now.
Re: organizing program structure
by ikegami (Pope) on Jul 22, 2010 at 22:37 UTC

    i've been told i shouldn't use subroutines [...]. it seems there's a general hatred of gotos

    Unless you are jumbling two topics, you are misusing the term "subroutine". A subroutine looks like the following in Perl:

    sub foo { ... }

    They enforce return to the correct point in the program. They provide encapsulation by limiting the scope of their private variables. They provide communication channels with the outside world (the parameter list and the return list) allowing looser coupling. The are re-entrant, allowing recursion. They are visible in a call stack, helping debugging. etc.

Re: organizing program structure
by toolic (Chancellor) on Jul 23, 2010 at 00:31 UTC
Re: organizing program structure
by cdarke (Prior) on Jul 23, 2010 at 09:03 UTC

      Wow, is this for real? Subroutines are bad but methods are good. The only difference is their names. Also, you do use gotos but you call them names like "last" and "next" (for more details, see `perldoc -f last` and `perldoc -f next`.

      I won't get into the reasons why everyone uses gotos but they do. Just don't call them "gotos"; use the other names the language designers thought up for them. :)

Re: organizing program structure
by ivo_ac (Acolyte) on Jul 23, 2010 at 21:13 UTC
    You are definitely mixing up the use of GOTO statements and the use of subroutines. With goto, your program looks like spaghetti, it jumps from one place to the next and after a time you really cant untangle it. By using subroutines (which are equal to C++ functions) you can divide your program into parts which perform different actions. If you also use lexical variables (my variables) and parameters (in Perl, these are in @_) you have a good programming style. With object oriented programming, you can divide even more. Not only can you divide the actions in subroutines (called methods in object oriented programming) but also in different objects. But for now if your new, i would advice to use subroutines and wait a little while with object oriented Perl.

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://850921]
Approved by ahmad
Front-paged by planetscape
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others examining the Monastery: (3)
As of 2014-07-31 02:06 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    My favorite superfluous repetitious redundant duplicative phrase is:









    Results (244 votes), past polls