September 2017
Beginner
402 pages
9h 52m
English
The need keyword loads a module at compile time but does not import the names from it. Loading a module also means that all the instructions within it will be executed. Also, the BEGIN block will be run. Let us add a couple of printing instructions to the module and see how it changes the output.
Here is the new module:
unit module Add;say 'Start';sub add($a, $b) is export { return $a + $b;}BEGIN { say 'This is Add.pm';}
In the main program, we use need instead of use this time, as shown:
need Add;my $a = 10;my $b = 20;my $sum = add($a, $b);say $sum;
Because the functions from the module are not imported to the program, it will not work, although, the output from the module itself will appear (the BEGIN module is triggered first, ...
Read now
Unlock full access