Often you'll see 1st time posters here stuck with code that looks like this

my $var = 4; my $bar; for(...){ ... ... ... while(...){ for(...){ ... ... ... } ... ... } ... ... ... } ... print $foo;

Or they go one step further and have some subs but still no main

my $var = 4; my $bar; ... sub bar { $bar = 42; ..} print "bar is $bar\n"; bar($bar); boo($var); sub boo { ... } ... sub foo { ... } print "bye\n";

But they can't figure out why $bar is undef when printed or or foo() is never called and boo is called twice or goo is called after foo when it should be before foo...

Its easy to understand why, the "main" program is only 40 lines but its mixed in between sub definitions and spread over the whole file, instead of just 40 consecutive lines

its hard for humans to read, like writing your whole program on one line  my $foo; my $bar; for(...){ while(...){...} ... } ...

perl doesn't have a problem with it, even a lot of humans can read it, but its slow going and difficult for no reason -- why would you trust a programmer that chooses to write code in a way that is hard for humans to read ?

programs aren't newspapers, mixing main/subs like ads/otherarticles isn't good for business

if you've ever tried teaching small children to write, they like mixing stuff , so for example to write apple, sometimes they'll start by drawing l because its the easiest, then go back and draw one p, then back and draw another p, then draw half an a (a little circle o), then draw half an e (little circle o), then add the j-part to complete the a, then draw the j-part to complete the e ...

so maybe there is a missing p or an extra p or the P's are backwards or the E is backwards or ....

mixing the main program in between sub definitions is like children learning to draw letters and write words all jumbled up and upside down :D

Also , The free pdf book Modern Perl has something to say about it on page 166 in "Handling Main"


In reply to Re: main() sub main{} ( Main( @ARGV ); exit( 0 ); by Anonymous Monk
in thread main() sub main{} by Anonymous Monk

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



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.