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

Hi again, Thanks to all those who have assisted me this far, but I seem to hit a brick wall with each step of my project.

I have the following file that I need to pull apart & 'play' with (it's only an example so I can follow the format for the real project):

Contents of sample.pm

#!/bin/sh . /etc/rc.common # __START_CONFIG__ binary_app=/usr/bin/true config_file=/etc/inetd.conf MOD_NAME="ABC" # __END_CONFIG__ # this file does nothing.
I need to do the following things with the file:

1 - Grab the file's contents up to & including # __START_CONFIG__ - write it to a temp file - 123456.beg

2 - Grab the middle section of the file up to & including MOD_NAME="ABC" & write it into an array @allow_edit

3 - Take the rest of the contents of the file from & including # __END_CONFIG & write it to a temp file 123456.end

4 - Split the strings in allow_edit at "=" and populate a template for editing the on an html page like so:

<FORM ACTION="/EDITOR.pm" METHOD="POST"> <INPUT TYPE=HIDDEN NAME="Module" VALUE="EDITOR"> <INPUT TYPE=HIDDEN NAME="Method" VALUE="set"> <INPUT TYPE=HIDDEN NAME="temp_name" VALUE="123456"> <INPUT TYPE=HIDDEN NAME="actual_name" VALUE="sample.pm"> <table> <tr> <td>binary_app:</td><td> <input type=text name ="binary_ap +p" value="/usr/bin/true"></td> </tr><tr> <td>config_file:</td><td> <input type=text name ="config_f +ile" value="/etc/inetd.conf"></td> </tr><tr> <td> MOD_NAME:</td><td> <input type=text name ="MOD_NAME" +value="ABC"></td> </tr> <table> <INPUT TYPE="SUBMIT" NAME="SUBMIT" VALUE="Save Changes"> </FORM>

5 - Take the POST data, convert to an array puting back the "=",read in "123456.beg", write to "sample.pm" append array to "sample.pm",read in "123456.end", append to "sample.pm" Currently, this is what I have below, which partially works, but I am getting blank info out of allow_edit & I haven't been able to figure out the split command properly as yet.

Hopefully someone might be able to help out - many thanks, Terry

Script to pull sample.pm apart

#!/usr/bin/perl # Open File - Read File Contents Then Modify & save contents # Specify name of file $data_file="sample.pm"; # Name of temp file 1 $prefile1="/tmp/123456.beg"; # Name of temp file 2 $prefile2="/tmp/123456.end"; $action = 1; # Open File abd read it all in to rawdata open (file_beg, ">$prefile1") || die ("Could not open file. <br> $!"); +# Open The File open (file_end, ">$prefile2") || die ("Could not open file. <br> $!"); +# Open The File open (sample, "$data_file") || die ("Could not open file. <br> $!");# +Open The File flock(sample, 2) or die "cannot lock file exclusively: $!";# Lock The +File @rawdata = <sample>;# Put data from file into array called sample # write data from sample.pm into beg_non_edit foreach $value (@rawdata) { print ("$value\n"); if($value =~ /# __END_CONFIG__/i) { $action=3; } if ( $action == 1 ) { # write to file_beg print file_beg "$value"; # as long as action is 1 it should wr +ite to this file. } if ($action == 3) { # write to file_end print file_end "$value"; # as long as action is 3 it will writ +e to this file } if ($action == 2) { ; # copy string to new array } if($value =~ /# __START_CONFIG__/i) { $action=2; } push @allow_edit, $_; } foreach $value (@allow_edit) { print ("$value\n"); } close (sample); close (file_beg); close (file_end);

Replies are listed 'Best First'.
Re: Problem with blank array (maybe) - Perl newbie
by duff (Parson) on Mar 24, 2006 at 06:34 UTC

    Nice state machine. Your immediate problem is that you're pushing $_ onto @allow_edit when you really want to be pushing $value.

    split works just like the docs say it does :-) Here's an example for you: my ($name,$value) = split /=/, $string;

    Rather than slurping the entire file into an array and then copying part of that array to another array, I'd just use a while loop to read a line at a time and only create an array for the stuff you want in @allow_edit. There's nothing wrong with the way you did it, that's just what I would do.

      Hi again, Many many thanks for your help - it's all about just knowing the syntax of stuff really - I will keep moving towards my target - that has gotten me a lot further.
Re: Problem with blank array (maybe) - Perl newbie
by davido (Cardinal) on Mar 24, 2006 at 06:28 UTC

    Here is your most immediate problem: push @allow_edit, $_; should be located within the action 2 if block. And also, you shouldn't be pushing $_. Instead, you should be pushing $value.

    So, the if block in question should look like this:

    if( $action == 2 ) { push @allow_edit, $value; }

    ...and push @allow_edit, $_; should be eliminated from your code altogether.

    That ought to get you going. ...not necessarily how I would do it, but at least it ought to solve your immediate issue.


    Dave