Beefy Boxes and Bandwidth Generously Provided by pair Networks
The stupid question is the question not asked
 
PerlMonks  

main() sub main{}

by Anonymous Monk
on Nov 09, 2015 at 19:20 UTC ( [id://1147282]=perlquestion: print w/replies, xml ) Need Help??

Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

what is the advantage of using coding style:
main(); sub main { . . . };

Replies are listed 'Best First'.
Re: main() sub main{}
by runrig (Abbot) on Nov 09, 2015 at 19:38 UTC
    There is some explanation in this post. But it (and I) suggest using "Main" instead of "main".
Re: main() sub main{} ( Main( @ARGV ); exit( 0 );
by Anonymous Monk on Nov 09, 2015 at 23:45 UTC

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

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

    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"

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://1147282]
Approved by GrandFather
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others wandering the Monastery: (4)
As of 2024-04-24 02:45 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found