From: leiji22 on
I'd like to define a large hash at the end of the script file. The hash
contains some parameters.

But I need to use this hash variable at the beginning of the script
file.

The hash is very long, so it's impossible for me to put it at the
beginning of the script.

Do I need to declare it at the beginning of the file? If not, does it
matter where in the file I put the variable definition?

Thanks for any help!

From: Scott Bryce on
leiji22(a)yahoo.com wrote:
> I'd like to define a large hash at the end of the script file. The hash
> contains some parameters.
>
> But I need to use this hash variable at the beginning of the script
> file.
>
> The hash is very long, so it's impossible for me to put it at the
> beginning of the script.
>
> Do I need to declare it at the beginning of the file? If not, does it
> matter where in the file I put the variable definition?
>
> Thanks for any help!
>

I'm not sure exactly what you are trying to accomplish. It looks to me
like you want to define a large data structure near the beginning of
your algorithm, but want to place the code that defines the data
structure near the end of your script for esthetic reasons.


my $large_data_structure = get_large_data_structure();

# Do something with the large data structure here
# $large_data_structure contains a reference to a hash.

sub get_large_data_structure {

my %large_data_structure = (

# Large data structure defined here.

}

return \%large_data_structure;
}
From: Scott Bryce on
Scott Bryce wrote:

> sub get_large_data_structure {
>
> my %large_data_structure = (
>
> # Large data structure defined here.
>
> }
-------^

That should be a ).

>
> return \%large_data_structure;
> }
From: John W. Krahn on
leiji22(a)yahoo.com wrote:
> I'd like to define a large hash at the end of the script file. The hash
> contains some parameters.
>
> But I need to use this hash variable at the beginning of the script
> file.
>
> The hash is very long, so it's impossible for me to put it at the
> beginning of the script.
>
> Do I need to declare it at the beginning of the file? If not, does it
> matter where in the file I put the variable definition?

You could use a subroutine to define it:

#!/usr/bin/perl
use warnings;
use strict;

my %large_hash = define_large_hash();

# many, many, many, many lines of code

sub define_large_hash {
key1 => value1,
key2 => value2,
....
key9998 => value9998,
key9999 => value9999,
}

__END__



John
--
use Perl;
program
fulfillment
From: Anno Siegel on
John W. Krahn <krahnj(a)telus.net> wrote in comp.lang.perl.misc:
> leiji22(a)yahoo.com wrote:
> > I'd like to define a large hash at the end of the script file. The hash
> > contains some parameters.
> >
> > But I need to use this hash variable at the beginning of the script
> > file.
> >
> > The hash is very long, so it's impossible for me to put it at the
> > beginning of the script.
> >
> > Do I need to declare it at the beginning of the file? If not, does it
> > matter where in the file I put the variable definition?
>
> You could use a subroutine to define it:
>
> #!/usr/bin/perl
> use warnings;
> use strict;
>
> my %large_hash = define_large_hash();
>
> # many, many, many, many lines of code
>
> sub define_large_hash {
> key1 => value1,
> key2 => value2,
> ...
> key9998 => value9998,
> key9999 => value9999,
> }
>
> __END__

Let me show up a few alternatives.

Almost equivalently, the constant pragma can be used:

my %large_hash = LARGE_HASH;

# many, many, many, many lines of code

use constant LARGE_HASH => (
key1 => value1,
key2 => value2,
...
key9998 => value9998,
key9999 => value9999,
);

Using Eric Roode's Readonly (from CPAN) would currently require an
explicit BEGIN block to make sure %large_hash is set early:

my %large_hash;

# many, many, many, many lines of code

use Readonly;
BEGIN {
Readonly %large_hash => (
key1 => value1,
key2 => value2,
...
key9998 => value9998,
key9999 => value9999,
);
}


As of the next version (if I understand correctly) one will be able
to write

my %large_hash;

# many, many, many, many lines of code

use Readonly \ %large_hash => (
key1 => value1,
key2 => value2,
...
key9998 => value9998,
key9999 => value9999,
);

Either way, I notice that with Readonly the declaration "my %large_hash"
would *have* to be commented to the effect that it is set at compile time
further down in the code. That is the cost of saving the intermediate
routine define_large_hash (or LARGE_HASH) of the other solutions, which
make the assignment explicit.

Anno
--
If you want to post a followup via groups.google.com, don't use
the broken "Reply" link at the bottom of the article. Click on
"show options" at the top of the article, then click on the
"Reply" at the bottom of the article headers.