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

Dear Fellow Monks,

I am doing a script for my temple. I hope you can help out to help me seek enlightenment in this script.
I was thinking of using a perl module to search a line from a test file and replace one of the value.

The sample text file at /home/shaolin/public_html/monkmeals.txt :

chinwoo=bun
wongfai=water
leeming=nothing
hailoo=bun

I wanted to make a function to change the meal of leeming. How can I use a perl function to loop through each line of the text file to look for leeming's meal using regex and change it to leeming=$value ?

Here is the pseudo code:
sub loadmeal { my($meal) = $_[0]; my($name,$value); open(meal,"/home/shaolin/public_html/monkmeals.txt"); LOOK FOR LEEMING=SOMETHING AND CHANGE IT TO LEEMING=$meal close(LANG); }

Please help us. Our temple's meal management system will depend on your guys. You will be helping put the right meals on the table of 100s of monks. On the behalf of the temple, I thank you in advance.

Edited by davido: Code tags added.

Replies are listed 'Best First'.
Re: Dear fellow monks, I need help in regex
by jdalbec (Deacon) on Aug 07, 2004 at 02:52 UTC
    Use <code></code> tags around Perl code to avoid problems with <, >, and square brackets.

    I recommend using the Tie::File CPAN module since otherwise you have to create a temporary file, copy the original file to it with your changes, and then rename the temporary file to replace the original file.

    use Tie::File; sub loadmeal { my($meal) = $_[0]; tie @meals, 'Tie::File', "/home/shaolin/public_html/monkmeals.txt" or +die ...; for (@meals) { s/leeming=.*/leeming=$meal/ } untie @meals;
    You may also want to consider using a database (such as DBD::SQLite) instead of a plain text file depending on how many monks you have and how often you will be updating their meal choices. Manipulating plain text files in this fashion doesn't scale well.
Re: Dear fellow monks, I need help in regex
by Velaki (Chaplain) on Aug 07, 2004 at 14:25 UTC

    A quick script you can run from the command line (on unix/linux) might look as follows:

    perl -pi.bak -e '$newMeal="food";s/leeming=.*/leeming=$newMeal/' /home +/shaolin/public_html/monkmeals.txt
    This will substitute leeming=anything to leeming=food, while creating a backup of the original file as well.

    Simply select something else for food, above, and change the file as much as you like.

    What might be better would be to use a template system with a database, but the above works for quick one-time replacements.

    -v
    "Perl. There is no substitute."
Re: Dear fellow monks, I need help in regex
by CountZero (Bishop) on Aug 07, 2004 at 08:06 UTC
    I would suggest that you seriously think of using a templating system. Then you can have a template for your meal and every monk which logs in, has the variables in the template filled in to his/her desires.

    The Template::Toolkit comes to mind. This templating system will also work "off-line", it doesn't have to run in a web-environment.

    CountZero

    "If you have four groups working on a compiler, you'll get a 4-pass compiler." - Conway's Law