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

Dear Gurus I need help in generating the variable substitutions ...by a perl script.. I have a main file with variable tokens like this:
name: File1 =========== Destination/Company=@deploy.company@ Destination/Environment=@deploy.env@ Destination/Location=@deploy.location@ Destination/Domain=@deploy.location@ MIG_GatewayAddresses=@deploy.gwaddress@ MIG_URL=@deploy.mig_url@ Database/URL=@deploy.db_url@ SBU/Domain=@deploy.company@_@deploy.location@
I have another file that provides the variable values...
name: File2 ============ deploy.company=ABC deploy.location=BLore deploy.env=DEV deploy.gwaddress=fnet-ayz3z1:18997;fnetzyx3z1:18997 deploy.mig_url=tibjmsnaming\://ldgcomp.abz.net\:25011,tibjmsnaming\:// +ldgcomp.abz.net\:25012 deploy.db_url=jdbc:antsoftware:oracle://ldgcomp.abz.net:1521;SID=devd; +enablecanceltimeout=true
I would like these values to be replaced with in the main file. I have been trying by best but couldnt reach a solution yet. Now I've almost there to sumit my assignment...can you please help? Many thanks in advance...

Replies are listed 'Best First'.
Re: config tokens substitutions
by Corion (Patriarch) on Jul 12, 2009 at 19:51 UTC

    We don't do homework assignments here.

    Having said that, maybe you want to show us the code you already have, and explain to us what it should do, what its input data is, what it should ouput and how it fails for you? Then we can help you with your specific problems.

    Alternatively, I'd just use Template Toolkit to fill out templates, but you'd have to rewrite your templates to fit in the TT syntax.

Re: config tokens substitutions
by psini (Deacon) on Jul 12, 2009 at 19:52 UTC

    This is only a trace you can follow:

    • read the second file, parsing each line (split) and populating a hash with var=>value
    • open a temp file in write mode (File::Temp)
    • read the first file and, line by line:
    • extract the left term with a regex
    • use it as a key to the hash
    • retrieve the value
    • substitute it in the line with another regex
    • write the new line to a temp file
    • on end of file, close the files and copy the temp file back in place of the first file (File::Copy)

    Rule One: "Do not act incautiously when confronting a little bald wrinkly smiling man."

Re: config tokens substitutions
by mzedeler (Pilgrim) on Jul 12, 2009 at 20:26 UTC

    Post the script you have written so far and explain exactly where you are stuck. Then you'll probably get much more concrete help.

Re: config tokens substitutions
by jwkrahn (Abbot) on Jul 13, 2009 at 00:48 UTC

    This is UNTESTED but it may help get you started:

    #!/usr/bin/perl use warnings; use strict; my $file2 = 'File2'; open my $IN, '<', $file2 or die "Cannot open '$file2' $!"; my %data; while ( <$IN> ) { next unless /^([^=]+)=(.+)/; $data{ $1 } = $2; } close $IN; ( $^I, @ARGV ) = ( '.bak', 'File1' ); while ( <> ) { s/(\@([^\@]+)\@)$/ exists $data{ $2 } ? $data{ $2 } : $1 /e; print; }