hmag has asked for the wisdom of the Perl Monks concerning the following question:
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
I need to do the following things with the file:#!/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.
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 | |
by hmag (Acolyte) on Mar 24, 2006 at 08:28 UTC | |
|
Re: Problem with blank array (maybe) - Perl newbie
by davido (Cardinal) on Mar 24, 2006 at 06:28 UTC |