0: =pod
1:
2: is_constant($sub_name) - returns true if subroutine is a
3: constant, false if not. $sub_name must be the fully
4: qualified name (Package::name) of a subroutine.
5:
6: =cut
7:
8: sub is_constant {
9: no strict 'refs';
10: my $name = shift;
11: my $code = *{$name}{CODE};
12:
13: # must have any empty prototype to be a constant
14: my $proto = prototype($code);
15: return 0 if defined $proto and length $proto;
16:
17: # attempt to redefine to itself - this will cause a
18: # warning for a real constant that starts with "Constant"
19: my $is_const;
20: {
21: local $SIG{__WARN__} = sub { $is_const = 1 if $_[0] =~ /^Constant/ };
22: eval { *{$name} = sub () { "TEST" } };
23: }
24:
25: # set it back
26: {
27: no warnings;
28: eval { *{$name} = $code; };
29: }
30:
31: # all done
32: return $is_const;
33: }
In reply to is_constant() - a routine that detects constant subs by samtregar
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |