Reopening file only if it was updated using regular open

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • rzismanx
    New Member
    • Oct 2008
    • 5

    Reopening file only if it was updated using regular open

    Hello
    I'm trying to reopen a certain file but only if it's updated:
    The problem is when i try to open a file (after it was changed and saved by me)for reading second time, i get nothing, like the file is empty but it's not.
    The filehandle is empty.Why?
    DOn't have a clue.
    The code is below.
    can some one help?
    Thanks
    Code:
    $last_acces=time;
    while($end_ctsc)
    {
    $full_path_file=lastmodifiedfile("path");
    open (DATA_log ,$full_path_file);
    $file_stat = stat(DATA_log);
    $new_acces=$file_stat->mtime;                
    close DATA_log;
    $Sub_is=$new_acces-$last_acces;
    if ($Sub_is)
    {
    $last_acces=$new_acces;
    $i=0;
    $full_path_file=lastmodifiedfile("path");
    open (DATA_log ,$full_path_file)||print("could not open the log");
    @file_log=<DATA_log>;
    close DATA_log;
    $sizefile_log = @file_log;
    print "the i is: $i, the sizefile_log is $sizefile_log\n";
    while ($i!=$sizefile_log)
    {
      #file handling 
    }
    }
  • eWish
    Recognized Expert Contributor
    • Jul 2007
    • 973

    #2
    After you open a file add a die statement after to make sure you did not have any problems.
    Code:
    open(my $file, '<', $file_to_open) || die "Can't open $file_to_open: $!\n";
    --Kevin

    Comment

    • rzismanx
      New Member
      • Oct 2008
      • 5

      #3
      Thanks for the response.
      I used $file as file handle,had the same result:
      First time it's working the second time it's empty.
      When i write second time i mean it could 500 time because i open the file and reopen it to check if it was updated using stat function.The funny thing is that i'm able to recognize if it was updated or not using open with filehandle but when i want to spread the data to an array i get it empty.
      Any ideas?

      Comment

      • KevinADC
        Recognized Expert Specialist
        • Jan 2007
        • 4092

        #4
        One potential problem is here:

        Code:
        $file_stat = stat(DATA_log);
        the stat() function returns a list so you have to assign its value to a list or array:

        Code:
        @stat = stat(DATA_log);
        Later you use $file_stat as if its an object:

        Code:
        $new_acces=$file_stat->mtime;
        Are you using a module that creates an object?


        You very much need to add "strict" and "warnings" to your perl program and note all the errors and fix them and post back here if you have more questions.

        Code:
        use strict;
        use warniings;
        use diagnostics;
        diagnostics will return more verbose nessages concerning the errors or warnings but it is optional.

        Comment

        • eWish
          Recognized Expert Contributor
          • Jul 2007
          • 973

          #5
          Here is a sample script that will create a test file if it does not exist. Then if you run it again it will modify the contents of the file and display the data.

          When you want to append a file then you must open it in append mode. Otherwise it will erase all of the data.

          Also, this sample script is intended to be run from a browser.

          Code:
          #! /usr/bin/perl -T
          
          [URL=http://perldoc.perl.org/functions/use.html]use[/URL] strict;
          use warnings;
          
          use CGI::Carp qw/fatalsToBrowser/;
          
          [URL=http://perldoc.perl.org/functions/print.html]print[/URL] "Content-type: text/html\n\n";
          
          [URL=http://perldoc.perl.org/functions/my.html]my[/URL] $file_to_open = './sample_data_file.txt';
          my $modified_time  = ([URL=http://perldoc.perl.org/functions/stat.html]stat[/URL]($file_to_open))[9];
          
          
          # If the target file does exists create it 
          if (not [URL=http://perldoc.perl.org/functions/-X.html]-e[/URL] $file_to_open) {
          	[URL=http://perldoc.perl.org/functions/open.html]open[/URL](my $fh, '>', $file_to_open) || [URL=http://perldoc.perl.org/functions/die.html]die[/URL] "Can't open $file_to_open.  Error Message: $!\n";	
          	print $fh "Some message\n";
          	[URL=http://perldoc.perl.org/functions/close.html]close[/URL]($fh);
          	
          	print 'File was created';
          }
          
          # If modified then update it
          if ($modified_time) {
          	open(my $fh, '>>', $file_to_open) || die "Can't open $file_to_open.  Error Message: $!\n";	
          	print $fh "Some new message\n";
          	close($fh);	
          	
          	print 'File was modified', "<br /><br />";
          }
          
          # Print modified contents
          if ($modified_time) {
          	open(my $fh, '<', $file_to_open) || die "Can't open $file_to_open.  Error Message: $!\n";
          	print "Modified Contents:<br />";
          	while (<$fh>) {
          		[URL=http://perldoc.perl.org/functions/chomp.html]chomp[/URL];
          		print $_, "<br>";
          	}
          	close($fh);
          }
          
          
          1;
          ------------------------------------------------------------
          Pragmas (perl 5.10.0) used :
          • strict - Perl pragma to restrict unsafe constructs
          • warnings - Perl pragma to control optional warnings

          Core (perl 5.10.0) Modules used :
          • CGI::Carp - CGI routines for writing to the HTTPD (or other) error log


          I just wanted to use this cool tool. :)

          --Kevin

          Comment

          Working...