Beefy Boxes and Bandwidth Generously Provided by pair Networks
more useful options
 
PerlMonks  

chmod in perl

by Anonymous Monk
on Mar 11, 2011 at 04:26 UTC ( #892582=perlquestion: print w/ replies, xml ) Need Help??
Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

I am opening a file for writing,but if the if the file already exists in read-only mode,am getting a "permission denied" error,I tried to add below,it didnt work,any inputs?

chmod 0777 deletefiles.txt; open(my $OUTPUT, '+>', "deletefiles.txt") or die $!;

Comment on chmod in perl
Download Code
Re: chmod in perl
by ikegami (Pope) on Mar 11, 2011 at 04:33 UTC
Re: chmod in perl
by atcroft (Monsignor) on Mar 11, 2011 at 04:33 UTC

    Have you checked the return status of the chmod command? For instance,

    my $filename = q{deletefiles.txt}; chmod 0777, $filename or die $!; open my $OUTPUT, q{+>}, $filename or die $!;

    Hope that helps.

    Update: 2011-03-11

    Fixed missing semicolon on line 2 of code example causing error referred to in Re^2: chmod in perl.

      I am getting the following error with the above code

      my $filename = q{deletefiles.txt}; chmod 0777, $filename or die $! open my $OUTPUT, q{+>}, $filename or die $!; Missing comma after first argument to die function at perl.pl line 135 +, near "$filename or" Execution of qdepot_upload.pl aborted due to compilation errors.
        You miscopied the code.
        Try and put the semicolon after first die statement. Because of that its throwing that error because it's gonna continue till the end of file if you dont specify where to exactly end.
Re: chmod in perl
by graff (Chancellor) on Mar 11, 2011 at 05:34 UTC
    If you happen to be on a unix/linux system (which is pretty much the only place where chmod makes sense), you should be aware that chmod will fail when used on a file that is not owned by the user who is running the script. (update: unless of course the user happens to be "root" -- in which case the user really ought to know what s/he is doing...)

    As mentioned above, checking the return value (and printing the value of $! when chmod returns false) will help you figure out what is going on.

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others browsing the Monastery: (16)
As of 2014-10-02 15:02 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    What is your favourite meta-syntactic variable name?














    Results (63 votes), past polls