CGI/Perl Guide | Learning Center | Forums | Advertise | Login
Site Search: in

  Main Index MAIN
INDEX
Search Posts SEARCH
POSTS
Who's Online WHO'S
ONLINE
Log in LOG
IN

Home: Perl Programming Help: Intermediate:
Convert dev to uat

 



w020637
Novice

May 28, 2009, 2:09 PM

Post #1 of 11 (1255 views)
Convert dev to uat Can't Post

I need to create scripts for different environments using devlopment script. Here is a sample dev script that needs to be converted to uat env

Dev script:dev.jil

/* ----------------- dev_autosys.job1 ----------------- */

insert_job: dev_autosys.job1 job_type: c
box_name: dev_autosys.job1_box
command: /test/dev/fin/guest/dir/shl/GKF_setup.sh d; /test/dev/fin/guest/dir/shl/run_data.sh"
machine: tt_sas
#owner: guestmgr
permission: gx
condition: success(dev_guest_cmd.lmn) and success(dev_guest_cmd.xyz)
description: "Freeze"
std_out_file: /data/dev/guest/dir/logs/$AUTO_JOB_NAME`date +%m%d`.out
std_err_file: /data/dev/guest/dir/logs/$AUTO_JOB_NAME`date +%m%d`.err
alarm_if_fail: 1
profile: /fmac/dev/guest/dir/cfg/profile_for_execution

Required sample UAT script:

/* ----------------- uat_autosys.job1 ----------------- */

insert_job: uat_autosys.job1 job_type: c
box_name: uat_autosys.job1_box
command: qrsh -N "GKF" ". /test/uat/fin/guest/dir/shl/GKF_setup.sh u; /test/uat/fin/guest/dir/shl/run_data.sh"
machine: tt_sas
#owner: guestuatmgr
permission: gx
condition: success(uat_guest_cmd.lmn) and success(uat_guest_cmd.xyz)
description: "Freeze"
std_out_file: /data/uat/guest/dir/logs/$AUTO_JOB_NAME`date +%m%d`.out
std_err_file: /data/uat/guest/dir/logs/$AUTO_JOB_NAME`date +%m%d`.err
alarm_if_fail: 1
profile: /fmac/uat/guest/dir/cfg/profile_for_execution

Here is what I am trying:

#!/bin/perl

use strict;
#use warnings;
use File::Find;

my $startdir = '/test/perl';
my $find = 'dev';
my $replace = 'uat';
my $doctype = 'dev.jil';

print qq~Finding "$find" and replacing it with "$replace"\n~;

find(
sub{
return unless (/\.$doctype$/i);
local @ARGV = $_;
local $^I = '.bac';
while( <> ){
if( s/$find/$replace/ig ) {
print;
}
else {
print;
}
}
}, $startdir);
find();
print "Finished";
I dont know why this is not working....and secondly I would like to introduce arrays and the process should loop through the file search for the strings in array 1 and replace it with the string in the second array. I think hash value pair .

Regards


w020637
Novice

May 29, 2009, 7:29 AM

Post #2 of 11 (1232 views)
Re: [w020637] Convert dev to uat [In reply to] Can't Post

Hi,

I was working on this stuff and as as I still continue I have reworked the codes with the help of some stuff that was already available on this forum.

#!/bin/perl -w

use strict;

my $find = 'this';
my $replace = 'that';

print qq~Finding "$find" and replacing it with "$replace"\n~;
my $filename="/test/perl/test.jil";
my $filename1="/test/perl/new_test.jil";
open(IN, "<$filename") || die "Can't open $filename";
open(OUT,">$filename1") || die "Can't open $filename1";

while( <IN> ){
s/$find/$replace/ig;
print OUT $_;
}
close(IN);
close(OUT);
print "Finished";


Now the next task which I working on is to have the $find and $replace as hash value pair or an array where the first value in $find get replaced

by the first value in replace as it parses the file.

Please suggest if there is already a thread that can guide in this direction.

Regards


w020637
Novice

May 29, 2009, 8:25 AM

Post #3 of 11 (1230 views)
Re: [w020637] Convert dev to uat [In reply to] Can't Post

Hi All,

Finally I gt what I wanted to do....here is the code


Code
  

#!/bin/perl -w

use strict;

print qq~Finding "$find" and replacing it with "$replace"\n~;
my $filename="/test/perl/test.jil";
my $filename1="/test/perl/new_test.jil";

my %dict;
#the pairs.txt is the file which contains key value combinations.

my $filename3="/test/perl/pairs.txt";
open(DICTFILE, "<$filename3") || die "Can't open $filename3";
while(<DICTFILE>){
chomp;
my ($key, $val) = split /=/;
$dict{$key} .= exists $dict{$key} ? "$val" : $val;
}

open(IN, "<$filename") || die "Can't open $filename";
open(OUT,">$filename1") || die "Can't open $filename1";

while( my $line = <IN> ){
foreach my $s (keys %dict){
$line =~ s/$s/$dict{$s}/g;
}
print OUT "$line";
}
close(IN);
close(OUT);
print "Finished";



Would appreciate if you guys can suggest any changes so it follows better coding norms and conventions.

Regards


KevinR
Veteran


May 29, 2009, 8:56 AM

Post #4 of 11 (1228 views)
Re: [w020637] Convert dev to uat [In reply to] Can't Post


Code
#!/bin/perl 
use strict;
use warnings;

print qq{Finding "$find" and replacing it with "$replace"\n};

my $filename = '/test/perl/test.jil';
my $filename1 = '/test/perl/new_test.jil';
my $filename3 = '/test/perl/pairs.txt';
my %dict;
#the pairs.txt is the file which contains key value combinations.


