Start from a main routine: Difference between revisions

From Rosetta Code
Content added Content deleted
m (moved omit template out of AWK.)
Line 5: Line 5:
Languages that always run from main() can be omitted from this task.
Languages that always run from main() can be omitted from this task.


=={{header|AWK}}==
{{omit from|Python}}
{{omit from|Python}}
=={{header|AWK}}==


The awk language is data driven. However, it does support the use of begin blocks, so we could use one of those to provide us with a main startup procedure:
The awk language is data driven. However, it does support the use of begin blocks, so we could use one of those to provide us with a main startup procedure:

Revision as of 12:11, 5 August 2011

Start from a main routine is a draft programming task. It is not yet considered ready to be promoted as a complete task, for reasons that should be found in its talk page.

Some languages (like Gambas and Visual Basic) support two startup modes. Applications written in these languages start with an open window that waits for events, and it is necessary to do some trickery to cause a main procedure to run instead. Data driven or event driven languages may also require similar trickery to force a startup procedure to run.

The task is to demonstrate the steps involved in causing the application to run a main procedure, rather than an event driven window at startup.

Languages that always run from main() can be omitted from this task.

AWK

The awk language is data driven. However, it does support the use of begin blocks, so we could use one of those to provide us with a main startup procedure:

<lang awk>BEGIN {

 # This is our main startup procedure
 print "Hello World!"

}</lang>

Gambas

In Gambas, to make an application startup from a main routine:

  • Create a new module called MMain
  • In the MMain module, create a public sub called Main as follows:

<lang gambas>PUBLIC SUB Main()

 ' This is the start of the program

END</lang>

  • Right click the MMain module, then select Startup class from the context menu

J

J, by default, starts an event loop.

If a file name is specified on the command line, that file is executed before dropping into the event loop.

Thus, if the script issues an exit command, that will happen before the event loop executes.

If you want the script to exit even when it hits an error, you can use an immex phrase, which will be the first thing executed by the event loop, before it prompts.

Perl 6

When executed with the standard setting, Perl 6 code always runs the mainline code automatically, followed by the MAIN function if you have one. However, it's possible to start up with an alternate setting that might want to create its own event loop or MAIN. In such cases you can always capture control at various phases with blocks that we call "phasers": <lang perl6>BEGIN {...} # as soon as parsed CHECK {...} # end of compile time INIT {...} # beginning of run time END {...} # end of run time</lang>

PureBasic

PureBasic is procedural and any code which is not part of a procedure is considered 'main' code. This code also does not use any explicit syntax (i.e. a 'main' module) to cause it to execute and it always executes first.

sed

A sed program repeats itself for each line of input, but your program can begin with commands that address line 1. (This requires that your input has a line 1. If your input is empty file, like /dev/null, then it is impossible to run commands.)

<lang sed># This code runs only for line 1. 1 { i\ Explain-a-lot processed this file and i\ replaced every period with three exclamation points!!! i\

}

  1. This code runs for each line of input.

s/\./!!!/g</lang>

Visual Basic

In Visual Basic to make an application startup from a main routine:

  • Create a new module called MMain.bas
  • Create a new subroutine in the new module as follows:

<lang vb>SUB Main()

 ' This is the start of the program

END</lang>

  • From the menu in the application development environment, choose: File, Project Options.
  • Ensure that MMain.bas is selected, by clicking in the list
  • From the pulldown list, choose "Sub Main"
  • Click the OK button.

ZX Spectrum Basic

On the ZX Spectrum, there is no main function as such. However a saved program can be made to start running from a particular line number by providing the line number as a parameter to the save command. The following example will save the program in memory so that it starts running from line 500:

<lang zxbasic>SAVE "MYPROG" LINE 500: REM For a program with main code starting at line 500</lang>