in reply to Re: File open problem with "GLOB"
in thread File open problem with "GLOB"

No need for the curlies around $output in the print statement.

Replies are listed 'Best First'.
Re^3: File open problem with "GLOB"
by chromatic (Archbishop) on Mar 16, 2008 at 01:05 UTC

    They disambiguate the one use of the horrid indirect object syntax that you can't easily avoid in Perl 5. (I know about IO::Handle, but it's even further away from the principle of least surprise.)

      Yes, but there's no need to disambiguate with print when the handle is nothing more than a bareword or a simple scalar. For the very common use of print HANDLE LIST and print $fh LIST, there's no need for curlies. Given how much Perl programmers are familiar with the lack of curlies in those instances, the curlies do nothing to promote readability, maintainability or simplicity.

        Sure, and there's no need to use strict or warnings if you don't ever make any typos.

        I put the curlies around the filehandle even in the simple case, because once I get out of the habit, I'll spend way too long debugging a filehandle in a complex expression that, for some baffling reason, refuses to work... and I know why the Perl 5 parser has a problem here. If that trips me up, I suspect it may also trip up the fair few Perl 5 programmers who haven't read the Perl 5 source code.

Re^3: File open problem with "GLOB"
by Narveson (Chaplain) on Mar 16, 2008 at 01:59 UTC

    theDamian, Perl Best Practices, Chapter 10: IO

    Printing to Filehandles

    Always put filehandles in braces within any print statement.

      Just because Damian Conway says it, that does not mean it's helpful or true. He has released a slew of really crap modules just for the book like Getopt::Euclid, Class::Std and IO::Prompt. The book recommends their use, even though they are buggy and unmaintained. By your "argumentation" we should be using them.

      If you want to argue for a guideline, and I believe Damian Conway says this in his book as well, you need to discuss the benefits over its drawbacks. Like blocks and using braces around filehandles with print does. Simply citing verse and chapter of a book that has many flaws does not help your argument.

        If you want to argue for a guideline, and I believe Damian Conway says this in his book as well, you need to discuss the benefits over its drawbacks. [...] Simply citing verse and chapter of a book that has many flaws does not help your argument.

        If a monk has no original thought beyond what's written in a book somewhere, I think I'd prefer a reference over a paste or paraphrase.

        Update: Upon further reflection, I guess I prefer the reference mainly in the case where I've already read the book. Unfortunately, the poster can't always know which books I've read.

Re^3: File open problem with "GLOB"
by Narveson (Chaplain) on Mar 16, 2008 at 22:30 UTC

    Dear brethren, I had the good fortune yesterday to spot an unanswered Seeker query to which I knew the answer. As I wrote my reply, I strove to be helpful, correct, but above all quick.

    There are details I might have done differently. The querent said

    '>C:\Program Files\cron\Cruise Ships\ship_data.csv'
    and when I converted to three-arg open I could have left the filename at
    'C:\Program Files\cron\Cruise Ships\ship_data.csv'
    or converted to
    'C:/Program Files/cron/Cruise Ships/ship_data.csv'
    because (as all Windows Perl programmers should be told) forward slashes work just fine for telling Windows where to go, and can never be mistakenly parsed as escapes, and I'd rather not spend any time at all thinking about when a backslash is an escape and when it isn't.

    I could also have changed $output to $output_fh and $input to $input_data, but chose instead to retain the querent's own variable names.

    Others would certainly have made different choices. I'm still surprised that the particular nit that was picked was the pair of braces around the filehandle in

    print {$output} $input;