open(my $DICTFILE, "<", $filename3) or die "Can't open $filename3: $!";
while(<$DICTFILE>){
chomp;
my ($key, $val) = split /=/;
$dict{$key} .= $val;
}
close $DICTFILE;

open(my $IN, "<", $filename) or die "Can't open $filename: $!";
open(my $OUT,">", $filename1) or die "Can't open $filename1: $!";

while( my $line = <$IN> ){
foreach my $s (keys %dict){
$line =~ s/$s/$dict{$s}/g;
}
print $OUT $line;
}
close($IN);
close($OUT);
print 'Finished';


Use the "warnings" pragam instead of the -w switch.

Use paired brackets with the quoting operators (like qq{}) and perl will escape the brackets inside the string. If you use a single symbol delimiter (like qq~~) and it appears in the string, you have to manually escape it.

Use the three agrument form of open() .

Include $! in the results returned from "die".

Use indirect-filehandles ($IN instead of IN).

Use the proper quotes: single-quotes for strings with no interpolation and double-quotes for strings with varaibles or meta sequences that need interpolation.

Use "or" instead of "||" for program flow control. || has a higher precedence than "or".

Don't needlessly quote varaibles. for example:


Code
print OUT "$line";


There is no reason to put double-quotes around $line. You use quotes to make strings, in this case you are not making a string, just printing a scalar variable.


This line looks wrong:


Code
$dict{$key} .= exists $dict{$key} ? "$val" : $val;


You needlessly quote $val if the condition is true, but even if the condition is false, you still concatenate $val to $dict{$key}, to me it look slike it should be written like so:


Code
$dict{$key} .= $val;

-------------------------------------------------


w020637
Novice

May 29, 2009, 10:20 AM

Post #5 of 11 (1224 views)
Re: [KevinR] Convert dev to uat [In reply to] Can't Post

Appreciate your feedback/rcommendations Kevin



Thanks


w020637
Novice

May 29, 2009, 11:22 AM

Post #6 of 11 (1222 views)
Re: [w020637] Convert dev to uat [In reply to] Can't Post

Kevin,

I get this warning

Can't use an undefined value as a symbol reference at /build.pl line 37. When I remove the $ from IN it is resolved. What might be the reason. Thanks

line 37


Code
 open(my $IN, "<$filename") or die "Can't open $filename: $!";



FishMonger
Veteran / Moderator

May 29, 2009, 11:29 AM

Post #7 of 11 (1220 views)
Re: [w020637] Convert dev to uat [In reply to] Can't Post

The corrected code that Kevin posted doesn't have a line 37.

Please post the complete code you ran when you received that error.


w020637
Novice

May 29, 2009, 11:52 AM

Post #8 of 11 (1218 views)
Re: [FishMonger] Convert dev to uat [In reply to] Can't Post

Actually I have added some code before that and that is the reason you dont find the line number as 37 in the old prg. Attaching the new program. I am able to successfully run the prg after removing the my and the $.

The use warnings dont work for me as I dont have that module(neither can I add one).


Code
 #!/bin/perl -wuse strict; 
use POSIX;
use Env qw(RDAT); my $filename="$ARGV[1]";
my $filename1="$ARGV[2]";
my $ENV ;
print "$ARGV[0] \n";
my $type="$ARGV[0]";
print "$type \n";
if ($type eq 'D') {
$ENV= "$RDAT/DEV.txt";
}
if ($type eq 'U') {
print "$RDAT/UAT.txt \n";
$ENV= "$RDAT/UAT.txt";
}
elsif ($type eq 'U2') {
$ENV= "$RDAT/UAT2.txt";
}
elsif ($type eq 'U3') {
$ENV= "$RDAT/UAT3.txt";
}
print "$ENV \n";



my %dict;
open(DICTFILE, "<$ENV") or die "Can't open $ENV: $!";
while(<DICTFILE>){
chomp;
my ($key, $val) = split /=/;
$dict{$key} .= $val;
}
close DICTFILE;



open(my $IN, "<$filename") or die "Can't open $filename: $!";
open(my $OUT,">$filename1") or die "Can't open $filename1: $!";



while( my $line = <$IN> ){
foreach my $s (keys %dict){
$line =~ s/$s/$dict{$s}/g;
}


print $OUT $line;
}
close($IN);
close($OUT);
print "Finished";



KevinR
Veteran


May 29, 2009, 12:22 PM

Post #9 of 11 (1213 views)
Re: [w020637] Convert dev to uat [In reply to] Can't Post


In Reply To
Actually I have added some code before that and that is the reason you dont find the line number as 37 in the old prg. Attaching the new program. I am able to successfully run the prg after removing the my and the $.

The use warnings dont work for me as I dont have that module(neither can I add one).


I think you must be using a very old version of perl. You might not be able to use indirect-filehandles if that is the case even though they have been supported for many years now. Chnge the filehandles back to barewords. Also look into getting perl upgraded to nothing older than 5.8.8
-------------------------------------------------


(This post was edited by KevinR on May 29, 2009, 12:23 PM)


FishMonger
Veteran / Moderator

May 29, 2009, 12:32 PM

Post #10 of 11 (1210 views)
Re: [w020637] Convert dev to uat [In reply to] Can't Post

What version of perl are you using?

Please post the output of this command.

perl -v


FishMonger
Veteran / Moderator

May 29, 2009, 12:35 PM

Post #11 of 11 (1209 views)
Re: [w020637] Convert dev to uat [In reply to] Can't Post

The use of indirect filehandles was added in perl 5.6. If you have anything older than that, you going to have problems in lots of areas.

 
 


Search for (options) Powered by Gossamer Forum v.1.2.0

Web Applications & Managed Hosting Powered by Gossamer Threads
Visit our Mailing List Archives