On Sun, Apr 05, 2009 at 08:53:43AM -0700, John W. Krahn wrote:
> Thomas H. George wrote:
>> In order to call them from menubuttons I moved my code into subroutines.
>>
>> sub open_file {
>>      if ( ! open NEWFILE, "<$file") {
>>              die "Could Not Open $file: $!"
>>      } else {
>>              my @lines = <NEWFILE>;
>
> my() *creates* a new variable that is only visible from this point until  
> the } two lines down.

I knew better than this, just typed 'my' in the posting, not in the
program.
>
>
>>              $message = "Open File" . $file;
>>      }
>> } # End sub open_file
>>
>> sub edit_file {
>>      $lbox -> insert ('end', @lines);
>>      ...
>>
>> The array @lines was declared as
>>
>> use Tk;
>> use strict;
>> my @lines = ();
>>
>> at the top of the program and $lbox is a Listbox. The code worked
>> perfectly in the main program but when moved into subroutines the array
>> @lines is an empty array when the second subroutine is called. 
>>
>> How should this be coded?
>
> use warnings;
> use strict;
> use Tk;
> my @lines = open_file();
>
>
> sub open_file {
>       if ( open my $NEWFILE, '<', $file ) {
>               $message = "Open File" . $file;
>               return <$NEWFILE>;
>       }
>       die "Could Not Open $file: $!"
> }
>
return did the trick.  I had experimented with it but got it wrong.
Your code cleared up my misunderstanding.

Thanks,

Tom
>
>
>

-- 
To unsubscribe, e-mail: beginners-unsubscr...@perl.org
For additional commands, e-mail: beginners-h...@perl.org
http://learn.perl.org/


Reply via email to