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 7985195
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T11:29:30+00:00 2026-06-04T11:29:30+00:00

If I have the following code sub a { my $id = shift; #

  • 0

If I have the following code

sub a {
    my $id = shift;
    # does something
    print &a_section($texta);
    print &a_section($textb);
    sub a_section {
        my $text = shift;
        # combines the $id and the $text to create and return some result.
    }
}

Assuming a_section is called only by a, will I run into a memory leak, variable dependability, or other problem?

I am exploring this as an alternative so I can avoid the necessity of passing $id to a_section.

  • 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-06-04T11:29:32+00:00Added an answer on June 4, 2026 at 11:29 am

    First, it’s not a private sub. It’s fully visible from the outside. Two, you will have problems.

    $ perl -wE'
       sub outer {
          my ($x) = @_;
          sub inner { say $x; }
          inner();
       }
       outer(123);
       outer(456);
    '
    Variable "$x" will not stay shared at -e line 4.
    123
    123     <--- XXX Not 456!!!!
    

    You could do:

    sub a {
        my $id = shift;
    
        local *a_section = sub {
            my $text = shift;
            # combines the $id and the $text to create and return some result.
        };
    
        print a_section($texta);
        print a_section($textb);
    }
    

    (You can call the inner sub recursively using a_section(...).)

    or:

    sub a {
        my $id = shift;
    
        my $a_section = sub {
            my $text = shift;
            # combines the $id and the $text to create and return some result.
        };
    
        print $a_section->($texta);
        print $a_section->($textb);
    }
    

    (Use __SUB__->(...) if you want to call the inner sub recursively to avoid memory leak, available in Perl 5.16+.)

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

Sidebar

Related Questions

I have the following code to create my window, my view and my sub
I have the following code: ... sub setImage { my $self=shift; my $filename=shift; unless(-r
I have following code snipped in _Layout.cshtml <div id=sub-navig-container> @RenderSection(subNavig) </div> <div id=text-content> @RenderBody()
I have the following code : sub run_query { my $name = shift ||
I have an agent with the following code: Sub Initialize MessageBox AgentStart Print AgentStart
I have the following code: Private Sub SortWorksheet(ByVal sheet As Worksheet) Dim sStartColumn Dim
i have the following code Private Sub select_color_Click(ByVal sender As System.Object, ByVal e As
I have the following code: Imports MySql.Data.MySqlClient Imports MySql.Data.Types Public Class FormMain Private Sub
Okay so i have a semi weridish problem with re.sub. Take the following code:
I have code like the following in a UserControl: Protected Overrides Sub Render(ByVal writer

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.