file - Filehandle Quirk Perl -


in following code if there space between file , ( in printf statement like

printf file ("test string inline\n"); 

perl treat file filehandle otherwise

printf file("test string inline\n"); 

will treated subroutine call(if no subroutine defined file perl through error undefined subroutine &main::file called @ ./test.pl line xx ). isn't there better way perl can implement ? (maybe why bareword filehandles considered outdated ?)

#!/usr/bin/perl  use warnings;  open(file,">test.txt"); printf file ("test string inline\n"); close(file);   sub file {     return("test string subroutine\n"); } 

are asking how avoid error accidentally? wrap handle in curlies

printf({ handle } $pattern, @args); print({ handle } @args); say({ handle } @args); 

or since parens omitted say, print , printf,

printf { handle } $pattern, @args; print { handle } @args; { handle } @args; 

or use method call

handle->printf($pattern, @args); handle->print(@args); handle->say(@args); 

Comments