> open(TEXT,"text.txt") or die ("error:  text.txt failed\n");
>       while (<TEXT>) {
>               my $text = <TEXT>;
>       }
> 
> It tells me $text requires explicit package name but I do give it 'my'
> so why is it still giving me that error?  

The $text has fallen out of scope.  Lexical variables (declared with my)
only exist inside the block in which they're declared.  Nobody outside
the block can see them.

What you may want is this:

my $text;
open( TEXT, "text.txt" ) or die;
while ( <TEXT> ) {
    $text = <TEXT>;
}
close TEXT;
print $text;

Now, if you do that, the $text will still be valid by the time you try
to print it, but will contain only the last line of the file.

If you change the while loop to

while ( <TEXT> ) {
    $text .= <TEXT>;
}

now you'll be accumulating everything into $text.

Finally, if all you want to do is slurp the entire file into one
variable, just tell Perl not to stop at the end of each line.

local $/ = undef;
open( TEXT, "text.txt" ) or die;
my $text = <TEXT>;
close( TEXT );
print $text;

-- 
'Andy Lester        [EMAIL PROTECTED]
 Programmer/author  petdance.com
 Daddy              parsley.org/quinn   Jk'=~/.+/s;print((split//,$&)
                            [unpack'C*',"n2]3%+>\"34.'%&.'^%4+!o.'"])

-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]

Reply via email to