...or, with a bit more complexity for `error checking'. Bit of a smartass, but it does copy/paste, and it does provide an example of named-ref style params :)
#!/usr/bin/perl
use warnings;
use strict;
my $need_work = 10;
my $i_tried = 0;
my $die_if_its_broke = 0; # bool: true is fatal
while ( $need_work > $i_tried ) {
$i_tried++;
handle_error({
value => $i_tried,
die_if_broke => $die_if_its_broke,
});
print "From main(), I've tried $i_tried\n";
}
sub handle_error {
my $params = shift;
my $value_to_check = $params->{ value };
my $death = $params->{ die_if_broke };
my $error_condition = 5;
if ( $value_to_check == $error_condition ) {
die "Fatality!!: $!" if $death;
open my $fh, ">", 'logfile.txt'
or die "Can't open the fscking file: $!";
my $error_statement
= "We've hit the error threshold " .
": $error_condition!\n";
print $fh $error_statement;
}
}
Steve