title: "Variable $foo will not stay shared" Warning/Error in Perl While Calling Subroutine
body: <p><strong><code>Update3:</code></strong> If you like this posting please don't upvote me but upvote the genius answer by DVK below.</p>
  
  <p>I have the following subroutines:</p>
  
  <pre><code> use warnings;
  #Input
   my @pairs = (
      "fred bill",
      "hello bye",
      "hello fred",
      "foo bar",
      "fred foo");
  
  #calling the subroutine
  my @ccomp = connected_component(@pairs);
  
  use Data::Dumper;
  print Dumper \@ccomp;
  
  sub connected_component {
  
      my  @arr    = @_;
      my %links;
  
      foreach my $arrm (  @arr ) {
          my ($x,$y) = split(/\s+/,$arrm);;
          $links{$x}{$y} = $links{$y}{$x} = 1;
  
      }
  
      my %marked;  # nodes we have already visited
      my @stack;
  
      my @all_ccomp;
  
      for my $node (sort keys %links) {
          next if exists $marked{$node};
          @stack = ();
          connected($node);
          print "@stack\n";
          push @all_ccomp, [@stack];
      }
  
      sub connected {
          no warnings 'recursion';
          my $node = shift;
          return if exists $marked{$node};  # Line 43
          $marked{$node} = 1;
          push @stack, $node;   # Line 45
          my $children = $links{$node};  # Line 46
          connected($_) for keys %$children;
      }
  
  
      return @all_ccomp;
  }
  </code></pre>
  
  <p>But why it gives this message:</p>
  
  <pre><code>Variable "%marked" will not stay shared at mycode.pl line 43.
  Variable "@stack" will not stay shared at mycode.pl line 45.
  Variable "%links" will not stay shared at mycode.pl line 46.
  </code></pre>
  
  <p>Is it harmful? Error? How can fix my code so that it get rid of that message?</p>
  
  <p><strong>Update1:</strong> I update the code that runs as is with the actuall error message</p>
  
  <p><strong>Update2:</strong> I tried to modify using sub as DVK suggested. And it WORKED!</p>
  
  <pre><code>use warnings;
  #Input
   my @pairs = (
      "fred bill",
      "hello bye",
      "hello fred",
      "foo bar",
      "fred foo");
  
  #calling the subroutine
  my @ccomp = connected_component(@pairs);
  
  use Data::Dumper;
  print Dumper \@ccomp;
  
  sub connected_component {
  
      my  @arr    = @_;
      my %links;
  
      foreach my $arrm (  @arr ) {
          my ($x,$y) = split(/\s+/,$arrm);;
          $links{$x}{$y} = $links{$y}{$x} = 1;
  
      }
  
      my %marked;  # nodes we have already visited
      my @stack;
  
      my @all_ccomp;
  
      my $connected_sub;
       $connected_sub = sub {
          no warnings 'recursion';
          my $node = shift;
          return if exists $marked{$node};  
          $marked{$node} = 1;
          push @stack, $node;  
          my $children = $links{$node};  
          &amp;$connected_sub($_) for keys %$children;
      };
  
      for my $node (sort keys %links) { # Line 43
          next if exists $marked{$node};
          @stack = ();
          &amp;$connected_sub($node);
          #print "@stack\n";
          push @all_ccomp, [@stack]; # Line 49
      }
  
      return @all_ccomp;
  }
  </code></pre>