M
Matthias Wille
Hello
I just started to learn perl and wrote my first program today (a big
step for me, a small step for mankind...). Actually it does what it is
supposed to do except for the part which is supposed to match a
certain string. I would appreciate if some perl cracks in this group
could take a short look at my script an tell me if it is good perl
style and tell me some improvements and shortcuts I can make. After
all I like to be a good perl programmer in the end...
I'm wondering if there is a simpler way for replacing strings in a
file without creating temp files.
Oh yes, the code should be as portable as possible, as it should run
also on windows...
Thanks very much!
Matthias
----
#!/usr/bin/perl
use Cwd;
use File::Copy;
use File::Basename;
use File:
ath;
#use strict;
#use warnings;
sub printUsage();
# find out current working directory
$cwd = Cwd::cwd();
# check that we are in the right directory
unless (basename(dirname($cwd)) eq "control") {
die "please run this skript from inside a test family folder, i.e.
'thread'";
}
# get command line arguments
($oldcase, $newcase) = @ARGV;
# require at least one argument
unless (defined $oldcase) {
printUsage();
exit 1;
}
$newcase = "$oldcase_COPY" unless defined $newcase;
$family = basename($cwd);
# check if .pm file of oldcase exists
die "Testcase $oldcase does not exist in this folder" unless (-e
"$oldcase.pm");
# try to open .pm files
open(OLD, "<$oldcase.pm") or die "could not open file $oldcase.pm for
reading: $!";
open(NEW, ">$newcase.pm") or die "could not open file $newcase.pm for
writing: $!";
# read in old file and change all occurances of $oldcase to $newcase
while($line = <OLD>) {
$line =~ s/$oldcase/$newcase/g;
print NEW $line;
}
# now the same again with the html file, if any present
if (-e "$oldcase.html") {
open(OLD, "<$oldcase.html") or die "could not open file $oldcase.html
for reading: $!";
open(NEW, ">$newcase.html") or die "could not open file $newcase.html
for writing: $!";
while($line = <OLD>) {
$line =~ s/$oldcase/$newcase/g;
print NEW $line;
}
}
else {
print STDOUT "html file for testcase $oldcase is missing. you shoul
add one...";
}
# add a new Case section for the new test in the isildap.ini file
open(OLD, "<isildap.ini") or die "could not open file isildap.ini for
reading: $!";
open(NEW, ">isildap.ini.new") or die "could not open file isildap.ini
for writing: $!";
# read all lines of isildap.ini file
$lines = join("", <OLD>);
# find matching 'Case = $oldcase { }' block and duplicate it
print STDOUT "found match\n" if $lines =~
m/Case.*=.*$oldcase.*\{(.*?)\}/s;
print NEW $lines;
print NEW "\n";
$newpattern = $&;
$newpattern =~ s/$oldcase/$newcase/g;
print NEW $newpattern;
# delete old isildap.ini file and rename new one
#unlink("isildap.ini") or die "could not delete old isildap.ini file";
#File::Copy::move("isildap.ini.new", "isildap.ini") or die "could not
rename file";
# add a new addVariants entry for the new test in the <family>.pm file
open(OLD, "<$family.pm") or die "could not open file $family.pm for
reading: $!";
open(NEW, ">$family.pm.new") or die "could not open file $family.pm
for writing: $!";
while($line = <OLD>) {
print NEW $line;
if($line =~ s/'$oldcase'/'$newcase'/g) {
print NEW $line;
}
}
# delete old $family.pm file and rename new one
# unlink("$family.pm") or die "could not delete old $family.pm file";
# File::Copy::move("$family.pm.new", "$family.pm") or die "could not
rename file";
exit 0;
# print some help on usage of this skript
sub printUsage() {
select STDOUT;
print "SYNOPSIS:\n";
print "\ttbcopytest testcasename [ newname ]\n";
}
I just started to learn perl and wrote my first program today (a big
step for me, a small step for mankind...). Actually it does what it is
supposed to do except for the part which is supposed to match a
certain string. I would appreciate if some perl cracks in this group
could take a short look at my script an tell me if it is good perl
style and tell me some improvements and shortcuts I can make. After
all I like to be a good perl programmer in the end...
I'm wondering if there is a simpler way for replacing strings in a
file without creating temp files.
Oh yes, the code should be as portable as possible, as it should run
also on windows...
Thanks very much!
Matthias
----
#!/usr/bin/perl
use Cwd;
use File::Copy;
use File::Basename;
use File:
#use strict;
#use warnings;
sub printUsage();
# find out current working directory
$cwd = Cwd::cwd();
# check that we are in the right directory
unless (basename(dirname($cwd)) eq "control") {
die "please run this skript from inside a test family folder, i.e.
'thread'";
}
# get command line arguments
($oldcase, $newcase) = @ARGV;
# require at least one argument
unless (defined $oldcase) {
printUsage();
exit 1;
}
$newcase = "$oldcase_COPY" unless defined $newcase;
$family = basename($cwd);
# check if .pm file of oldcase exists
die "Testcase $oldcase does not exist in this folder" unless (-e
"$oldcase.pm");
# try to open .pm files
open(OLD, "<$oldcase.pm") or die "could not open file $oldcase.pm for
reading: $!";
open(NEW, ">$newcase.pm") or die "could not open file $newcase.pm for
writing: $!";
# read in old file and change all occurances of $oldcase to $newcase
while($line = <OLD>) {
$line =~ s/$oldcase/$newcase/g;
print NEW $line;
}
# now the same again with the html file, if any present
if (-e "$oldcase.html") {
open(OLD, "<$oldcase.html") or die "could not open file $oldcase.html
for reading: $!";
open(NEW, ">$newcase.html") or die "could not open file $newcase.html
for writing: $!";
while($line = <OLD>) {
$line =~ s/$oldcase/$newcase/g;
print NEW $line;
}
}
else {
print STDOUT "html file for testcase $oldcase is missing. you shoul
add one...";
}
# add a new Case section for the new test in the isildap.ini file
open(OLD, "<isildap.ini") or die "could not open file isildap.ini for
reading: $!";
open(NEW, ">isildap.ini.new") or die "could not open file isildap.ini
for writing: $!";
# read all lines of isildap.ini file
$lines = join("", <OLD>);
# find matching 'Case = $oldcase { }' block and duplicate it
print STDOUT "found match\n" if $lines =~
m/Case.*=.*$oldcase.*\{(.*?)\}/s;
print NEW $lines;
print NEW "\n";
$newpattern = $&;
$newpattern =~ s/$oldcase/$newcase/g;
print NEW $newpattern;
# delete old isildap.ini file and rename new one
#unlink("isildap.ini") or die "could not delete old isildap.ini file";
#File::Copy::move("isildap.ini.new", "isildap.ini") or die "could not
rename file";
# add a new addVariants entry for the new test in the <family>.pm file
open(OLD, "<$family.pm") or die "could not open file $family.pm for
reading: $!";
open(NEW, ">$family.pm.new") or die "could not open file $family.pm
for writing: $!";
while($line = <OLD>) {
print NEW $line;
if($line =~ s/'$oldcase'/'$newcase'/g) {
print NEW $line;
}
}
# delete old $family.pm file and rename new one
# unlink("$family.pm") or die "could not delete old $family.pm file";
# File::Copy::move("$family.pm.new", "$family.pm") or die "could not
rename file";
exit 0;
# print some help on usage of this skript
sub printUsage() {
select STDOUT;
print "SYNOPSIS:\n";
print "\ttbcopytest testcasename [ newname ]\n";
}