It's good for the beginners
-----Original Message-----
From: John W. Krahn [mailto:[email protected]]
Sent: Sunday, April 05, 2009 9:24 PM
To: Perl Beginners
Subject: Re: Using array from subroutine
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.
> $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: $!"
}
John
--
Those people who think they know everything are a great
annoyance to those of us who do. -- Isaac Asimov
--
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]
http://learn.perl.org/
--
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]
http://learn.perl.org/