Refactor function export code.

In the act of removing a now-extraneous method prototype in the next
patch I noticed that I had not fixed ksb::Debug's import method to work
with method that had no prototype.

"Don't Repeat Yourself" being a good idea and all, I refactored both
methods to a common one that works on everything.
wilder
Michael Pyne 14 years ago
parent e93d2f62e0
commit 332a44e284
  1. 61
      kdesrc-build

@ -133,20 +133,7 @@ my $run_mode = 'build'; # Determines if updating, building, installing, etc.
my @exports = qw(debug pretending debugging whisper
note info warning error pretend clr);
# This loop is only slightly "magical". Basically to import functions
# into a different package in Perl, we can use something like:
# *PACKAGE::FUNCTION = \&SOURCE_PACKAGE::FUNCTION;
# where the *PACKAGE modifies the symbol table for that package.
#
# The extra part, which requires using eval, is to predeclare the
# subroutine with a prototype first.
# "sub foo($old_prototype);"
for my $fn (@exports) {
my $prototype = prototype($fn);
eval "sub ${caller}::${fn}(${prototype});\n" .
"*${caller}::${fn} = \\&${pkg}::${fn};";
}
ksb::Util::exportFunctionsToPackage($pkg, $caller, @exports);
}
# I'm lazy and would rather write in shorthand for the colors. This sub
@ -299,17 +286,17 @@ my $run_mode = 'build'; # Determines if updating, building, installing, etc.
ksb::Debug->import();
sub import
# This function exports some functions from a source package (who should
# actually call this function) to a destination package (who should have
# made the request by calling $sourcePackage->import()). Any subroutine
# prototypes defined when this function is called are copied over as well.
#
# The first parameter is the name of the source package.
# The second parameter is the name of the destination package.
# The remaining parameters are the names of the functions to export.
sub exportFunctionsToPackage
{
my $pkg = shift;
my $caller = caller;
my @exports = qw(list_has make_exception assert_isa assert_in
croak_runtime croak_internal
log_command disable_locale_message_translation
split_quoted_on_whitespace
safe_unlink safe_system p_chdir super_mkdir
filter_program_output prettify_seconds
);
my ($sourcePackage, $destinationPackage, @exports) = @_;
# This loop is only slightly "magical". Basically to import functions
# into a different package in Perl, we can use something like:
@ -317,21 +304,37 @@ my $run_mode = 'build'; # Determines if updating, building, installing, etc.
# where the *PACKAGE modifies the symbol table for that package.
#
# The extra part, which requires using eval, is to predeclare the
# subroutine with a prototype first.
# subroutine with a prototype first (if that subroutine has a
# prototype).
# "sub foo($old_prototype);"
for my $fn (@exports) {
my $prototype = prototype($fn);
my $prototype = prototype("${sourcePackage}::$fn");
if ($prototype) {
eval "sub ${caller}::${fn}(${prototype});\n" .
"*${caller}::${fn} = \\&${pkg}::${fn};";
eval "sub ${destinationPackage}::${fn}(${prototype});\n" .
"*${destinationPackage}::${fn} = \\&${sourcePackage}::${fn};";
}
else {
eval "*${caller}::${fn} = \\&${pkg}::${fn};";
eval "*${destinationPackage}::${fn} = \\&${sourcePackage}::${fn};";
}
}
}
sub import
{
my $pkg = shift;
my $caller = caller;
my @exports = qw(list_has make_exception assert_isa assert_in
croak_runtime croak_internal
log_command disable_locale_message_translation
split_quoted_on_whitespace
safe_unlink safe_system p_chdir super_mkdir
filter_program_output prettify_seconds
);
exportFunctionsToPackage($pkg, $caller, @exports);
}
# Function to work around a Perl language limitation.
# First parameter is a reference to the list to search. ALWAYS.
# Second parameter is the value to search for.

Loading…
Cancel
Save