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



On Apr 26, 2:28 pm, king <hara.acha...@xxxxxxxxx> wrote:
I have a module named TPWizardMgr.pm as below.

sub load_school_template
{
my $self = shift;
#...
my @template_subject_period;
# Put lots of stuff in @template_subject_period;

}

sub get_course_info
{
my $self = shift;
# Stuff that needs @template_subject_period calculated above
}

1;
==================================================================

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.

No because it's lexically scoped to the other subroutine.

If you want data that persists from one method call to another you
store it _in_the_object_.



How I can do this.


sub load_school_template
{
my $self = shift;
#...
# Put lots of stuff in @{$self->{template_subject_period}};
}

sub get_course_info
{
my $self = shift;
# Stuff that uses @{$self->{template_subject_period}};
}


Now I've answered the question you asked I'll compose another follow-
up that addresses some other issues with your code.

.



Relevant Pages

  • Re: passing database data to a sub
    ... > I'm not sure of the difference, why isn't it a subroutine? ... > sure about this 'shift' thing anyway :-) ... > sub teardown ... > # Setup the template to use for the output. ...
    (perl.beginners)
  • Re: Replacing a line
    ... #Using core module Tie::File to process a file in this subroutine ... sub process_one_file { ... $cpp_file = shift; ... for (@array) #Each line should come one by one ...
    (comp.lang.perl.misc)
  • subroutine in LWP - in order to get 700 forum threads
    ... sub get_threads { ... my $page = shift; ... push @links, values %attr; ... This must get a subroutine - doesn t it? ...
    (perl.beginners)
  • RE: passing database data to a sub
    ... Second, GetOfficersis called as a subroutine, not as a method of an object. ... I not completely sure about this 'shift' thing anyway :-) ... sub teardown ... # Note...this subroutine uses the template 'db_mainmenu.tmpl.htm' to give ...
    (perl.beginners)
  • Re: Memory problem with XML::DOM::Parser???
    ... >> # Die Anfrage ist Teil der neuen EPA ... # Modul für die XML-Funktionen des Clients ... # Subroutine, um die XML-Struktur aus dem XML-Rootfile und den ... sub construct_xml { ...
    (comp.lang.perl.misc)