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

Dear Monks,

I want to change the value of the global variable of the perl module in the .pm file itself(permanently). How can I change the value using program (without using file operations).Here is the example code.

package pkgname; our $var1 = 'value1';

After changing the value1 to value2. It should look like this

package pkgname; our $var1 = 'value2';

I tried using methods it changes in the runtime. After execution I opened the file and saw $var1 doesn't change in the pm file. Is there any other way to do this without using file operations?

Expecting your blessing,

Arun Kumar C.N

Replies are listed 'Best First'.
Re: Changing a variable in perl module
by JavaFan (Canon) on Mar 04, 2009 at 12:51 UTC
    I suggest putting a sticky note with 'value2' written on it on the screen. Just about there were you see 'value1'. Then you have changed the content of the file, without "file operations".

    Another way to use a disk instead of a file operation. Find the location of 'value1' on disk, and use 'dd' to write 'value2' on it. But that usually requires root priviledges.

    Not being able to become root, you still have an option left, assuming physical access to the disk(s). It involves magnets and a very steady hand.

Re: Changing a variable in perl module
by ELISHEVA (Prior) on Mar 04, 2009 at 12:28 UTC
    Welcome to Perl Monks!

    It seems like you are trying to do a search and replace in the file? Was that the goal?

    I think it would help a lot if you posted the code you used when you "tried using methods it changes in the runtime.". Without knowing exactly what you did, it is next to impossible for us to suggest what you might be able to do better.

    Best, beth

    P.S. There are a lot of great tips on how to write a successful question in this article: How (Not) To Ask A Question.

Re: Changing a variable in perl module
by cdarke (Prior) on Mar 04, 2009 at 12:34 UTC
    Let's get this clear, you want to change a source file without using a file operation? What have you got against file operations?

    There must be a better way of doing this. Try designing the task (whatever it is ) another way.
Re: Changing a variable in perl module
by targetsmart (Curate) on Mar 04, 2009 at 14:24 UTC
    This is not a direct answer to your question
    but you can store and use variables and its values from .ini files
    You have config::inifiles perl module to read and write into it effectively.
    http://search.cpan.org/~shlomif/Config-IniFiles-2.47/lib/Config/IniFiles.pm
    You can look in to that.

    Vivek
    -- In accordance with the prarabdha of each, the One whose function it is to ordain makes each to act. What will not happen will never happen, whatever effort one may put forth. And what will happen will not fail to happen, however much one may seek to prevent it. This is certain. The part of wisdom therefore is to stay quiet.
Re: Changing a variable in perl module
by Anonymous Monk on Mar 04, 2009 at 12:27 UTC
    You can't.