Re: use one subroutine's variable value in another subroutine inside a module.



On 04/26/2007 08:28 AM, king wrote:
I have a module named TPWizardMgr.pm as below.
[ 50+ lines of code snipped ]

But I want to use the variable @template_subject_period in the
subroutine get_course_info( ).

I am not able to use this @template_subject_period variable value in
the get_course_info subroutine.

How I can do this.

Thanks in advance.


Since @template_subject_period was declared with "my," it is restricted to the scope of get_course_info. The solution is to make the variable a package variable; declare it with "our."

You can access the variable from other packages as @TPWizardMgr::template_subject_period. Since you're creating a class, a more object oriented approach would be to make the variable accessible through a class method, e.g:

sub template_subject_period {
my $class = shift;
our @template_subject_period;
return \@template_subject_period;
}

Since the above method returns an array reference, you would have to be comfortable using array references. If not, just use the other solution above.
.