GetRc

 view release on metacpan or  search on metacpan

GetRc.pm  view on Meta::CPAN

  $self->dprint("Can't read file ".$self->{'filename'}),return(-2) unless ( -r $self->{'filename'} || -R $self->{'filename'} );

  ### locking ? uff .. this a bit messy code
  if ( $self->{'lock'} ) {
    eval {
      local $SIG{ALRM} = sub { die "File lock timeouted\n" };
      alarm($self->{'locktimeout'});
      $self->dprint("openning file ".$self->{'filename'});
      $rc = new IO::File $self->{'filename'}, O_RDONLY;
      croak "Can't open file ".$self->{'filename'}." : $!" unless  ( defined $rc );
      $self->dprint("locking file ".$self->{'filename'});
      flock($rc,LOCK_EX);
      $self->dprint($self->{'filename'} . " locked");
      alarm(0);
    };

  if ($@) {
    $self->dprint("File lock timeouted\n") if $@ eq "File lock timeouted\n";
    return(-3);
  }   

  } else {
    $self->dprint("openning file ".$self->{'filename'});
    $rc = new IO::File $self->{'filename'}, O_RDONLY;
    croak "Can't open file ".$self->{'filename'}." : $!" unless ( defined $rc );
  }

  my $separator = $self->{'ifs'};

    while (<$rc>) {
      chomp;
      #### Skip blank text entry fields and comment
      next if ( /^\s*#/ || /^\s*$/ || /^\s*\;/);

      #### Allow for multiple line values 
      if (s/\\$//) {
        $_ .= <$rc>;
        redo;
      }
      
      ($key,$value) = /\s*(.*?)${separator}(.*)/;

      ### skip empty keys
      $self->dprint("skip line: $_"),next if ( !defined($value) || !$key );
      
      #### Allow for multiple values of a single name
      if ($h_input{"$key"} && $self->{'multivalues'}) {
        $h_input{"$key"} .= ", " ;
        $h_input{"$key"} .= $value;
      } else {
        $h_input{"$key"} = $value;
      }
    }
  flock($rc,LOCK_UN),$self->dprint( $self->{'filename'} ." unlocked") if ( $self->{'lock'} );
  $rc->close;
  croak "Can't close file ".$self->{'filename'} .": $1" if $?;
  $self->dprint( $self->{'filename'} ." closed");
  return(0);
}

sub updaterc ($\%){
  my $self = shift;
  local *h_input = shift;
  my ( $key, $value, $rc, %update_input);

  $self->dprint("join to updaterc");
  $self->dprint("WARN: file ".$self->{'filename'}." doesn't exist") unless ( -e $self->{'filename'} );
  $self->dprint("WARN: Can't read file ".$self->{'filename'}) unless ( -r $self->{'filename'} || -R $self->{'filename'} );

  ### locking ? uff .. this a bit messy code
  if ( $self->{'lock'} ) {
    eval {
      local $SIG{ALRM} = sub { die "File lock timeouted\n" };
      alarm($self->{'locktimeout'});
      $self->dprint("openning file ".$self->{'filename'});
      $rc = new IO::File $self->{'filename'}, O_RDWR|O_CREAT;
      croak "Can't open file\n" unless ( defined $rc );
      $self->dprint("locking file ".$self->{'filename'});
      flock($rc,LOCK_EX);
      $self->dprint($self->{'filename'} . " locked");
      alarm(0);
    };

  if ($@) {
    return(-3),$self->dprint("File lock timeouted\n") if $@ eq "File lock timeouted\n";
    return(-4),$self->dprint("Can't open file ".$self->{'filename'}) if $@ eq "Can't open file\n";
    
  }   

  } else {
    $self->dprint("openning file ".$self->{'filename'});
    $rc = new IO::File $self->{'filename'}, O_RDWR|O_CREAT;
    croak "Can't open file ".$self->{'filename'}." : $!" unless ( defined $rc );
  }

  my $separator = $self->{'ifs'};

    while (<$rc>) {
      chomp;
      #### Skip blank text entry fields and comment
      next if ( /^\s*#/ || /^\s*$/ || /^\s*\;/);

      #### Allow for multiple line values 
      if (s/\\$//) {
        $_ .= <$rc>;
        redo;
      }
      
      ($key,$value) = /\s*(.*?)${separator}(.*)/;

      ### skip empty keys
      $self->dprint("skip line: $_"),next if ( !defined($value) || !$key );
      
      #### Allow for multiple values of a single name
      if ($update_input{"$key"} && $self->{'multivalues'}) {
        $update_input{"$key"} .= ", " ;
        $update_input{"$key"} .= $value;
      } else {
        $update_input{"$key"} = $value;
      }
    }



( run in 0.611 second using v1.01-cache-2.11-cpan-9581c071862 )