I guess you can not do the obvious and patch in a flag to your code that is flipped by the same logic that decides whether to alter the tied array ?

use Tie::File; tie @array, 'Tie::File', filename or die ...; if ($something) { push @array, $something_else $file_altered++; }

Another other way would be to use Tie::Watch to notify you if changes are made to the file tied array. I am assuming you can chain these two together, will try some test code myself and report back later if I get time.

you could also do an MD5 hash of your tied array when you first open it and compare this to one before you untie it. This is going to be a lot more costly than comparing the modification times.

Update

OK I have tried Tie::Watch and Tie::Array together and it looks like they do not chain. Whichever is done last is the only one in effect. My code is below in case a wiser monk than I can point out my error. It may still be possible to create a new array in place of the original, Tie::Watch it and have the methods in Tie::Watch call the appropriate update to the Tie::File array. Without further ado here is my test.
#!/usr/bin/perl use strict; use warnings; use Tie::File; use Tie::Watch; $main::touched=0; my @array; my $file='test_tie_file'; sub touched {$main::touched=1} push @array, int (rand 1000) until $#array > 39; # fill it on the firs +t pass my $watch = Tie::Watch->new( -variable => \@array, -debug => 0, -shadow => 1, -fetch => sub {print "Fetch\n"; my $self=shift; $self->Fetch}, -store => sub {print "Store\n"; $main::touched=1; my $self=shift; $self->Store (@_)}, -push => sub {print "Push\n"; $main::touched=1; my $self=shift; $self->Push (@_)}, -clear => sub {print "Clear\n"; $main::touched=1; my $self=shift; $self->Clear (@_)}, -extend => sub {print "Extend\n"; $main::touched=1; my $self=shift; $self->Extend (@_)}, ); tie @array, 'Tie::File', $file or die "cannot tie to $file: $!\n"; $main::touched=0; print "array of length $#array is now watched\n"; print "would you like to alter the file (y/n)\n"; my $ansa=<STDIN>; push @array, 'touched' if $ansa =~/^y/; untie @array; $watch->Unwatch; if ($main::touched) { print "File has been altered\n" } else { print "File remains the same\n"; }

Cheers,
R.

Pereant, qui ante nos nostra dixerunt!

In reply to Re: tie::file - file modification status by Random_Walk
in thread tie::file - file modification status by pearlie

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • 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:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.