Skip to content

Commit

Permalink
Provide a practical example of interpolating package names
Browse files Browse the repository at this point in the history
  • Loading branch information
tbrowder authored and JJ committed Sep 7, 2021
1 parent 2b3d848 commit 68484aa
Showing 1 changed file with 44 additions and 0 deletions.
44 changes: 44 additions & 0 deletions doc/Language/packages.pod6
Expand Up @@ -257,6 +257,50 @@ go into that package by default. Process-wide variables live in the
C<PROCESS> package. Most predefined globals such as C<$*UID> and C<$*PID>
are actually process globals.
=head1 Programmatic use of modules
It is sometimes useful to be able to programmatically define and use modules.
Here is a practical example.
Assume we have a series of modules with the modules files in a directory
tree like this:
=begin code
lib/
TomtomMaps/
EG/
# a directory of modules created from the Tomtom Maps SDK
A01.rakumod
#..l
C09.rakumod
=end code
Module C<TomtomMaps::EG::C09> looks like this:
=begin code
unit module TomtomMaps::EG::C09;
# ensure you use the 'our'
our sub print-example($fp, :$api-maps-key) { # do not need to export the sub
$fp.print: qq:to/HERE/;
# ... the example html file
HERE
}
=end code
We can access and use the subroutines like this:
=begin code
my $module-dir = 'TomtomMaps::EG';
my $module = 'C09';
my $m = "{$module-dir}::{$module}";
# you must use the run-time statement, not the compile-time 'use'
require ::($m);
# execute the subroutine
&::($m)::print-example($fp, :$api-maps-key); # the map's html file is written
=end code
=end pod

# vim: expandtab softtabstop=4 shiftwidth=4 ft=perl6

0 comments on commit 68484aa

Please sign in to comment.