atemon has asked for the wisdom of the Perl Monks concerning the following question:
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
My perl script looked likepackage 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"; }
Output :#!/usr/bin/perl use tests; my $obj = new tests(); $obj->myfunction_one(); $obj->myfunction_two();
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 Outside 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.This is myfunction_one This is myfunction_two Inside This is myfunction_two Outside
Cheers !
--VC
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Scope of subroutines with same name
by Joost (Canon) on Feb 05, 2009 at 23:42 UTC | |
|
Re: Scope of subroutines with same name
by ikegami (Patriarch) on Feb 05, 2009 at 23:40 UTC | |
|
Re: Scope of subroutines with same name
by almut (Canon) on Feb 05, 2009 at 23:41 UTC | |
|
Re: Scope of subroutines with same name
by Lawliet (Curate) on Feb 05, 2009 at 23:42 UTC |