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

Hello All ! I have been at this a week now - all over the Internet and all over tutorials - and I still have not found a way - although I feel CERTAIN it is do-able. I want to write - "print" - a line of about 20 characters to another cgi script but not at the very top - the top of a script is reserved for the #! (Perl Path) and not at the bottom. It would be fine to do it one line after the top though. I can print my line to this file just fine, but it appends at the bottom - over and over again. Can I print to a specific location? Like replace a "%HOLD%" statement? I cannot figure out how to do this. I may thank God for having found this site - and would surely thank any of you kind folks who may be able to give me some advice as how to accomplish this. With very best regards to all who take their time to read this, Donnie
  • Comment on Doing a "Print" to a predetermined location in another file

Replies are listed 'Best First'.
Re: Doing a "Print" to a predetermined location in another file
by blakem (Monsignor) on Oct 12, 2002 at 09:41 UTC
    You can use Tie::File to insert lines into files... That might be what you're looking for.

    -Blake

      Hello Blake, Thank you for the info. I looked at the information behind the "Tie" link and found a wealth of stuff to persue. I appreciate your input. Best Regards, Donnie
Re: Doing a "Print" to a predetermined location in another file
by valdez (Monsignor) on Oct 12, 2002 at 10:31 UTC

    Hi Donnie,

    you need a template system :) Something like HTML::Template or Text::Template. This my solution for your problem, a bit overkill and maybe unorthodox. Suppose you have a file named script.in with the following content:

    #!/usr/bin/perl my $hold='<TMPL_VAR HOLD>'; my $leave = '<TMPL_VAR LEAVE>'; my $munge = '<TMPL_VAR MUNGE>';

    The program that will substitute HOLD, LEAVE and MUNGE is something like this:

    #!/usr/bin/perl use strict; use warnings; use HTML::Template; use vars qw( $in %args $template ); $in = shift @ARGV; %args = @ARGV; $template = HTML::Template->new( filename => $in ); $template->param(%args); print $template->output;

    Now you can use it this way:

    ./fill_blanks script.in HOLD this LEAVE that MUNGE these

    which prints the following:

    #!/usr/bin/perl $hold = "this"; $leave = "that"; $munge = "these";

    Ciao, Valerio

    Update: you are welcome Donnie :)

      Dear Valerio, WOW! Thanks so much for the code example! I will be with this most of today! It is awesome people take so much time here to help one another. May the sun always shine upon you! Best Regards and many thanks!
Re: Doing a "Print" to a predetermined location in another file
by robartes (Priest) on Oct 12, 2002 at 14:31 UTC
    Right, I'm gonna tell you something you already know, and in fact, already expect and possibly even dread: (all together now) there's more than one way to skin a camel (forgive me the gratuitous rephrasing :) ). In your case, here are some ways of doing this:

    * Way the first: read in your target file one line at a time, write it back out to a temporary file but add your new content when necessary:

    use strict; my $newcontent="My camel has been bitten by a rather large flea. Pleas +e call a vet."; my $input="camel.cgi"; my $tempfile="/tmp/camel.$$"; open TARGET, "<$input" or die "Blerch: $!\n"; open TEMP, ">/$tempfile" or die "Hcerlb: $!\n"; while (<INPUT>) { # check for your cue: could be a running counter to # keep track of lines read, could be a regexp, could # be just about anything else. Let's assume the check # sets $ship_ahoy when you need to add lines. if $ship_ahoy {print TEMP $newcontent} print TEMP; } close INPUT; close TMP; rename $tempfile $input or die "Hrlecb:$!\n"; print "Beware, this is untested code, and could definitely do with mor +e robust error checking!\n";
    * Way the second: play seek games. This is probably the easiest if you want to enter your text at a fixed position in the file. I'm pressed for time at the moment, so I'll leave the implementation of this as an exercise to the reader. The link above should get you almost the entire way there.

    CU
    Robartes-

      Dear Robartes, I want to personally thank you for your great and finely detailed reply to my question. This is just great! Yesterday I was blindly flailing around and this moring I have all this help! I will spend my day looking at all the information you and others provided and hopefully get this thing to work. You are most kind to provide the example and I am very grateful. With very best regards, Donnie
Re: Doing a "Print" to a predetermined location in another file
by thor (Priest) on Oct 12, 2002 at 13:54 UTC
    In the spirit of TMTOWTDI, you could write your stuff in to another file and require it. Though, more out of curiosity's sake, what do you want to put in to your CGI script? Having an external process modify a live CGI could be considered a breach of security, but I'm no expert in such things...

    thor

      Dear Thor, Thank you for your response. To answer your question: I have a script that can make certain modifications to a webpage, but the file name has to be "hard coded" into the script before it is run. What I am trying to do is make the script run on any webpage of the users choice, by using another script to select the choice to be made by the user, then plugging it into the main script before it is run. (Am I making sense here?) The user selects the page to be modified in the first script (Got that working just fine) and then plugs the resulting filename to the actual modifying script before it is run as: $filename = "webpagex.html". Thanks for your help. I have some things now to work with and will plod along to see where they lead. With best regards, Donnie
        Though it may require a shifting of gears mentally, I would suggest reading this in from an input box on the webpage, and then have your script called with that as a passed in parameter. Modifiying the code on the fly is dangerous, and creates a possible race condition if two users go for it at the same time. With a passed in parameter, each user gets their own "copy" of the program to do whatever they want to do with.

        thor