From: J�rgen Exner on
cate <catebekensail(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.

As a general rule you should try to avoid global variables, they are
rarely necessary. And a large number of global variables usually
indicates poor design of the algorithm or the data structure.

Instead of trying to hide the variables I would rather investigate how
to improve my code or data structure and eliminate them.

jue
From: Brad Baxter on
On 1/21/2010 9:12 AM, cate 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.
>
> use strict;
> code using $var1 ...
> code using $var1 ...
> more code
>
>
> BEGIN {
> my $var1 = 'sfsdf';
> my $var2 = 'sdfsdf';
> }
>
> thank you

You have gotten advice against wanting to do this,
and I concur that your motives are perhaps dubious.

But to answer the specific question, you just need
to declare the lexicals at the top and assign them
in the BEGIN block at the bottom *without* my.

1 #!/usr/local/bin/perl
2
3 use warnings;
4 use strict;
5
6 my $var1;
7 my $var2;
8
9 print "$var1, $var2\n";
10
11 exit;
12
13 BEGIN {
14 $var1 = 'Hello';
15 $var2 = 'World';
16 }
17
18 __END__
19 Hello, World

--
Brad
From: Uri Guttman on
>>>>> "BB" == Brad Baxter <bmb(a)mail.libs.uga.edu> writes:

BB> On 1/21/2010 9:12 AM, cate 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.


BB> But to answer the specific question, you just need
BB> to declare the lexicals at the top and assign them
BB> in the BEGIN block at the bottom *without* my.

then you don't solve his problem of a large list of vars at the
beginning of the script!

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: RedGrittyBrick on

cate 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.
>
> use strict;
> code using $var1 ...
> code using $var1 ...
> more code
>
>
> BEGIN {
> my $var1 = 'sfsdf';
> my $var2 = 'sdfsdf';
> }
>

As Uri and Jürgen suggested, I'd try hard not to have a large list of
vars anywhere. I'd declare variables in the smallest useful scope, near
where they are used.

If forced to have a large list, since I prefer not the have BEGIN blocks
at the end - I'd try something like:

-----------------------------8<-------------------------------
#!perl
use strict;
use warnings;

{ # to restrict scope of %vars

my %vars = getVars();
# ...
print $vars{'var1'};
# ...
}

sub otherSub {
# no use of %vars possible here as it's lexical & hence out of scope?
}

sub getVars {
return (
var1 => 'sfsdf',
var2 => 'sdfsdf'
)
}
-----------------------------8<-------------------------------

Which I think avoids creating/using global variables

--
RGB
From: Ferry Bolhar on
"cate" <catebekensail(a)yahoo.com> schrieb im Newsbeitrag
news:1373c121-018f-4f0c-9fb6-56e7fed5a4af(a)p24g2000yqm.googlegroups.com...
>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.

You must _declare_ them always before first using them. Once declared,
you can use a BEGIN block to assign initial values before actual use.

use strict;
my ($var1, $var2);

....<code using $var1>...
....<code using $var2>...

BEGIN {
$var1 = 'Initvalue for var1';
$var2 = 'Initivalue for var2';
}
__END__

However, if the number of variables used this way is large, I'd consider
to use a hash:

my %hash; # Just one declaration!

....<code using $hash{var1}>...
....<code using $hash{var2}>...

BEGIN {
$hash{var1} = 'Initvalue for var1';
$hash{var2} = 'Initvalue for var2';
}

A hash also allows you to assign the particular values in more efficient
ways.

Regards, Ferry

--
Ing. Ferry Bolhar
Magistrat der Stadt Wien - MA 14
A-1010 Wien
E-Mail: ferdinand.bolhar-nordenkampf(a)wien.gv.at