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

i am having a file called as exec.pl . in that file i am having following lines.
use Cwd; .. .. #Now process Config file<br> open(CFG_READ, "< txt52_mm.org") || die "Couldn't open tmp con +fig command file for reading"; open(CFG, "> txt52_mm.cfg") || die "Couldn't open config file +for writing"; ... ... ... ... ... open(ASM_READ, "< t52${fl}a.org") || die "Couldn't open tmp"; open(ASM, "> t52${fl}a.asm") || die "Couldn't open file for wr +iting "; #######################################################
i want to modify this file using perl script as follows
i want to add $execdir before txt52_mm.cfg or t52${f1}a.asm.
these name can be different but will have .org extension or .asm extension.

use Cwd; $execdir = $ENV{EXECDIR}; #Now process Config file open(CFG_READ, "< txt52_mm.org") || die "Couldn't open tmp con +fig command file for reading"; open(CFG, "> $execdir/txt52_mm.cfg") || die "Couldn't open config file + for writing"; ... ... ... ... ... open(ASM_READ, "< t52${fl}a.org") || die "Couldn't open tmp"; open(ASM, "> $execdir/t52${fl}a.asm") || die "Couldn't open fi +le for writing ";

Edit by BazB. Added code tags.

Replies are listed 'Best First'.
Re: add a line or string in complex perl file
by rev_1318 (Chaplain) on Mar 16, 2005 at 11:30 UTC
    i want to add $execdir before txt52_mm.cfg...

    No you don't. Because tomorrow to want to change txt52 in catch42. Replace the entire directory/filename with a variable, so you gain more flexibility. Hard-coding names is seldom a good idea.

    Paul

    PS. Please use <code>-tags in the future...

Re: add a line or string in complex perl file
by perlsen (Chaplain) on Mar 16, 2005 at 10:11 UTC

    Hi, just try this

    input 1.pl; **************** open(CFG_READ, "< txt52_mm.org") || die "Couldn't open tmp config comm +and file for reading"; open(CFG, "> txt52_mm.cfg") || die "Couldn't open config file for writ +ing"; ... ... ... ... ... open(ASM_READ, "< t52${fl}a.org") || die "Couldn't open tmp"; open(ASM, "> t52${fl}a.asm") || die "Couldn't open file for writing "; ********************** ####### perl-coding start undef $/; open(READ, "< 1.pl") ; $str = <READ>; close(READ); $str =~s#"(> )(.+?)\.(cfg|asm)"#"$1\$execdir\/$2\.$3"#gsi; open(OUT, "> 2.pl") ; print OUT $str; ####### perl-coding end ********** output 2.pl ************** open(CFG_READ, "< txt52_mm.org") || die "Couldn't open tmp config comm +and file for reading"; open(CFG, "> $execdir/txt52_mm.cfg") || die "Couldn't open config file + for writing"; ... ... ... ... ... open(ASM_READ, "< t52${fl}a.org") || die "Couldn't open tmp"; open(ASM, "> $execdir/t52${fl}a.asm") || die "Couldn't open file for w +riting "; ******************