Consider the following Perl script script.pl as an example:
use strict;
use warnings;
sub f1
{statements}
sub f2
{statements}
sub f3
{statements}
f1();f2();f3();
When I execute the script, it should show the following output:
./script.pl
number of subroutines:3
names of subroutines:f1 f2 f3
When the code is executed, how can I count the number of subroutines, get their names, and then print them during runtime?
You are looking for Devel::Symdump:
#!/usr/bin/env perl
use strict;
use warnings;
{
require Devel::Symdump;
my $sym = Devel::Symdump->new('main');
my @subs = $sym->functions;
printf "Number of subroutines: %d\n", scalar @subs;
printf "Names of subroutines: %s\n", join(q{, } => map { s/^main:://; $_ } @subs);
}
sub f1 {
# statements
}
sub f2 {
# statements
}
sub f3 {
# statements
}
f1();
f2();
f3();
Output:
Number of subroutines: 3 Names of subroutines: f2, f1, f3
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With