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

Hey Forum!

My first post here is appropriately themed as I am asking about how to start up a perl programme.

I have been dabbling in perl for about a year, but recently started to get into it more seriously (albeit strictly as a hobbyist). I come from a self-taught C++/ASM background and so the freeform nature of perl is quite a culture shock. Specifically I find it hard to mentally deal with the lack of a perl version of main()

Is it acceptable to cobble-together a sort of jury-rigged main() by enclosing my 'entry-point' code in something like:

sub main_routine(){ ... }

and then as the first line of code in the programme call:

&main_routine();

I say 'acceptable' because I can't help thinking imposing that kind of structure on to perl might turn around and bite me somewhere sensitive in the future!

Replies are listed 'Best First'.
Re: creating a perl entry-point
by roboticus (Chancellor) on Jan 03, 2014 at 17:14 UTC

    morelenmir:

    There's no problem with that at all. Just leave off the & from the call, and the parenthesis off your sub definition, and you're golden. (Subroutine prototypes aren't the same as in C++, so don't use 'em.)

    Update: Fixed grammar and mentioned prototype...

    ...roboticus

    When your only tool is a hammer, all problems look like your thumb.

Re: creating a perl entry-point
by boftx (Deacon) on Jan 03, 2014 at 17:39 UTC

    roboticus has good points. I would add the following only as an additional consideration:

    use strict; use warnings; main(); # always have an exit from a script, it will save you grief! exit; sub main { ... # do some really cool stuff here # always have a known return value, just falling off the end WILL +bite # bite you someday. return; } __END__

    The use of exit and __END__ can prevent unexpected additions to your script for one reason or another. It is also handy to have all your POD (you do document your code, right?) after the __END__ statement.

    It helps to remember that the primary goal is to drain the swamp even when you are hip-deep in alligators.
Re: creating a perl entry-point
by Your Mother (Archbishop) on Jan 03, 2014 at 17:59 UTC

    Update: HA! boftx posted almost exactly this (and I endorse the __END__ bit as well) while I didn't refresh the OP to notice... Great minds, I say!

    In addition to what roboticus said, here's a skeleton of what you are suggesting-

    #!/usr/bin/env perl use strict; use warnings; main_routine(); exit; sub main_routine { # lots of stuff. }

    Keeping your subs separate with an exit keeps code accidents away pretty well.

Re: creating a perl entry-point
by morelenmir (Beadle) on Jan 03, 2014 at 20:57 UTC

    MANY thanks chaps!!!

    That tip about using exit is a damn good idea. I can see how if you miss it out the flow of execution might run on and then catch up with the other functions and... Messy!

    In regards scripting - I actually got started with perl last year as an alternative to the bloody awful batch file language in windows. The ability to properly manipulate strings and extract text/returns from console applications makes all the difference in the world! ESPECIALLY the strings! I know some fellows rave over 'PowerShell' but I could never quite wrap my head around it. Anyway - after the rigours of C/C++ perl is just so... PLEASANT to use!

Re: creating a perl entry-point
by aitap (Curate) on Jan 03, 2014 at 20:46 UTC
    Yes, it is acceptable. Some people use it when they write Perl files that are both modules (they cay be used/required, they declare a namespace (package), they possibly export subroutines) and ordinary perl programs (they do useful work when run via perl file.pm). At the end of the file there is usually a line:
    main unless caller;
    which checks whether the file is called as a module and runs the "main" sub if not.
Re: creating a perl entry-point
by locked_user sundialsvc4 (Abbot) on Jan 03, 2014 at 19:59 UTC

    This is where you do see the origins of Perl as “a scripting language.”   You will, in the end, “invoke some ‘script’ (a .pl file),” and this file will “simply consist of” some set of executable statements, which will be executed.   In effect, then, this file, in its entirety, is your void main().

    Modern-day Perl is, however, “a modular language,” so it is quite to be expected that your “.pl file” will mostly consist of use xxx; statements (thereby referring to xxx.pm files that are to be found somewhere in the @INC search-list ...) such that most of the actual meat-and-potatoes of the total application will actually be found there.

    In the Perl language (and its many brethren ...) you certainly can see a very sharp distinction with the world of pure-compiled languages such as C(++) ... a great many more things happen “on the fly,” and it’s okay, because, all-the-time and throughout everything, “the Perl interpreter is there,” and its magnificent perlguts.   We stand on the shoulders of giants.

Re: creating a perl entry-point
by Preceptor (Deacon) on Jan 04, 2014 at 12:06 UTC

    Specifying &main_routine() to call it works, but bypasses any prototypes. That's typically not what you want. (If there's no prototype, it's moot, but if you've specified one... well, why are you trying to bypass it?)

    Otherwise - matter of preference. I tend to simply write my 'main' at the bottom of the program, which is generally short and calls a bunch of subs. Sometimes there'll be a comment indicating where it starts. Nothing wrong with putting most of your code into defined subs, but ... well, perl starts at a very simple 'one line to do something' model, and grows from there - so you won't find a 'main' a common thing.