kranthi has asked for the wisdom of the Perl Monks concerning the following question:

Hi Perl monks,
I have one pm file in which only hash structure with values is defined.
Is there any way to change specific value of the hash permenantly in the pm file through our main program.
Suppose my Hash structure contains
#stud_Info.pm %studentInfo = ( stud_1 => { Name => 'ABC', Address => 'Street I' }, stud_2 => {Name => 'DEF' Address => 'Stret II}, );
I'll use the above pm file in my main file to fetch the data of the students.And now i want to change the Name of stud_1 to 'abcd' permenantly in the stud_Info.pm file.
Request you to help me with your suggestions in this regard.

Replies are listed 'Best First'.
Re: Changing Hash value in .pm file
by Tanktalus (Canon) on Jun 19, 2008 at 17:37 UTC

    Yes. There is a way. It's just text, there's always a way.

    However, by the time you get that working flawlessly, a better, cheaper solution would have been in production already.

    Have your stud_Info.pm load the hash from a Storable or some other serialisation method. Then it becomes trivial to update. And you can basically hide the code in this module and still provide the same interface to all callers.

Re: Changing Hash value in .pm file
by dragonchild (Archbishop) on Jun 19, 2008 at 18:09 UTC
    Take a look at DBM::Deep, Tie::Persistent, and DBD::SQLite. The method you're using is less than optimal.

    My criteria for good software:
    1. Does it work?
    2. Can someone else come in, make a change, and be reasonably certain no bugs were introduced?
Re: Changing Hash value in .pm file
by Fletch (Bishop) on Jun 19, 2008 at 18:09 UTC

    Sounds like perhaps you want something along the lines of DBM::Deep or the like. That would be cleaner since your data would stored as data and not code.

    If you're really masochistic you could just require / do the file then dump it back out again with a suitable Data::Dumper invocation, but in all likelihood you don't want to go down that route.

    The cake is a lie.
    The cake is a lie.
    The cake is a lie.

Re: Changing Hash value in .pm file
by kranthi (Sexton) on Jun 20, 2008 at 03:39 UTC
    Hi Monks,
    Thanks for the response. I request you to pls let me know the procedure for changing the same in the pm file.
      The aforementioned Data::Dumper is the closest you'll get. Anything better would be impossible without constraints. But since you haven't said how Data::Dumper is unacceptable, it's hard to accommodate you.
        Hi, I am sorry to say that i am new to perl programming. I have less than 1 month of experience. I am unable to access the methods of Data::Dumper (rather confused which method to use). Hence please guide me.