Beefy Boxes and Bandwidth Generously Provided by pair Networks
laziness, impatience, and hubris
 
PerlMonks  

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

by bellaire (Hermit)
on Nov 20, 2009 at 13:56 UTC ( #808438=note: print w/replies, xml ) Need Help??


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

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. :)

Replies are listed 'Best First'.
Re^2: Append to file or create file depending on input filename.
by moritz (Cardinal) on Nov 20, 2009 at 14:14 UTC
    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!
Re^2: Append to file or create file depending on input filename.
by graff (Chancellor) on Nov 20, 2009 at 23:24 UTC
    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.

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others drinking their drinks and smoking their pipes about the Monastery: (5)
As of 2019-10-23 06:06 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    Notices?