0

次のコードで、 と の間にスペースがFILEある場合(printf

printf FILE ("Test string inline\n");

それ以外の場合、 Perl はFILEファイルハンドルとして扱います。

printf FILE("Test string inline\n");

サブルーチン呼び出しとして扱われます (サブルーチンが FILE で定義されていない場合、perl はエラーを返しますUndefined subroutine &main::FILE called at ./test.pl line xx)。Perl がこれを実装できるより良い方法はありませんか? (おそらくこれが、ベアワード ファイルハンドルが時代遅れと見なされる理由でしょうか?)

#!/usr/bin/perl

use warnings;

open(FILE,">test.txt");
printf FILE ("Test string inline\n");
close(FILE);


sub FILE
{
    return("Test string subroutine\n");
}
4

2 に答える 2

0

試す:

#!/usr/bin/env perl

use strict;
use warnings;

# --------------------------------------

use charnames qw( :full :short );
use English qw( -no_match_vars ) ;  # Avoids regex performance penalty

my $test_file = 'test.txt';

open my $test_fh, '>', $test_file      or die "could not open $test_file: $OS_ERROR\n";
printf {$test_fh} "Test string inline" or die "could not print $test_file: $OS_ERROR\n";
close   $test_fh                       or die "could not close $test_file: $OS_ERROR\n";
于 2013-07-11T18:11:53.923 に答える