From: cate on
I have a large list of my vars that I would like to get out of the
way; place them at the end of the script - kinda a class thing.

Is there a way to use BEGIN some how? Something like this. I
suspect you can't, but I'm asking the pros.

use strict;
code using $var1 ...
code using $var1 ...
more code


BEGIN {
my $var1 = 'sfsdf';
my $var2 = 'sdfsdf';
}

thank you
From: Marc Girod on
On Jan 21, 2:12 pm, cate <catebekens...(a)yahoo.com> wrote:

> Is there a way to use BEGIN some how?   Something like this.  I
> suspect you can't

Their scope will be this of the BEGIN block.
You could do:

use vars qw($var1 $var2);

....

BEGIN {
$var1 = 'sdfsdf';
$var2 = 'sdfsd';
}

Now, should I say that I am not convinced it buys you much...

Marc
From: ccc31807 on
On Jan 21, 9:12 am, cate <catebekens...(a)yahoo.com> wrote:
> I have a large list of my vars that I would like to get out of the
> way; place them at the end of the script - kinda a class thing.
>
> Is there a way to use BEGIN some how?   Something like this.  I
> suspect you can't, but I'm asking the pros.

Put them in a separate file, either an ordinary file or a PM.

If in an ordinary file, say vars.txt, like this:
$var1=GWashington
$var2=12
$var3=ashington, D.C.

You can do this in your script:
my %vars;
open VARS, '<', 'vars.txt';
while (<VARS>) {
chomp;
my ($key, $val) = split /=/;
$vars{$key} = $val;
}
close VARS;

If in a Perl module, say VARS.pm, use them like this in your script:

use VARS;
print $VARS::var1; # prints GWashington
$product = $VARS::var2 + 3; # $product is 15

In package VARS declare your variables with our.

CC.
From: Uri Guttman on
>>>>> "MG" == Marc Girod <marc.girod(a)gmail.com> writes:

MG> On Jan 21, 2:12�pm, cate <catebekens...(a)yahoo.com> wrote:
>> Is there a way to use BEGIN some how? � Something like this. �I
>> suspect you can't

MG> Their scope will be this of the BEGIN block.
MG> You could do:

MG> use vars qw($var1 $var2);

MG> ...

MG> BEGIN {
MG> $var1 = 'sdfsdf';
MG> $var2 = 'sdfsd';
MG> }

MG> Now, should I say that I am not convinced it buys you much...

it buys you the loss of lexicals. those are now package globals and can
be accessed from anywhere in the program.

uri

--
Uri Guttman ------ uri(a)stemsystems.com -------- http://www.sysarch.com --
----- Perl Code Review , Architecture, Development, Training, Support ------
--------- Gourmet Hot Cocoa Mix ---- http://bestfriendscocoa.com ---------
From: Uri Guttman on
>>>>> "c" == cate <catebekensail(a)yahoo.com> writes:

c> I have a large list of my vars that I would like to get out of the
c> way; place them at the end of the script - kinda a class thing.

c> Is there a way to use BEGIN some how? Something like this. I
c> suspect you can't, but I'm asking the pros.

c> use strict;
c> code using $var1 ...
c> code using $var1 ...
c> more code


c> BEGIN {
c> my $var1 = 'sfsdf';
c> my $var2 = 'sdfsdf';
c> }

the lexicals will be scoped only to the BEGIN block so they won't be
seen by the rest of the code. but needing to declare a mess of lexicals
tells me you have a weak design for this program. they are effectively
file globals and needing many globals is a poor design. try declaring
them in tighter scopes where they are just needed. use subs to organize
mainline code into smaller scopes where you can declare lexicals you
only need there. there should be almost no mainline code (code outside
subs) in any decent sized script. this will help with flow control,
understanding the code, maintaining it, etc. if you need a long flow,
still break it up into subs and call them from higher level subs. and do
that again if you have long higher level subs.

another solution is to use a single lexical hash with many/most of your
lexical data. it may need you to rewrite code that refers to them but
that be done quickly with a search/replace edit call. then you declare
the lexical hash at the top and initialize it in the BEGIN at the
bottom.

uri

--
Uri Guttman ------ uri(a)stemsystems.com -------- http://www.sysarch.com --
----- Perl Code Review , Architecture, Development, Training, Support ------
--------- Gourmet Hot Cocoa Mix ---- http://bestfriendscocoa.com ---------