Beefy Boxes and Bandwidth Generously Provided by pair Networks
"be consistent"

Re^3: invoking umask command in perl

by ig (Vicar)
on Jun 01, 2011 at 20:30 UTC ( #907707=note: print w/replies, xml ) Need Help??

in reply to Re^2: invoking umask command in perl
in thread invoking umask command in perl

The 'umask' function in your shell is a built-in function. It is executed by the shell itself and changes the setting of the shell itself. If it were a separate executable, then it would run in a sub-process of the shell and would affect itself but not the shell it was invoked from.

If you write a Perl script and run it, it runs in a sub-process of your shell and cannot easily change the settings of your shell.

If you want a Perl script that changes the setting of the shell you invoke it from, you have a challenge. It might be possible, if you have permissions to attach to and control that other process. For example, gdb can attach to a process and modify it in many ways. But I don't think you will find anything in Perl and possibly not in CPAN to do this.

If you want a Perl script that changes the setting for itself and any processes it spawns, that's easy: use the umask function as Corion has suggested.

Here is an example that shows how to invoke the bash 'umask' function from a Perl script, and how to set the umask of the perl script.

use strict; use warnings; umask 0037; system('bash -c umask');

If you run this and you have bash on your system you will see that the umask function in the bash process displays the umask you set in the script. If, after running this script you check the umask of your login shell, from which you invoked the Perl script (I'm making some assumptions here - let me know if they're not valid for you) then you will see that the umask of your login shell is not changed. This is expected. The general rule is that child processes do not change the setting of their parent processes.

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://907707]
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others chanting in the Monastery: (8)
As of 2018-05-28 08:17 GMT
Find Nodes?
    Voting Booth?