Loading ...
Sorry, an error occurred while loading the content.

mac2pc formatting subroutine works but worried about exceptions

Expand Messages
  • vivek_ramaswamy <vivek_ramaswamy@yahoo.c
    I ve tested the following function in a 4000 round test and it works okay...but users have found files they ve given to it lose data and this is the only
    Message 1 of 3 , Feb 28, 2003
    • 0 Attachment
      I've tested the following function in a 4000 round test and it works
      okay...but users have found files they've given to it lose data and
      this is the only function I think could be the cause. Does anyone see
      anything dangerous about this function or conditions in which it might
      be incorrectly flushing/adding to the array?

      Thanks for your help!

      Vivek

      sub macFormat{

      my(@arr2,$elem,$finalresult);

      $FILE1 = $_[0];
      open FILE1 or die "ouch! file not found!\n";

      while($_ = <FILE1>){

      my (@arr1,$test,$strlength);

      $strlength = length $_;
      for($i=0;$i<$strlength;$i++){
      $elem = chop $_;
      $test = ord $elem;
      if($test==13){
      $elem = chr 10;
      }
      unshift @arr1, $elem;
      }
      $result1 = join "",@arr1;
      push @arr2,$result1;
      }

      close FILE1;

      $FILE1 = "> ".$FILE1;
      open FILE1 or die "ouch! file not found!\n";
      select(FILE1);
      $| = 1;
      foreach $elem(@arr2){
      print FILE1 $elem;
      }

      close FILE1;
      select(STDOUT);
      $|=1;
      return;
      }
    • Charles K. Clarkson
      ... macFormat() doesn t seem to be the problem. One way to aid you in testing would be to create a temporary file instead of placing the entire file into
      Message 2 of 3 , Feb 28, 2003
      • 0 Attachment
        vivek_ramaswamy <vivek_ramaswamy@...> wrote:
        :
        : Does anyone see anything dangerous about this
        : function or conditions in which it might
        : be incorrectly flushing/adding to the array?

        macFormat() doesn't seem to be the problem.
        One way to aid you in testing would be to
        create a temporary file instead of placing the
        entire file into @arr2. The inefficiency could
        be conquered with translate (tr///) instead of
        the for loop.

        If you are getting unequal file lengths,
        perhaps it's a file naming problem. If two
        people are working on the same file and file
        locking is not in place you might get file
        corruption. Try file locking on the input
        file.

        temp_file() should return a unique
        temporary file name. It doesn't have to be a
        subroutine. I just cheated. I also didn't run
        this. I just checked it for syntax.


        sub macFormat {
        die "You must supply a file name to macFormat()" unless @_;

        # don't clobber any open file handles.
        # when this sub ends, previous values (if any)
        # of IN or OUT will be restored.
        local *IN;
        local *OUT;

        my $file_name = shift;
        my $temp_file_name = temp_file( $file_name );

        open IN, '<', $file_name or die "Cannot open $file_name: $!";
        open OUT, '>', $temp_file_name or die "Cannot open $temp_file_name:
        $!";

        while ( <IN> ) {
        tr/\015/\012/;
        print OUT;
        }

        close OUT;
        close IN;

        # compare the lengths of the two files
        if ( -s $file_name == -s $temp_file_name ) {
        # if they are equal in length, everything seems okay.
        # delete the old file, rename the temp file.
        return 1;

        } else {
        # if they are not the same length, something went wrong.
        # the original file is still intact and an
        # alarm could be emailed to the programmer.
        return;
        }

        }


        HTH,

        Charles K. Clarkson
        --
        Head Bottle Washer,
        Clarkson Energy Homes, Inc.
        Mobile Home Specialists
        254 968-8328
      • vivek_ramaswamy <vivek_ramaswamy@yahoo.c
        Thanks for the feedback Charles. I ll try out your suggestions. Vivek
        Message 3 of 3 , Mar 3, 2003
        • 0 Attachment
          Thanks for the feedback Charles. I'll try out your suggestions.

          Vivek

          --- In perl-beginner@yahoogroups.com, "Charles K. Clarkson"
          <cclarkson@h...> wrote:
          > vivek_ramaswamy <vivek_ramaswamy@y...> wrote:
          > :
          > : Does anyone see anything dangerous about this
          > : function or conditions in which it might
          > : be incorrectly flushing/adding to the array?
          >
          > macFormat() doesn't seem to be the problem.
          > One way to aid you in testing would be to
          > create a temporary file instead of placing the
          > entire file into @arr2. The inefficiency could
          > be conquered with translate (tr///) instead of
          > the for loop.
          >
          > If you are getting unequal file lengths,
          > perhaps it's a file naming problem. If two
          > people are working on the same file and file
          > locking is not in place you might get file
          > corruption. Try file locking on the input
          > file.
          >
          > temp_file() should return a unique
          > temporary file name. It doesn't have to be a
          > subroutine. I just cheated. I also didn't run
          > this. I just checked it for syntax.
          >
          >
          > sub macFormat {
          > die "You must supply a file name to macFormat()" unless @_;
          >
          > # don't clobber any open file handles.
          > # when this sub ends, previous values (if any)
          > # of IN or OUT will be restored.
          > local *IN;
          > local *OUT;
          >
          > my $file_name = shift;
          > my $temp_file_name = temp_file( $file_name );
          >
          > open IN, '<', $file_name or die "Cannot open $file_name: $!";
          > open OUT, '>', $temp_file_name or die "Cannot open $temp_file_name:
          > $!";
          >
          > while ( <IN> ) {
          > tr/\015/\012/;
          > print OUT;
          > }
          >
          > close OUT;
          > close IN;
          >
          > # compare the lengths of the two files
          > if ( -s $file_name == -s $temp_file_name ) {
          > # if they are equal in length, everything seems okay.
          > # delete the old file, rename the temp file.
          > return 1;
          >
          > } else {
          > # if they are not the same length, something went wrong.
          > # the original file is still intact and an
          > # alarm could be emailed to the programmer.
          > return;
          > }
          >
          > }
          >
          >
          > HTH,
          >
          > Charles K. Clarkson
          > --
          > Head Bottle Washer,
          > Clarkson Energy Homes, Inc.
          > Mobile Home Specialists
          > 254 968-8328
        Your message has been successfully submitted and would be delivered to recipients shortly.