chmod 0444 on a file handle open for writing works for me. I'm running Linux Debian 2.6.22 (lenny) and Perl 5.10. However, I seem to recall that when I was running code on Debian Etch + 5.8.8 chmod on a file open for writing would fail. That older version of Perl (or of the operating system) didn't let you mark a file as read only when it was open for writing.

As mentioned earlier, chmod on a file handle in Perl only works if your system supports chmod on file handles in general (i.e. it supports fchmod). I'm guessing your system does not support it or else you are using an older perl/os combination. Can you set any permissions on a file handle? Or is the problem limited to making files open for writing read only?

In any case, your best bet is to simply call chmod on the file name rather than the file handle. I can see from your code that you have access to both so this shouldn't be a problem. Your only risk is a small chance that someone could intercept the file and modify it between the time that you close the handle and the time that you set the permissions.

Also, as a side note, I notice you are using the two parameter open. Don't. Use the three parameter open instead. With the two parameter open there is a small chance that Perl with confuse the file name and the open instructions. With the three parameter open, this risk goes away. You may not be able to avoid the security/timing risk, but the risk of a misinterpreted is something you can eliminate entirely.

Update: - struck out words -my recollection was faulty - looking back on my testing notes from a ways back it wasn't chmod on a file handle that failed in Etch+5.8.8 but rather 0444 passed to sysopen - system open would turn 0444 into 0644. I just brought up the Debian Etch system and chmod 0444 on a file handle does work on Etch+5.8.8. Thank-you anonymous monk below for making me double check this.


In reply to Re: Why doesn't chmod work on this lexical filehandle? by ELISHEVA
in thread Why doesn't chmod work on this lexical filehandle? by williff

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.