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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T14:38:34+00:00 2026-06-18T14:38:34+00:00

I want my code to escape from a recursive subroutine after a specific iterations

  • 0

I want my code to escape from a recursive subroutine after a specific iterations

This is my my test code:

#!/usr/bin/perl 
use strict;

&sub();

my $count = 0;
sub sub
{
    if ( $count lt 1 )
    {
        print "This statment is visible \n";
        $count++;
        &sub();
    }
    else
    {
        return;
    }
}

THis gives below output and I am happy with that:

This statment is visible 

But If I use the #!/usr/bin/perl -w, then it gives me hte output but also gives me a warning:

Use of uninitialized value $count in string lt at sub.pl line 9.
This statment is visible

Quistion 1: How to get rid of above warning.

Now, Above code was my test code and I want to apply the same logic to my actual code below:

In short, I want to resolve a hostname to its IP address. If it doesn’t work, then i want to append the domain name (.my.company.net) to the hostname and then try the name resoltuion.

However, my above code (in some cases, don’t know why) goes into infinite deep recursion and eats up the whole memory. My VM becomes unresponsive and I have to reboot it to get it back working.

Hence, I want to break free from the recursive sub after one iteration rather than doing infinite recursion.

Here is the code:

#!/usr/bin/perl -w
use strict;

my $IP = to_ip("abcdefgh");
print $IP."\n";

my $count = 0;
sub to_ip
{
    if ( $count lt 1 )
    {
        use Socket;
        my $hostname    = shift;
        my $packed_ip   = gethostbyname($hostname);
        if ( defined $packed_ip )
        {
            sprintf inet_ntoa($packed_ip);
        }
        else
        {
            $hostname .= ".my.company.net";
            to_ip($hostname);
        }
        $count++;
    }
    else
    {
        return;
    }
}

Aobve code gives me below warning and then again get stuck. I have to reboot hte VM to get it back on.

Useless use of sprintf in void context at get_deleted_list.pl line 98.

please ignore the line number in above warning. it is referring to the sprintf in my sub

Question 2: How to break free from my sub after one iteration. I dont care whether I get the result from the sub but I want to come out of it after one iteration.

Thanks.

  • 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-18T14:38:36+00:00Added an answer on June 18, 2026 at 2:38 pm

    I think a simple loop-based solution would be more straightforward:

    sub to_ip
    {
      use Socket;
      my $hostname = shift;
      for my $suffix ("", ".my.company.net") {
        my $packed_ip = gethostbyname("$hostname$suffix");
        next unless defined $packed_ip;
        return sprintf inet_ntoa($packed_ip);
      }
     }
    

    This simply tries all the alternatives in order until one succeeds, at which point it returns the desired string. (BTW, is sprintf really necessary here?). If you fall out of the loop without a successful call to gethostbyname, you return undef implicitly.

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

Sidebar

Related Questions

I have some code: $(xml).find(strengths).each(function() { //Code //How can i escape from this block
I want to remove manual escaping from my code. Instead of this I want
I want to code this part of a VB6 application in c#. How can
i just want to know from below code what does ? and : specify,
EDIT 1 I am getting courseID from this code: $coursesOutput = '<option value=></option>'; while($row
I want to read and save some data from this rss feed to in
I got this code from a website and I am trying to tailor it
i have this code which is not seeming to work, what i want to
I want to delete specific Markers from my database, I can save than with
I currently have this code: $rawsql2 = SELECT * FROM _erc_foffices n INNER JOIN

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.