Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8080633
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T16:28:44+00:00 2026-06-05T16:28:44+00:00

how do you decide whether to put code in a subroutine or in main

  • 0

how do you decide whether to put code in a subroutine or in main portion of a Perl script?

I understand if you need to re-use code you would put it in subroutine, but what if you’re not reusing? Is it good practice to put in subroutines to group code and make it easier to read?

Example (in main):

my ($num_one, $num_two) = 1, 2;
print $num_one + $num_two;
print $num_one - $num_two;

Example (in subroutines):

 my ($num_one, $num_two) = 1, 2;
 add($num_one, $num_two);
 subtract($num_one, $num_two);

 sub add {
     my ($num_one, $num_two) = @_;
     return $num_one + $num_two;
 }

 sub subtract {
     my ($num_one, $num_two) = @_;
     return $num_one - $num_two;
 }    

I know this is a simple example, but for more complex code (if sub add and sub subtract had more code), would it make sense to group in subroutines?

Thanks!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-05T16:28:45+00:00Added an answer on June 5, 2026 at 4:28 pm

    You’ve pointed out that code is easier to reuse if it’s in a subroutine. I think there are at least three other good reasons for using subroutines.

    1. If your subroutines are well-written (by which I mean that they don’t use global variables, they have well-defined return points and they are small – certainly no more than a screen-full of code) then you can work on the code in the subroutine without caring about the rest of the code. And that makes your code easier to maintain.
    2. Well-named subroutines make your code easier to follow. If your main line of execution just calls half a dozen well-named subroutines then it is instantly obvious what your program does.
    3. Code in subroutines is far easier to test than code outside of subroutines. Even better if your subroutines are in a separate library. The test suite can just load the library and try each of the subroutines in turn, checking that it has the correct effect.

    These points are obviously all simplifications. But they go some way towards explaining why the vast majority of my code is in subroutines.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I know you shouldn't use this to decide whether or not to change an
I'm trying to decide whether or not it makes sense to go through the
I'm trying to decide whether to use James( http://james.apache.org/ ) or Javamail( http://java.sun.com/products/javamail/ )
I'm trying to decide whether to use the reduce() method in Javascript for a
NOTE: This question is tricky (to decide whether it pertains to SO or not),
I'm trying to decide whether to use a Rails or a Django guru to
I'm trying to decide whether I should use the runkit php extension for my
I'm running testing scenarios to decide whether or not to implement a system using
Please help me decide whether I should use a function's prototype object and the
I'm trying to decide whether I should create a simple StackPanel with Buttons on

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.