Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 3330708
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 17, 20262026-05-17T23:30:59+00:00 2026-05-17T23:30:59+00:00

I have a code that works well here: #!/usr/bin/perl use strict; use warnings; use

  • 0

I have a code that works well here:

#!/usr/bin/perl
use strict;
use warnings;
use Data::Dumper;



        my %graph =(
            F => ['B','C','E'],
            A => ['B','C'],
            D => ['B'],
            C => ['A','E','F'],
            E => ['C','F'],
            B => ['A','E','F']
        );

        sub findPaths {
            my( $seen,  $start, $end ) = @_;

            return [[$end]] if $start eq $end;

            $seen->{ $start } = 1;
            my @paths;
            for my $node ( @{ $graph{ $start } } ) {
                my %seen = %{$seen};
                next if exists $seen{ $node };
                push @paths, [ $start, @$_ ] for @{ findPaths( \%seen, $node, $end ) };
            }
            return \@paths;
        }


            my $start = "B";
            my $end   = "E";
        print "@$_\n" for @{ findPaths( {}, $start, $end ) };

What I want to do is to generate a more general subroutine
so that it just take \%graph, $start,$end as input and return final array.

I tried to do it this way but it doesn’t compile.

sub findPathsAll {

    my ($graph,$start,$end) = @_;

    my $findPaths_sub;
        $findPaths_sub {
            my( $seen) = @_;

            return [[$end]] if $start eq $end;

            $seen->{ $start } = 1;
            my @paths;
            for my $node ( @{ $graph{ $start } } ) {
                my %seen = %{$seen};
                next if exists $seen{ $node };
                push @paths, [ $start, @$_ ] for @{ &$findPaths_sub( \%seen, $node, $end ) };
            }
            return \@paths;
        }


    my @all;
    push @all,@$_ for @{ &$findPaths_sub( {}, $start, $end ) };
    return @all;
}

What’s the right way to do it?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-17T23:30:59+00:00Added an answer on May 17, 2026 at 11:30 pm

    I can’t figure out what you want findPathsAll to return, so this may not be quite what you want. Anyway, here’s a translation of findPaths into a lexical recursive coderef:

    use Scalar::Util 'weaken';
    
    sub findPathsAll {
      my ($graph,$start,$end) = @_;
    
      my $findPaths_sub;
      my $strongRef = $findPaths_sub = sub {
        my( $seen, $start, $end ) = @_;
    
        return [[$end]] if $start eq $end;
    
        $seen->{ $start } = 1;
        my @paths;
        for my $node ( @{ $graph->{ $start } } ) {
          my %seen = %{$seen};
          next if exists $seen{ $node };
          push @paths, [ $start, @$_ ]
              for @{ $findPaths_sub->( \%seen, $node, $end ) };
        }
        return \@paths;
      };
    
      weaken($findPaths_sub);       # Prevent memory leak
    
      my @all;
      push @all,@$_ for @{ $findPaths_sub->( {}, $start, $end ) };
      return @all;
    
      ## The above turns all the paths into one big list of nodes.
      ## I think maybe you meant this:
      #    return @{ $findPaths_sub->( {}, $start, $end ) };
      ## which would return a list of arrayrefs, one for each path.
    }
    

    Some notes:

    You declare a coderef like this:

    $var = sub { ... };
    

    Note the assignment operator and the trailing semicolon. If you want the coderef to be recursive, you must have already declared $var. (If you say my $var = sub { ... };, the new variable doesn’t exist until after the sub is created, so it can’t refer to $var.)

    You call it like this:

    $var->(arg1, arg2, ...);
    

    (There are other syntaxes that work, but I think this is the preferred one.)

    There was a subtle memory leak in the first version I posted. Perl uses a reference-count garbage collector, which means it can’t delete self-referential data structures. Since the coderef in $findPaths_sub captured a reference to itself, it would never be cleaned up (until program exit). I now use the weaken function from Scalar::Util (as mentioned by singingfish in his blog entry) to avoid that. $strongRef is used only to keep the coderef from being garbage collected before we’re done with it.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

No related questions found

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.