#!/uns/bin/perl -w package util_mde; require 5.003; # uses prototypes require Exporter; @ISA = qw(Exporter); @EXPORT = qw( hash_value_replace mail_file mail system_or_die ); use checkargs; use strict; use Carp; use English; ########################################################################### ### Hashes ### sub hash_value_replace ( $$% ) { my ($old_val, $new_val, %hash) = check_args_at_least(2, @_); for my $key (keys %hash) # precompute, so side effects don't affect { if ($hash{$key} eq $old_val) { $hash{$key} = $new_val; } } return %hash; } ########################################################################### ### External processes ### sub mail_file ( $$$ ) { my ($address, $subject, $filename) = check_args(3, @_); return system_or_die("/bin/mail -s '$subject' $address < $filename"); } sub mail ( $$$ ) { my ($address, $subject, $body) = check_args(3, @_); return system_or_die("echo '$body' | /bin/mail -s '$subject' $address"); } # Execute the command; die if its execution is erroneous. sub system_or_die ( $ ) { my ($cmd) = check_args(1, @_); my $result = system($cmd); if ($result != 0) { croak "Failed executing $cmd"; } return $result; } ## I'm not convinced this versin is performing correctly. # my $sod_count = 0; # # # Execute the command; die if its execution is erroneous. # sub system_or_die ( $ ) # { my ($cmd) = check_args(1, @_); # my $errfile = "/tmp/sod$$.$sod_count"; # $sod_count++; # my $result = system("$cmd >& $errfile"); # if ($result != 0) # { # It doesn't help to print $! here. # if ((-f $errfile) && !(-z $errfile)) # { open(ERRS, "<$errfile"); # my @err_output = ; # close(ERRS); # unlink $errfile; # croak "Failed executing $cmd:\n@err_output"; } # unlink $errfile; # croak "Failed executing $cmd"; } # unlink $errfile; # return $result; # } ########################################################################### ### End of file ### # Return true to indicate success loading this package. 1;