use strict ; use warnings ; sub del { my ( $delroadName, $delroadNumber ) = @_ ; # This line handles params for function, # moved to first part of function my $process_file = "data.txt"; # Variable name changed to reflect action - # This kind of thing can mess things up later. my @data ; { # Adding additional scope to prevent missuse or error use of filehandle. open( my $data_file_handle, '<', "$process_file" ) # Changed to 3 param open and replaced bareword file handle. or die("Can't open file: $process_file"); # Changed || to 'or' in case someone removes brackets for 'open' # Also added file name to error message. @data = <$data_file_handle> ; # NOTE: 'my' removed to ensure scope. close( $data_file_handle ); } chomp( @data ); open( my $out_file_handle,'>', $process_file ) or die("Can't open file: $process_file"); # Modified as above. foreach my $line_from_file ( @data ) { # Changed from for to foreach removed dependence on $_ my @field = split( /\:/, $line_from_file ); # next if( ( $field[0] eq $delroadName ) && ( $field[1] == $delroadNumber ) ); # Added brackets and changed $field[0] in both to [0] and [1] next if( ( $field[0] eq $delroadName ) && ( $field[1] == $delroadNumber ) ); print $out_file_handle $line_from_file, "\n"; # Modified to print to file handle - Else the file will be truncated # Since you are open is with '>' and will remain blank. } # close $output; close $process_file; # You want to close the file handle, not the string. }