The BEGIN condition.
I know it's done at the start of a program sometimes, like with CGI::Carp. Like (code from perlman):
BEGIN {
use CGI::Carp qw(carpout);
open(LOG, ">>/usr/local/cgi-logs/mycgi-log") or
die("Unable to open mycgi-log: $!\n");
carpout(LOG);
}
What's it's purpose?
Matt@Ikonboard posted this at 02:55 — 10th January 2001.
They have: 16 posts
Joined: Jan 2001
The BEGIN { } routine forces whatever code is in there to run at compile time.
This makes it very handy to unshift the @INC for custom paths when using 'use' or 'require'.
Basically, anything you need to do before the code is executed can be put in there.
my $friends = qq[=:Jilly , Andrew , Peter & Harry:=];
print &welcome($friends), "2"; my @home;
sub welcome{my $s=shift; $s=~s{^(=\(.+?)(\:=)$}
{$_=$2;@home=split/[&,]/;}esgx;$friends= join'me',@home;
$friends=~s{(\A|\S|\s+)([A-Z]).+?(\s|\Z)}{$2}sge;my$c=-1;
$friends=~s{(me|\Z)}{++$c;@_=(qw|ust nother erl acker|);qq!$_[$c] !;}eg; $friends}
jcokos posted this at 18:28 — 16th January 2001.
They have: 3 posts
Joined: Dec 1999
We'll use BEGIN {} blocks to force some things that we know we need prior to the program actually running.
In most CGI programs, it's not necessary to do this, but if you're taking advantage of mod_perl or fastCGI, you can manipulate the begin blocks to preload some variables.
Since the BEGIN only gets evaluated at compile time, in a mod_perl program, the BEGIN block gets run when apache first loads the program into memory, and from that point on, your program has access to anything inside the BEGIN block.
as an example:
package foo;
## This gets run when the CGI is first loaded by apache
BEGIN { use vars qw ( %config );
BEGIN {
## Maybe read in some settings from a file.
open ("CFG", "/path/to/my/configfile");
while(<CFG>) {
chomp;
my ($var, $val) = split(/\=/);
$config{$var} = $val;
}
close(CFG);
## Bring in some modules ...
use DB_File;
use File::Copy;
}
## All the stuff from the BEGIN Blocks is read in.
## If we're running under mod_perl or fastCGI, the
## routine below never needs gets run, since the
## %config hash was loaded when it was compiled
## the first time you ran it. %config will
## always be ready within the program from this point
## forward, saving your program the overhead of opening
## up and reading that file each time the .cgi gets called....
##
## The same goes for the 2 "use" lines.
## Since perl's smart enough to know that it already
## used them in the begin block, it will skip right
## over them, again, saving you precious execution time.
## When running in non- mod_perl or fastCGI mode, they'll
## just get "used" over and over.
unless ( defined %config ) {
open ("CFG", "/path/to/my/configfile");
while(<CFG>) {
chomp;
my ($var, $val) = split(/\=/);
$config{$var} = $val;
}
close(CFG);
}
Hope that cleared things up a bit
John
Want to join the discussion? Create an account or log in if you already have one. Joining is fast, free and painless! We’ll even whisk you back here when you’ve finished.