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

I've been reading about pipes all day, and I'm not sure if they are what I need or not, so I'm just going to ask this question and see what you bearers of wisdom would suggest.

I just want to setup a basic scenario of what I would like to be able to do (hopefully armed with knowledge of how to do this, I can just build on that for what I hope to do later).

There are two scripts: Main.pl and Child.pl

Main.pl has a variable called $contents. $contents gets information added to it as the script goes along, such as html that will later get outputted. So lets say in Main.pl we have:

$content .= qq~ <table> <tr><td> This is table cell one </td><td> This is table cell two </td></tr> </table> ~;

Now, without changing that script at all, I would like to change the contents of the $contents variable using Child.pl.
When $contents contains:

<table> <tr><td> This is table cell one </td><td>

But has not yet added the second table cell, I would like Child.pl to insert:

This is an inserted table cell </td><td>

Into the $contents variable in Main.pl

-----

In scenario #2, let's say we have some code being executed in Main.pl, such as:

open(FILE, "myfile.txt");
@file = <FILE>;
close(FILE);

open(FILE, ">myfile.txt")
print FILE @file;
print FILE "New line of date";
print FILE "\n";
close(FILE);

Now, while that code is executing, I would like Child.pl to add another print statement into the code while it executes, but without actually changing the Main.pl file. So what I'm looking to do is after Main.pl executes the command:

print FILE "New line of date";

But before it executes:

print FILE "\n";

I want it to do another print statement specified in Child.pl:

print FILE " - additional info from Child.pl";

I hope what I'm trying to do makes sense, and hopefully there is a fairly simple (or at least understandable) solution. Thanks for all your help and support.

- Dave

Replies are listed 'Best First'.
Re: Inserting one script's info into another script's output
by talexb (Chancellor) on Apr 28, 2002 at 03:35 UTC
    I don't really have a good handle on what you want to do or really the timing of the whole operation. Having child.pl do something while main.pl is executing seems a little odd.

    Why not call a routine (the code in child.pl) that returns "Something" or "", and act on that?

    --t. alex

    "Nyahhh (munch, munch) What's up, Doc?" --Bugs Bunny

Re: Inserting one script's info into another script's output
by Fletch (Bishop) on Apr 28, 2002 at 03:36 UTC

    Consider looking into one of the popular templating modules (Template, HTML::Mason, HTML::Template). Template Toolkit makes it possible to define blocks which can be included, and Mason lets you define components in a similar way.

      I think I need to explain what the concept behind this is.

      What I want is a main script that runs a program (here called Main.pl).

      However, users may want to add on functions to Main.pl and change things within the program, and then share this with other users. Instead of modifying the Main.pl code, I'd like a plug-in to be run (Child.pl here). That way someone could write a modification for Main.pl, and novice users could simply drop it into a plug-ins folder. Main.pl would then communicate with the scripts in the plug-ins folder. Does that make more sense?

        Define a plugin interface. It may be as simple as saying, "The plugin will have a function called process() that takes these values and returns this type of value." Ignore any plugins that do not behave appropriately. Find some way to identify installed plugins and to call them.

        This will allow you to use Perl modules, Template Toolkit templates, or HTML::Mason components as necessary. Any of these options is easier than working up your own IPC scheme.