Skip to content

Instantly share code, notes, and snippets.

@adkinss
Created August 12, 2016 15:36
Show Gist options
  • Save adkinss/3762edee5eb6ef3a4e80241259ac49fd to your computer and use it in GitHub Desktop.
Save adkinss/3762edee5eb6ef3a4e80241259ac49fd to your computer and use it in GitHub Desktop.
#!/usr/bin/perl -w
use strict;
###
## This demonstrates how you can include BEGIN and END blocks in
## a perl script, but they don't necessarily execute in the order
## that they appear in the script. Here is the output when run:
##
## $ ./begin_end_perl_test.pl
## BEGIN MAIN
## BEGIN FOREACH
## BEGIN SUB
## inside MAIN
## a
## b
## c
## END SUB
## END FOREACH
## END MAIN
###
BEGIN { print "BEGIN MAIN\n" }
print "inside MAIN\n";
END { print "END MAIN\n" }
foreach ( qw (a b c) ) {
BEGIN { print "BEGIN FOREACH\n" }
print "$_\n";
END { print "END FOREACH\n" }
}
exit 0;
sub hello {
BEGIN { print "BEGIN SUB\n" }
print "inside SUB\n";
END { print "END SUB\n" }
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment