Skip site navigation (1)Skip section navigation (2)
Date:      Tue, 18 Apr 2000 04:04:32 +0100
From:      Paul Richards <paul@originative.co.uk>
To:        Steve Price <sprice@hiwaay.net>
Cc:        freebsd-chat@freebsd.org
Subject:   Re: Un*x scripting magic
Message-ID:  <38FBD0C0.F3385C66@originative.co.uk>
References:  <Pine.OSF.4.21.0004172140100.30660-100000@fly.HiWAAY.net>

next in thread | previous in thread | raw e-mail | index | archive | help
Steve Price wrote:
> 
> On Tue, 18 Apr 2000, Paul Richards wrote:
> 
> # > Then I have a multi-line (printable characters only) ASCII
> # > text file containing the message that I'd like to replace
> # > for %%MESSAGE%%. [1]  Anyone have a neat little trick in
> # > their bag-o-magic?  I know the answer should be really
> # > obvious but everything I've tried thus either hasn't worked
> # > or was too convoluted.
> #
> # What's wrong with the obvious?
> 
> It doesn't embed the text in the script.  So everytime I
> execute the script I have to re-read the file.

Well in that case you need a pre-processor since there's no other way
you can embed the information in the script. Write another perl script
that reads your runtime script and does the substitution.

In fact, it's not too hard assuming some constraints on the text (i.e.
it shouldn't include %%.*%% strings) and that the placeholders are on a
line of their own. It's not that much effort to make the whole thing
more generic but the following few lines will probably do the job you
want.

my $SCRIPT = "the script to be preprocessed";
open(SCRIPT, $SCRIPT) || die;
while (<SCRIPT>) {
	if (/^%%(.+)%%$/) {
		open(TXT, $1) || die;
		my $txt = "";
                while (<TXT>) {
			$txt .= $_;
                }
		print $txt;
		close(TXT) || die;
	} else {
		print $_;
	}
}
close (SCRIPT) || die;

> 
> # open(MESSAGE, $MESSAGE) || die;
> # while (<MESSAGE>) {
> #       $msg .= $_;
> # }
> #
> # You can probably optimise that a bit, read the whole file in one go for
> # starters.
> 
> As someone else pointed out something like this would work
> if I didn't want to embed the content within the code in its
> final form yet keep them separate while they are in CVS. :)
> 
> $msg = `cat foo`;

That's really lazy perl and should be frowned upon.

Paul.


To Unsubscribe: send mail to majordomo@FreeBSD.org
with "unsubscribe freebsd-chat" in the body of the message




Want to link to this message? Use this URL: <https://mail-archive.FreeBSD.org/cgi/mid.cgi?38FBD0C0.F3385C66>