Typically, scripts don't include the already-inclusive main as anonymonk pointed out. Where it is a bit more idiomatic, is when including a package within a file, along with the main code (I *only* use this for examples though). First (use warnings; use strict; is implied here):
package main; {
...
}
Is effectively the exact same as:
{
...
}
...in a script as far as scoping is concerned. With the braces, the scope is determined. Without the braces, everything still resides in main, but is scoped at the file level, not at the block level. Now, onto the package and script in one file example (untested):
package Blah; {
sub foo {
print "bar!\n";
}
}
package main; {
foo();
}
I believe that it allows the reader at a quick glance to see where the script begins.
It is very unusual in Perl code to see main referenced directly. Sometimes hacking at the symbol table you'll see it for illustration purposes, but anything without a package prepended is main anyways:
perl -wMstrict -E 'my $x; say *::x; say *main::x'
*main::x
*main::x
To further, in say C, the main function must be defined explicitly, and that function handles any incoming arguments:
int main (int argc, char* argv[])
In Perl, the args happen at the file level (even *if* you scope main):
my ($x, $y);
if (@ARGV == 2){
($x, $y) = @ARGV;
}
else {
...
}
package main; {
print "$x, $y\n";
}
In conclusion, with most compiled languages I write in, main is considered the "entry-point". In Perl scripts, there isn't really such a definition, and regardless of whether packages are declared or not, the entry point is the first executable line of code found. If none are found outside of a package declaration, it'll execute the first line of code within a main package, if found. If not, it'll do its compile time stuff, and nothing else (does this sound right all?).
update: note that in Python, you don't necessarily *need* main either. Often, it's explicitly expressed when you want a library to optionally operate as a script as well, and the main definition would end up at the bottom of the library, with the:
if __name__ == '__main__':
main()
For a Python file that you intend only to use as a script, no reference to main is necessary at all. Eg:
x = 1
y = 2
print(x + y)
|