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 に答える