Course of Raku / Advanced / Modules / Packages and namespaces 🆕 / Exercises / A package
Solution: A package
Here is a possible solution to the task.
Code
package Temperature {
our $freezing = 0;
our sub fahrenheit($c) { $c * 9/5 + 32 }
}
say $Temperature::freezing;
say Temperature::fahrenheit(100);🦋 You can find the source code in the file a-package.raku.
Output
0
212Comments
Both the variable and the subroutine are declared with
our, so both become part of theTemperaturenamespace and are reachable from outside.The variable is reached as
$Temperature::freezing— sigil, package name, then variable name — while the subroutine is called asTemperature::fahrenheit(100). Converting100gives100 * 9/5 + 32, which is212.A plain
packageprovides just the namespace. For a reusable library we would have usedmoduleinstead — and, once objects are on the table, aclass— but the namespace mechanism is the same in each case.
Course navigation
← A package | A nested name →