Beefy Boxes and Bandwidth Generously Provided by pair Networks
XP is just a number
 
PerlMonks  

Re^2: invoking umask command in perl

by austin43 (Acolyte)
on Jun 01, 2011 at 19:53 UTC ( [id://907702]=note: print w/replies, xml ) Need Help??


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

When I change it to 0037 in a terminal, I type:
umask 0037
This works successfully, as I check the umask afterward by typing:
umask
and it echoes:
0037
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.

Replies are listed 'Best First'.
Re^3: invoking umask command in perl
by MidLifeXis (Monsignor) on Jun 01, 2011 at 20:28 UTC

    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.

    --MidLifeXis

Re^3: invoking umask command in perl
by ig (Vicar) on Jun 01, 2011 at 20:30 UTC

    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.

Re^3: invoking umask command in perl
by choroba (Cardinal) on Jun 01, 2011 at 22:28 UTC
    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?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://907702]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others sharing their wisdom with the Monastery: (5)
As of 2024-03-29 15:29 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found