- Perl ›
- Subroutine ›
- here
Subroutine definitions are not affected by scope
Subroutine definitions are not affected by scope.
{ sub func { return 2; } } my $ret = func ();
Subroutines are not affected by scope. At first glance, it's easy to think that func is invisible outside the scope, but that's not the case.
The subroutine definition is registered in symbol table at compile time. Since the symbol table can be referenced from anywhere in the program, subroutine can also be referenced from anywhere in the program.
Example
The subroutine definition is an example to understand that it is not affected by scope.
use strict; use warnings; # Subroutine definition and scope # Subroutines are not affected by scope. { sub func { return 2; } } print "1: Subroutine definition is not affected by scope.\n"; my $ret = func (); print "\$ret is $ret.\n";