Hi,
I need to generate a file and before doing so,I have to check the whether the file exists and if so ,is it modified or not .I know in the recent past there have been lot of questions on this but my question is different.
I am checking in a different way.
i,e I am pushing the contents of the existing file "file1" into any array "@seq1" and the contents of the file to be generated is stored in an array "@seq2".
I am using Perl module Algorithm::diff to find the difference of 2 arrays.
I want to know is this the right way to find whether the existing file is modified or not ?
my $flag = 1;
if ( -e $file1 ) {
print "Script '$file1' already exists\n";
open(FILE1,"$file1");
my @seq1 = <FILE1>;
close FILE1;
# @seq2 contains the contents of the file to generated.
my $diff = Algorithm::Diff->new( \@seq1, \@seq2 );
my $sep;
my $flag = 1;
$diff->Base( 1 ); # Return line numbers, not indices
while( $diff->Next() ) {
next if $diff->Same();
if( ! $diff->Items(2) || ! $diff->Items(1) ) {
$flag = 0;
print $diff->Get(qw( Min1 Max1 Max2 )),"\n";
}
else {
$flag = 0;
$sep = "---\n";
print $diff->Get(qw( Min1 Max1 Min2 Max2 )),"\n";
}
print "< $_" for $diff->Items(1);
print $sep;
print "> $_" for $diff->Items(2);
}
if($flag == 0) {
print
"User edits,do u want to overwrite the exisiting $file1?";
print "[INFO] Continue ? [no]";
my $answer = <STDIN> ; chomp $answer;
if ($answer =~ /^n/i) {
print"Not Generated the script '$file1'";
}
....
Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
Read Where should I post X? if you're not absolutely sure you're posting in the right place.
Please read these before you post! —
Posts may use any of the Perl Monks Approved HTML tags:
- a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
| |
For: |
|
Use: |
| & | | & |
| < | | < |
| > | | > |
| [ | | [ |
| ] | | ] |
Link using PerlMonks shortcuts! What shortcuts can I use for linking?
See Writeup Formatting Tips and other pages linked from there for more info.