Beefy Boxes and Bandwidth Generously Provided by pair Networks
No such thing as a small change
 
PerlMonks  

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

by graff (Chancellor)
on Nov 20, 2009 at 22:59 UTC ( [id://808544]=note: print w/replies, xml ) Need Help??


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

Sheesh. Why not just make the script usage be as UNIX intended it:
perl script.pl > outfile # create new or truncate existing outfile perl script.pl >> 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 script.pl | some_other_process ...
That's what we call elegant.

Replies are listed 'Best First'.
Re^2: Append to file or create file depending on input filename.
by markuhs (Scribe) on Nov 23, 2009 at 10:46 UTC
    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?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://808544]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others cooling their heels in the Monastery: (6)
As of 2024-04-23 18:51 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found