Beefy Boxes and Bandwidth Generously Provided by pair Networks
Pathologically Eclectic Rubbish Lister

Append to file or create file depending on input filename.

by markuhs (Scribe)
on Nov 20, 2009 at 13:13 UTC ( #808423=perlquestion: print w/replies, xml ) Need Help??

markuhs has asked for the wisdom of the Perl Monks concerning the following question:

Hi Monks,

I learned about three-way open and wanted to use it. Everything seemed OK. Then I encountered problems with one particular script. It opens a file and writes to it. Normally no problem. The usage, however, caused some problems.

The script is used in the following two ways:
C:\>perl outfile C:\>perl ">outfile"
The second is used to append the data produced by the script to the already existing data.

Hmm... Three-way open brought me from open $filehandle ">$outfile" to open $filehandle ">", $outfile" which causes the trick not to work anymore.

First take on it:
my $openmode = '>'; if($outfile =~ m/\A>/xms) { $openmode = '>>'; $outfile =~ s/\A>//xms; } open($filehandle, $openmode, $outfile);
Any suggestions how to do this more elegant?

UPDATE: as per comment of bellaire (>outfile changed to ">outfile").

Replies are listed 'Best First'.
Re: Append to file or create file depending on input filename.
by bellaire (Hermit) on Nov 20, 2009 at 13:56 UTC
    Elegance is in the eye of the beholder of course, but how about:
    open($filehandle, (($outfile =~ s/\A>//xms) ? '>>' : '>'), $outfile);
    That is, if the substitution succeeds, it should return a true value (the number of things replaced, at least 1), and you get '>>' for your mode at the same time that you fix your outfile namae. Otherwise you get '>' for your mode. I wasn't sure about the evaluation happening in the right order, but I tested it and it seems to work. :)
      You can also write the substitution a bit simpler:
      $outfile =~ s/^>//

      I know that PBP encourages you to always use xms, and \A instead of ^, but that doesn't mean it's the most elegant or simple way to write it.

        As this problem was caused by applying things learned from PBP I surely will use the code as provided by bellaire. ;)

        ++bellaire! Thank you!
      I wasn't sure about the evaluation happening in the right order, but I tested it and it seems to work. :)

      Well, I would be inclined to worry about that too, and unless I find the bit of perl documentation that actually spells out the "official policy" about evaluation order for arguments in a subroutine call, I would consider a successful run on my own machine to be no guarantee that it'll work on other machines, or on my machine after I upgrade to the next Perl release.

      And rather than take the time to hunt down that (possibly obscure) snippet of documentation, it would be easier for me just to add one more line of code and one more variable, so I don't have to worry about it.

        This is a good point. I don't know how likely this behavior is to change in the future or between platforms, only that the rough rule of thumb is that evaluation happens left-to-right. However, as this mailing-list thread shows, that's a rule subject to many exceptions.

        And, as you might have suspected, the discussion there seems to indicate that the order of evaluation is not well-defined, so perl simply does what it does.

Re: Append to file or create file depending on input filename.
by bellaire (Hermit) on Nov 20, 2009 at 13:36 UTC
    When you use a shell command like:
    C:\>perl >outfile
    The argument >outfile never reaches the Perl script. The shell interprets it as you asking to execute perl and send its output to outfile. As far as Perl knows, there are no command-line arguments to If you want to optionally use the filename to change your output behavior, you'll have to do so without using the > character as the first character of your argument.

      I was wondering the same.

      To (ab)use 2-arg open in the way the OP describes, he has to do some trickery like:

      C:\>perl ">outfile" C:\>rem Or C:\>perl ^>outfile

      which will effectively hide the output redirect from the cmd and result in an $ARGV[0] containing >outfile.

      Correct, the usage was wrong. I missed out the double quotes...
      Thanks for pointing that out!
Re: Append to file or create file depending on input filename.
by ww (Archbishop) on Nov 20, 2009 at 13:27 UTC
    Rather than relying on your OS, try Perl's native open:

    From perldoc -f open:


    and, from paragraph 5:

    .... If MODE is '>', the file is truncated and opened for output, being created if necessary. If MODE is '>>', the file is opened for appending, again being created if necessary.

    Clarification of para 1

Re: Append to file or create file depending on input filename.
by JadeNB (Chaplain) on Nov 20, 2009 at 14:14 UTC
    In a similar vein to bellaire's reply, how about
    open $filehandle, (@ARGV == 1 ? '>' : ()), @ARGV;
    ? This lets the user specify the mode on the command line just as you would in the program, using another argument.
Re: Append to file or create file depending on input filename.
by graff (Chancellor) on Nov 20, 2009 at 22:59 UTC
    Sheesh. Why not just make the script usage be as UNIX intended it:
    perl > outfile # create new or truncate existing outfile perl >> outfile # create new or append to existing outfil +e
    The perl script doesn't have to bother opening a specific output file at all; it just prints. (STDOUT is the default output file handle.) Plus, no more worries over forgetting to include quotes on the command line -- just let the shell do what it's supposed to do.

    That also allows you to use the script in yet another handy way (and the last time I checked, the "standard" windows cmd.exe handles all three modes the same way unix and linux do - it's portable!):

    perl | some_other_process ...
    That's what we call elegant.
      Thanks, ++graff!
      Sometimes, user-friendliness or rules from above(/before you were there) do not allow you to do what you want. I need to have the output file as option on the command line. Most users do not want to bother with shell stuff and some are very inexperienced... -> My achievable elegance is drastically limited/controlled... ;-(
        Well, if a primary objective is to protect simple-minded users from the complexities of the shell, you really should avoid using angle brackets on the command line altogether.

        To put that another way, if users don't want to be bothered with the standard usage of angle brackets, things will go much better for all concerned if you simply forbid any use of angle brackets.

        Why? Because when you forget to use quotes in OP approach to appending, you can get in bad trouble. REALLY Bad Trouble. As in, deleting the very data you were supposed to preserve by appending.

        So do it like this:

        Usage: script_name [-a] output_file_name by default, output_file is created as a new file use "-a" to have output appended to existing output_file
        That is, use "-a" instead of an angle bracket. Or better yet (assuming that it's more important to avoid deleting existing data), make appending the default mode of operation, and have an option "-n" (for "new") or "-b" (for "begin" or "blank-slate") or whatever, to create/truncate the output file.

        Rest assured, the OP design is the farthest thing from "user friendly". You have to do better than that.

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://808423]
Approved by wfsp
Front-paged by wfsp
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others examining the Monastery: (2)
As of 2021-06-21 05:06 GMT
Find Nodes?
    Voting Booth?
    What does the "s" stand for in "perls"? (Whence perls)

    Results (98 votes). Check out past polls.