Hi,
I have a special requirement where I ended up in defining a subroutine with same as another subroutine already existed. I found like one instance is never executed and I fixed by renaming my subroutine. But I wish to know why its behaves like this. If any monks can help me, it will be great.

My package looks like

package tests; sub new{ bless {},shift; } sub myfunction_one{ print "This is myfunction_one\n"; sub myfunction_two{ #I never remember this sub existed print "This is myfunction_two Inside\n"; } &myfunction_two(); } sub myfunction_two{ # This is my sub, but I renamed it to fix the issue print "This is myfunction_two Outside\n"; }
My perl script looked like
#!/usr/bin/perl use tests; my $obj = new tests(); $obj->myfunction_one(); $obj->myfunction_two();
Output :
This is myfunction_one This is myfunction_two Outside This is myfunction_two Outside
I expected the subroutine "myfunction_two" defined inside the sub "myfunction_one" to execute when I call $obj->myfunction_one();. I expected an output like:
This is myfunction_one This is myfunction_two Inside This is myfunction_two Outside
Is there way in which I can have both subroutines with same name and invoke them independently without conflict? I request you to help me in understanding what is happening there. Thank you in advance for the help.

Cheers !

--VC


In reply to Scope of subroutines with same name by atemon

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.