変数をファイルのように扱うには
ファイル名の部分にスカラー変数へのリファレンスを渡してやればok。Perl 5.8以降が必要です。
open $handle, '<', \$string; # 読み込み専用 open $handle, '>', \$string; # 書き込み専用 open $handle, '+>', \$string; # 読み書き可能。元の内容は上書き。 open $handle, '+<', \$string; # 読み書き可能。元の内容の後に追加。
普通のファイルハンドルと同じように扱えるので、seek
とかtruncate
とかも使えます。もちろんwhile (<$handle>) { foo(); }
とかも。
Perlクックブック第2版のレシピ8.23を参照。