in reply to when to use subroutine

This can be one of those divisive topics around the water cooler at work; and, as I'm seeing here, everyone has their own experience which leads to some similar answers and some different opinions on when to use subroutines. I'll throw my two-cents in as well.

For me, a subroutine is one of a number of solutions to help provide one of the following:

All of these things help to streamline your processing flow by reducing the amount of code someone (you or whoever might have to maintain it) has to read through to get a good understanding of what the code is doing.

Subroutines aren't the only way to provide this, and Perl, like many languages, offers a number of ways to provide modularization and code reuse. Subroutines can be separated into a separate file, a file of related subroutines could be made into a Module and even turned into an reusable Object.

All the commenters make good points, but I think you'll just have to take a look at your code while keeping these ideas in mind and see what makes sense.

---
echo S 1 [ Y V U | perl -ane 'print reverse map { $_ = chr(ord($_)-1) } @F;'

Replies are listed 'Best First'.
Re^2: when to use subroutine
by convenientstore (Pilgrim) on Oct 28, 2007 at 19:57 UTC
    thank you all,
    I think I definitely need to read up more on how to design the code and think like programmer in general before going into more complicated stuff in perl(mainly OO subject).
    Perl is awesome language for me because it would allow me to do amazing stuff at work that I wouldn't even dream'd of getting it done w/out it. Now that I took upon myself to write bit more complicated code at work, I see that lack of general computer science background is coming back to haunt me.

    It is something I feel I would like to study further on before digging deeper into the perl.(as this is really my first computer language). I just ordered "Pascal, an introduction to the art and science of programming" per someone's recommendation on the forum and also I would like to look into the book "The Pragmatic Programmer, The Practice of Programming, and Refactoring" Brian recommended(but it wasn't on amazon nor my collection of libraries' so I will have to dig deeper).

    My biggest problem is that I too also try to learn by reading someone's code but sometimes I wonder why such a simple operation(those one liner sub routine) would require its own function. "How a script becomes a module" was excellent reading for me and sort of gave me more insight to other's thoughts.
    Thank you again and back to the reading..

      The Pragmatic Programmer, The Practice of Programming, and Refactoring are three separate books. All three are worth reading (and they should be easier to find now).

        indeed, thank you