diff --git a/kdesrc-build b/kdesrc-build index 36c4b7f..76e6f11 100755 --- a/kdesrc-build +++ b/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.