in reply to Safe Temp Files

The advantage of File::Temp is that you can use it if you need the name of the temp file. The advantage of open is that its syntax is simpler.

Replies are listed 'Best First'.
Re^2: Safe Temp Files
by xdg (Monsignor) on Dec 04, 2006 at 17:57 UTC
    advantage of open is that its syntax is simpler

    I'm not convinced of that. Apart from the "use" line, the File::Temp approach is about the same length and the intent is more immediately obvious.

    open(my $fh, "+>", undef) or die ...
    use File::Temp (); my $fh = File::Temp->new or die ...

    -xdg

    Code written by xdg and posted on PerlMonks is public domain. It is provided as is with no warranties, express or implied, of any kind. Posted code may not have been tested. Use of posted code is at your own risk.

      the File::Temp approach is about the same length and the intent is more immediately obvious

      I disagree on the latter. new is the worst name for a method, for a module like this, so a line like this:

      my $fh = File::Temp->new
      is always enough to make me go "WTF...?".

      I wish the module author had put some more inspiration in the name for the constructor, like Tim Bunce used "connect" in DBI instead of the plain, boring and somewhat obscure "new". I like this a lot better.

        I guess I'm used to the convention of using "new" as a constructor and just mentally parse it backwards as "a new Temp File".

        In the Pod, the author writes it in the indirect notation:

        my $fh = new File::Temp;

        -xdg

        Code written by xdg and posted on PerlMonks is public domain. It is provided as is with no warranties, express or implied, of any kind. Posted code may not have been tested. Use of posted code is at your own risk.