Re: [PATCH] perlipc.pod Revamp

[email protected] (Shlomi Fish) Mon, 8 Nov 2010 16:14:14 +0200
Newsgroups perl.perl5.porters,perl.documentation
Message-ID <[email protected]>
On Monday 08 November 2010 15:45:02 demerphq wrote:
> On 7 November 2010 20:23, Shlomi Fish <[email protected]> wrote:
> >> >     } else {     # child
> >> > 
> >> >         ($EUID, $EGID) = ($UID, $GID); # suid progs only
> >> >
> >> >-        open (FILE, "> /safe/file")
> >> >-            || die "can't open /safe/file: $!";
> >> >+        open (my $file, ">", "/safe/file")
> >> 
> >> Why?  There is 100.00000000000% chance that that will behave.
> >> It's a constant string of known content.
> > 
> > Still, we should not encourage such potentially bad idioms. Here it is
> > harmless, but people can learn that it's OK to do ">$myfile" too.
> 
> If the intent of these changes is to demonstrate a better style, then
> I would expect the filename to be stored in a var so that it isn't
> duplicated in the error message.

Good point.

> 
> Also, im not sure that i agree that changing open(FILE, ... ) to
> open(my $file, ...) is very sensible, in that to me it is not clear
> what "$file" is (in my coding style such a var would almost always be
> a file /name/), whereas FILE is very clearly a handle, accordingly I
> would tend to use var names like $out_fh for a write handle and $in_fh
> for a read handle, in particular I almost always suffix the var with
> '_fh' or something to make it clear that it is not the name of a file
> but a handle to one.

Another good point. On this web page, I (not Damian) recommend against calling 
variables "$file" due to ambiguity between file handle and file name:

http://perl-begin.org/tutorials/bad-elements/#calling-variables-file

As a result, I changed it to this code:

        my $safe_filename = "/safe/file";
        open (my $safe_fh, ">", $safe_filename)
            or die "can't open ${safe_filename}: $!";
        while (<STDIN>) {
            print {$safe_fh} $_; # child's STDIN is parent's $kid_to_write
        }
        exit;  # don't forget this

These changes can be found here:

http://github.com/shlomif/perl/tree/perlipc-revamp

Regards,

	Shlomi Fish

> 
> IOW, (respecting Tom's style expectations):
> 
> my $safe_file = "/safe/file";
> open(my $safe_fh, ">", $safe_file) || die "Failed to open '$safe_file'
> for writing: $!";
> 
> Cheers,
> yves

-- 
-----------------------------------------------------------------
Shlomi Fish       http://www.shlomifish.org/
"The Human Hacking Field Guide" - http://shlom.in/hhfg

<rindolf> She's a hot chick. But she smokes.
<go|dfish> She can smoke as long as she's smokin'.

Please reply to list if it's a mailing list post - http://shlom.in/reply .