Re-inventing the wheel, same hash, three scripts

Discussion in 'Perl Misc' started by Justin C, Apr 24, 2007.

  1. Justin C

    Justin C Guest

    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.

    --
    Justin C, by the sea.
     
    Justin C, Apr 24, 2007
    #1
    1. Advertising

  2. Justin C

    Guest

    wrote:
    > 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

    --
    -------------------- http://NewsReader.Com/ --------------------
    Usenet Newsgroup Service $9.95/Month 30GB
     
    , Apr 24, 2007
    #2
    1. Advertising

  3. schrieb:
    > 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};

    > Xho
    >


    Sorry for the nitpicking, no offense intended :->

    Andreas Pürzer

    --
    Have Fun,
    and if you can't have fun,
    have someone else's fun.
    The Beautiful South
     
    Andreas Pürzer, Apr 24, 2007
    #3
  4. Justin C wrote:
    > 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
    --
    Perl isn't a toolbox, but a small machine shop where you can special-order
    certain sorts of tools at low cost and in short order. -- Larry Wall
     
    John W. Krahn, Apr 24, 2007
    #4
  5. Justin C

    Justin C Guest

    In article <20070424110709.877$>, wrote:
    > wrote:
    >> 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?


    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.


    >> 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 understand. What I mean, then, is that I'd rather manage this task
    with a standard Perl install.


    >> 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.


    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.

    --
    Justin C, by the sea.
     
    Justin C, Apr 24, 2007
    #5
  6. Justin C

    Justin C Guest

    In article <240420071017079484%>, Jim Gibson wrote:
    > In article <>, Justin
    > C <> wrote:
    >
    >> 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.

    >
    > 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.

    --
    Justin C, by the sea.
     
    Justin C, Apr 24, 2007
    #6
  7. Justin C

    Justin C Guest

    In article <qqvXh.7$_G.4@edtnps89>, John W. Krahn wrote:
    > Justin C wrote:
    >> 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


    Thank you, I shall read those.

    Justin.

    --
    Justin C, by the sea.
     
    Justin C, Apr 24, 2007
    #7
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Ronald Fischer
    Replies:
    1
    Views:
    15,364
    Jacob
    Jul 22, 2003
  2. Devon Null

    Am I re-inventing the wheel?

    Devon Null, Jun 6, 2007, in forum: C++
    Replies:
    4
    Views:
    315
    Devon Null
    Jun 7, 2007
  3. rp
    Replies:
    1
    Views:
    597
    red floyd
    Nov 10, 2011
  4. Srijayanth Sridhar
    Replies:
    19
    Views:
    677
    David A. Black
    Jul 2, 2008
  5. Wes Groleau
    Replies:
    6
    Views:
    145
    Tad McClellan
    Jan 16, 2005
Loading...

Share This Page