0

I am using CGI in strict mode and a bit confused with variables. I am reading a file that has two lines. Storing both in two variables. But when i try outputing them using html, it says global variable error

This is what I am doing

 open TEXT, "filename";
 $title = <TEXT>;
 $about = <TEXT>; 
 close TEXT;

but this gives the global variable error. whats the best way to fix this?

4

1 回答 1

4

您需要声明变量my以使其范围为本地。这是使用时的最佳实践和强制性strict

use strict;
use warnings;

open my $fh, '<', 'filename' or die $!;
my ( $title, $about ) = <$fh>;
close $fh;

进一步的改进:

  1. 避免了裸字文件句柄(例如FILE)。而是使用本地文件句柄,例如my $fh
  2. die处理文件处理时使用的错误处理
  3. @Suic建议的$title和的组合分配$about
  4. use warnings显示@TLP指出的问题
于 2013-11-02T20:13:52.100 回答