Beefy Boxes and Bandwidth Generously Provided by pair Networks
Syntactic Confectionery Delight
 
PerlMonks  

Parsing variables from one file to another.

by fatmcgav (Novice)
on Jan 21, 2009 at 13:45 UTC ( [id://737818]=perlquestion: print w/replies, xml ) Need Help??

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

Hi,

I'm trying to get Perl to write the contents of a template file with $variables to another file, with those $'s expanded out...

Currently, my function looks like this:
sub make_apachectl { my $apachectl = shift; my $dname = shift; my $apachectl_template = "/home/www/bin/files/bin/apachectl"; open( TEMPLATE, "<", $apachectl_template ) || die "Failed to open +apachectl template: $!\n"; open( TARGET, ">", $apachectl ) || die "Failed to create apachectl +: $!\n"; while( <TEMPLATE> ) { print TARGET ; } }
The function call is:
make_apachectl ( "/home/www/$dname/bin/apachectl", $dname );

The template file has the following variables set:
# Defining PID file allows multiple instances of Apache from the same +binary PIDFILE=/home/www/$dname/pid/$dname.pid # the path to your httpd binary, including options if necessary HTTPD='/usr/local/apache/bin/httpd -f /home/www/$dname/conf/httpd.conf +'

How do I get perl to expand out the $dname variables when printing from TEMPLATE to TARGET?

Regards
Gavin

Replies are listed 'Best First'.
Re: Parsing variables from one file to another.
by Corion (Patriarch) on Jan 21, 2009 at 13:51 UTC

    This sounds to me as if you really want one of the templating solutions for Perl, like Template Toolkit.

    If you're bent on doing this yourself (this is one of the rites of passage of a Perl programmer), then look at perlre. Put all your template values into a hash and use a regular expression with the /e modifier:

    my %template = ( apachectl => 'my_apachectl', dname => 'my_foo', ); open my $fh, '<', $apachectl_template or die "Failed to open '$apachectl_template': $!"; my $template = do { local $/; <$fh> }; $template =~ s/\$(\w+)/$template{$1} || $1/ge;

    If you're hell-bent on substituting lexical variables, which is a fairly bad idea, you'll have to resort to eval, but I won't even show you how to exactly do this, as using a hash is much, much safer.

Re: Parsing variables from one file to another.
by jeffa (Bishop) on Jan 21, 2009 at 13:50 UTC
Re: Parsing variables from one file to another.
by hbm (Hermit) on Jan 21, 2009 at 14:02 UTC
    You can substitute in your while-print loop, like this:
    while( <TEMPLATE> ) { s/\$dname/$dname/g; print TARGET ; }
      the above worked a treat for what i wanted to do...

      cheers..

      Gav
Re: Parsing variables from one file to another.
by fatmcgav (Novice) on Jan 26, 2009 at 09:24 UTC
    Ok, just to bring this one back from the dead...

    I went down the route of using Template, and it's working fantastically.

    However i'm having issues with an IF Include code block...
    I've got the following code:
    [% IF ep == cms; INCLUDE cmsproxy; BLOCK cmsproxy; # Proxy onto application server ProxyPass /CMSBackOffice http://[% vmname %]:80 +80/CMSBackOffice ProxyPassReverse /CMSBackOffice http://[% vmname %]:80 +80/CMSBackOffice ProxyPass /CMSResources[% resources %] http://[% v +mname %]:8080/CMSResources[% resources %] ProxyPassReverse /CMSResources[% resources %] http://[% v +mname %]:8080/CMSResources[% resources %] END; ELSE; # Proxy onto application server ProxyPass /CMSBureauWebService/CMSBureauWS http:/ +/[% vmname %]:8080/CMSBureauWebService/CMSBureauWS ProxyPassReverse /CMSBureauWebService/CMSBureauWS http:/ +/[% vmname %]:8080/CMSBureauWebService/CMSBureauWS END; %]
    When i try to run the template through, it throws the following error:
    file error - parse error - /home/www/bin/files/conf/sites/ssl.conf.template line 236-241: unexpected token (/CMSBackOffice)

    Any ideas what this means?

    Regards
    Gavin

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://737818]
Approved by ww
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others musing on the Monastery: (4)
As of 2024-04-23 23:45 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found