Why is three-argument open calls with autovivified filehandles a Perl best practice?

后端 未结 3 770
-上瘾入骨i
-上瘾入骨i 2020-11-22 12:32

I\'ve got two questions about the Perl open function:

1) I seem to remember from Perl Best Practices that the 3-argument version of open<

3条回答
  •  陌清茗
    陌清茗 (楼主)
    2020-11-22 12:46

    Tackling #2:

    OUT is a global filehandle and using it exposes you to insidious bugs like this:

    sub doSomething {
      my ($input) = @_;
      # let's compare $input to something we read from another file
      open(F, "<", $anotherFile);
      @F = ; 
      close F;
      &do_some_comparison($input, @F);
    }
    
    open(F, "<", $myfile);
    while () {
        &doSomething($_);   # do'h -- just closed the F filehandle
    }
    close F;
    

提交回复
热议问题