title: How to get reference to parent class subroutine perl
body: <p>I have a situation where in child class, I need a reference of subroutines defined in parent class which I need to pass to some other class which would execute them.
  So I was wrote following sample modules for testing the same. </p>
  
  <h1>Parent1.pm</h1>
  
  <pre><code>package Parent1;
  
  sub new {
      my ($class, $arg_hash) = @_;
  
      my $self = bless $arg_hash, $class; 
  
      return $self;
  }
  
  sub printHello{
  
      print "Hello\n";
  }
  
  sub printNasty{
      print "Nasty\n";
  }
  1;            
  </code></pre>
  
  <h1>Child1.pm</h1>
  
  <pre><code>package Child1; 
  
  use base Parent1;
  
  sub new {
      my ($class, $arg_hash) = @_;
  
      my $self = bless $arg_hash, $class; 
  
      return $self;
  }
  
  sub testFunctionReferences{
  
      my ($self) = @_;
  
      # Case 1: Below 2 lines of code doesn't work and produces error message "Not a CODE reference at Child1.pm line 18."
      #my $parent_hello_reference = \&amp;$self-&gt;SUPER::printHello;
      #&amp;$parent_hello_reference();
  
      # Case 2: Out of below 2 lines of code, 1st line executes the function and produces output of "Hello\n" but 2nd line doesn't work and produces error message "Not a CODE reference at Child1.pm line 23."
      #my $parent_hello_reference2 = \$self-&gt;SUPER::printHello;
      #&amp;$parent_hello_reference2();
  
      # Case 3: does not work either. Says "Undefined subroutine &amp;Child1::printNasty called at Child1.pm line 27"
      #my $parent_nasty_reference = \&amp;printNasty;
      #&amp;$parent_nasty_reference();
  
      # Case 4: works. prints "World\n" as expected  
      #my $my_own_function_reference = \&amp;printWorld;
      #&amp;$my_own_function_reference();
  
      # Case 5: works. prints "Hello\n" and  "Nasty\n" as expected
      #$self-&gt;printHello();
      #$self-&gt;SUPER::printNasty();
  
      # Case 6: does not work produces error "Undefined subroutine &amp;Child1::printHello called at Child1.pm line 38" 
      #printHello();
      return;
  }
  
  sub printWorld{
      print "World\n";
  }   
  </code></pre>
  
  <h1>test.pl</h1>
  
  <pre><code>#!/usr/bin/perl
  
  use Child1;
  
  my $child = Child1-&gt;new({});
  
  $child-&gt;testFunctionReferences();
  </code></pre>
  
  <p>So my questions are:</p>
  
  <ol>
  <li><p>As in case 1, what is the correct syntax to get a reference to parent subroutine? </p></li>
  <li><p>When I use inheritance, how can I call the parent function directly as in case 6? Is it even possible in perl? </p></li>
  <li><p>When case 5 works then why not case 6?</p></li>
  </ol>
  
  <p>Any insights are appreciated. Thanks</p>