Quote:
Originally Posted by eWish
Will this style of coding cause me some speeding penalties? I am just trying to reduce the number of times I have to write an open function. Naturally, this is just an example.
- my ($file_to_modify) = &set_log_file(3);
-
my (@act_valid_log) = &read_data_file(3);
-
-
#do something with the result of @act_valid_log
-
-
-
sub set_log_file {
-
-
my ($get_path) = @_;
-
-
my ($return_path) = $get_path == 1 ? qq{giftcard_log.log}
-
: $get_path == 2 ? qq{giftcard_db.log}
-
: $get_path == 3 ? qq{giftcard_act_valid.log}
-
: &errorcode(__FILE__, __LINE__, "Your entry does not match anything.", "$!");
-
;
-
-
return ($return_path);
-
-
}
-
-
sub read_data_file {
-
-
my ($file_to_read) = @_;
-
my ($giftcard_file) = &set_log_file($file_to_read);
-
my (@data);
-
-
open(my $READ_DATA_FILE, '<', $giftcard_file) || &errorcode(__FILE__, __LINE__, $giftcard_file, "$!");
-
@data = <$READ_DATA_FILE>;
-
close($READ_DATA_FILE);
-
-
return(@data);
-
-
}
Looking for some feedback.
--Kevin
It will be slightly more efficient if you just use one subroutine. set_log_file looks like it could be part of read_data_file instead of a seperate sub. But if you have other subs that call set_log_file that would be OK. Overall, there is nothing really inefficient about the code that I see. Personally I don't like to write a single scalar as a list assignment:
my ($file_to_read) = @_;
I would write that as:
my $file_to_read = $_[0];
although I am not sure if it is anymore efficient one way or the other, it's really a matter of personal preference I guess.