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

Re: invoking umask command in perl

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

in reply to invoking umask command in perl

it's not changing the default umask like I expected

What is the result you expect?

If you were doing it manually instead of using perl, what would you do? Have you done it successfully? Or is part of the problem that you don't know how to achieve your objective?

If you answer these questions you may get better guidance.

Replies are listed 'Best First'.
Re^2: invoking umask command in perl
by austin43 (Acolyte) on Jun 01, 2011 at 19:53 UTC
    When I change it to 0037 in a terminal, I type:
    umask 0037
    This works successfully, as I check the umask afterward by typing:
    and it echoes:
    The result I expect is for the default umask to be changed to 0037. I simply don't know how to apply the same thing I do in a terminal to my perl script.

      This works successfully, as I check the umask afterward by typing

      Consider the process tree like an outline. Each node in the outline has a set of attributes. Each node is only able to modify its own attributes. When a child process is created, the attributes from the parent node are copied to the child node.

      umask is an attribute of the process. Your process is only able to modify the umask of itself. All children started by that process will then inherit the current umask at the time the child process is invoked. You cannot modify any process that is not the current process (ignore kernel hackery for this discussion).

      What you are considering behavior that "works" is the following:

      • Shell is using the internal directive to change its umask. From that moment on (at least until it is changed again), the shell is running with the new umask. If this were done with an external command, it would not work, since the external command would be on a different node in the outline.
      • Every child process started after that point inherits the current umask of the shell at the time the new process is started.

      What you are seeing in the terminal is consistent with the outline / attribute analogy. What you are seeing in the perl code is also consistent with the analogy. You are starting a child process (`umask...`), which is inheriting the current umask value of the perl process. Once the child process is complete, the parent continues on, not effected by what the child process did to change its umask attribute.

      When the perl process terminates, the parent process that started perl also does not care what perl did to its own umask attribute.

      Once you understand that a process is not effected by its children (outside of IPC means), a lot of things become clearer.


      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.

      Your terminal example corresponds to this:
      perl -e 'print `bash -c "umask 0037 ; umask"`'
      And it works as well. What you are trying to do corresponds rather to this terminal session:
      umask 0037 exit umask
      which, clearly, does not do what you want.

Log In?

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

How do I use this? | Other CB clients
Other Users?
Others contemplating the Monastery: (3)
As of 2018-06-23 01:32 GMT
Find Nodes?
    Voting Booth?
    Should cpanminus be part of the standard Perl release?

    Results (125 votes). Check out past polls.