Scope/Function names and labels: Difference between revisions

Added Perl example
m (→‎{{header|REXX}}: added comments in the REXX section header explaining a method to include multiple labels on the same line.)
(Added Perl example)
Line 578:
Functions are global and must be defined before use.
Methods are global and must be declared before use. They can be used before a method implementation.
 
=={{header|Perl}}==
Perl allows various ways to futz with scope, but keeping it simple: Routines are package-scoped, and in each package the final definition of the routine is the one that is used throughout. Labels are generally also package-scoped, except when it comes to <code>goto</code>; let's don't even go there.
<lang perl>no warnings 'redefine';
 
sub logger { print shift . ": Dicitur clamantis in deserto." }; # discarded
 
logger('A'); # can use before defined
HighLander::logger('B'); # ditto, but referring to another package
 
package HighLander {
logger('C');
sub logger { print shift . ": I have something to say.\n" }; # discarded
sub down_one_level {
sub logger { print shift . ": I am a man, not a fish.\n" }; # discarded
sub down_two_levels {
sub logger { print shift . ": There can be only one!\n" }; # routine for 'Highlander' package
}
}
logger('D');
}
 
logger('E');
sub logger {
print shift . ": This thought intentionally left blank.\n" # routine for 'main' package
};</lang>
{{out}}
<pre>A: This thought intentionally left blank.
B: There can be only one!
C: There can be only one!
D: There can be only one!
E: This thought intentionally left blank.</pre>
 
=={{header|Perl 6}}==
2,392

edits