Re-inventing the wheel, same hash, three scripts

J

Justin C

I've been re-inventing the wheel, and it's costing me time. I have a
hash that is used in three scripts, and it's in all three scripts. Each
time there is a change to it I have to update all three scripts. I know
I'm wasting my time.

I'd like to store this hash in an external file and reference the
contents from each script. The scripts are only going to need to look up
one or two elements (out of about 100) each time they are run.

It has been suggested to me that I use one of the Config:: modules, I'd
rather, if possible, do this without modules (these may end up running
somewhere I can't request modules be installed).

I'm happy to read the documentation, I just don't know which
documentation I should read. Opening a file, reading all the contents
into a hash seems, though easy, a waste of time.

Thank you for your suggestions for help with this matter.

Justin.
 
X

xhoster

I've been re-inventing the wheel, and it's costing me time. I have a
hash that is used in three scripts, and it's in all three scripts. Each
time there is a change to it I have to update all three scripts. I know
I'm wasting my time.

I'd like to store this hash in an external file and reference the
contents from each script. The scripts are only going to need to look up
one or two elements (out of about 100) each time they are run.

If the scripts are only going to look up 1 or 2, then why are there 100
in the first place?
It has been suggested to me that I use one of the Config:: modules, I'd
rather, if possible, do this without modules (these may end up running
somewhere I can't request modules be installed).

A module is nothing more than an external file containing a certain kind
of valid perl code. If you can't install modules, you can't install an
external file to contain the hash's info, either.
I'm happy to read the documentation, I just don't know which
documentation I should read. Opening a file, reading all the contents
into a hash seems, though easy, a waste of time.

How precious is your computer's time relative to your own? If it is very
precious, perhaps you should choose a different language to code in in the
first place.


Personally, I'd just make the config file be a custom-made module.

=== my_config.pm ===
package my_config;
%my_config::config = (
foo => 'bar',
baz => 'boo',
};

1;

=== script.pl ===
use my_config;
warn $my_config{foo};


Xho
 
A

Andreas Pürzer

Personally, I'd just make the config file be a custom-made module.

=== my_config.pm ===
package my_config;
%my_config::config = (
foo => 'bar',
baz => 'boo',
};

Unmatched right curly bracket at my_config.pm...
1;

=== script.pl ===
use my_config;
warn $my_config{foo};

Warning: something's wrong at script.pl...
warn $my_config::config{foo};

Sorry for the nitpicking, no offense intended :->

Andreas Pürzer
 
J

John W. Krahn

Justin said:
I've been re-inventing the wheel, and it's costing me time. I have a
hash that is used in three scripts, and it's in all three scripts. Each
time there is a change to it I have to update all three scripts. I know
I'm wasting my time.

I'd like to store this hash in an external file and reference the
contents from each script. The scripts are only going to need to look up
one or two elements (out of about 100) each time they are run.

It has been suggested to me that I use one of the Config:: modules, I'd
rather, if possible, do this without modules (these may end up running
somewhere I can't request modules be installed).

I'm happy to read the documentation, I just don't know which
documentation I should read. Opening a file, reading all the contents
into a hash seems, though easy, a waste of time.

perldoc -f dbmopen
perldoc -f dbmclose



John
 
J

Justin C

If the scripts are only going to look up 1 or 2, then why are there 100
in the first place?

What I meant was that though they'll only look up 1 or 2 values each
time, they're likely to be different keys/values each time.

A module is nothing more than an external file containing a certain kind
of valid perl code. If you can't install modules, you can't install an
external file to contain the hash's info, either.

I understand. What I mean, then, is that I'd rather manage this task
with a standard Perl install.

How precious is your computer's time relative to your own? If it is very
precious, perhaps you should choose a different language to code in in the
first place.

Yes, but I'm not starting from "in the first place" am I? I already have
these scripts written, and my time is valuable enough that I don't want
to write them again in something else.

Thank you for your comments.

Justin.
 
J

Justin C

Here is one way:

main.pl:

require 'hash.pl';
if( $hash{$key} ) {
...


hash.pl:

%hash = ( ... );

OK, that looks fairly straight forward. I'll have a read up on 'require'
and see where that leads me. I'm not sure, at this stage how we get from
"require 'fname' ;" to "$hash{$key}" ... does it just 'import' those?
I'd have to be careful not to re-use those variable names.

You can improve this scheme by using 'package', 'my', or 'our' in
various combinations (left as an exercise or another poster).

Thanks, that's more to read up on, I know 'my' (learnt that very early!)
not come across 'package', or 'our' before, so that's going to confuse
the grey matter... still, I need something to keep me occupied at work.

Thanks again for the help.

Justin.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,057
Latest member
KetoBeezACVGummies

Latest Threads

Top