Home > Software engineering >  How to print only caller function in Perl
How to print only caller function in Perl

Time:06-15

I am trying to print caller function in Perl. Its displaying as per the expectation.

Below is the code:

#!/usr/bin/perl

use strict; use warnings;

my $a = 5;
my $b = fun1($a);

print "a:$a ** b:$b\n";

sub fun1 {
    my $r = shift;
    my $s = fun2($r);
    return $s;
}

sub fun2 {
    my $p = shift;
    print "the calling function is:", (caller 1)[3], "\n";
    print "and you're in:", (caller 0)[3], "\n";
    my $q = $p * 5;
    return $q;
}

Output:

the calling function is:main::fun1
and you're in:main::fun2
a:5 ** b:25

How can I print only fun1 and fun2 instead of main::fun1 and main::fun2 respectively.

If there is a way to print them according to I stated above its good. Or I would have to trim the result :(

CodePudding user response:

You can use the following:

my $name = $full_name =~ s/^.*:://sr;
  •  Tags:  
  • perl
  • Related