use autodie; # Using this module means open and close errors are # handled nicely. my @affected_fields = qw/0 8/; .. # Figure out the input and the output file names here .. .. open ( my $input_fh, '<', $input_file ); open ( my $output_fh, '>', $output_file ); while ( <$input_file> ) { my @fields = split ( /,/ ); if ( $type =~ /^en(crypt)?$/ ) { foreach ( @affected_fields ) { $fields[ $_ ] = $cipher->encrypt( $fields[ $_ ] ); } } else { # I would prefer to use an elsif here, but # you could catch this during parameter checking. foreach ( @affected_fields ) { $fields[ $_ ] = $cipher->decrypt( $fields[ $_ ] ); } } print $output_fh join (',', @fields ) . "\n"; } close ( $input_fh ); close ( $output_fh );