http://www.perlmonks.org?node_id=808781


in reply to Re: Append to file or create file depending on input filename.
in thread Append to file or create file depending on input filename.

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... ;-(
  • Comment on Re^2: Append to file or create file depending on input filename.

Replies are listed 'Best First'.
Re^3: Append to file or create file depending on input filename.
by graff (Chancellor) on Nov 24, 2009 at 04:22 UTC
    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.