http://www.perlmonks.org?node_id=655115

BadMagic has asked for the wisdom of the Perl Monks concerning the following question:

Hi,
I'm getting sick of having to chmod 755 every perl .pl file I create. I use Vim to create/edit my files and I'm using the BASH shell under Solaris.
Is there some way of getting .pl files to automagically be created with the 755 permissions without having to chmod them or change the default file creation permissions?
I've searched everywhere for this from the man pages to Google and I can't seem to find any information pertaining to my problem.

Replies are listed 'Best First'.
Re: perl file permissions
by shmem (Chancellor) on Dec 05, 2007 at 16:31 UTC
    You could put
    pvi () { touch $*; chmod 755 $*; vim $* }

    into your ~/.bashrc and use pvi to edit your perl files.

    --shmem

    _($_=" "x(1<<5)."?\n".q·/)Oo.  G°\        /
                                  /\_¯/(q    /
    ----------------------------  \__(m.====·.(_("always off the crowd"))."·
    ");sub _{s./.($e="'Itrs `mnsgdq Gdbj O`qkdq")=~y/"-y/#-z/;$e.e && print}
Re: perl file permissions
by dsheroh (Monsignor) on Dec 05, 2007 at 16:30 UTC
    The file is actually created by your editor, not by bash (unless you're doing a touch foo.pl or similar and then editing the empty file), so the answer would be editor-specific. Most likely, though, the editor (like bash) will use a single umask setting which is applied to determine the initial permissions of all new files, regardless of their type.

    However, when copying a file, bash copies its permissions as well... My personal workaround to avoid lots of chmodding is to cp foo bar; vi bar (where foo happens to be a Perl program/module - the .pl is strictly optional) so that I'm editing an existing file which already has the right permissions rather than creating a new one with the editor.

Re: perl file permissions
by sh1tn (Priest) on Dec 05, 2007 at 16:36 UTC
    I use the following bash script to automate this task:
    #!/bin/bash file=$1 if [ -e $file ] then vim $file else touch $file echo '#!'`which perl` > $file echo '' >> $file echo 'use strict;' >> $file echo 'use warnings;' >> $file echo '' >> $file chmod +x $file vim $file fi


Re: perl file permissions
by sgifford (Prior) on Dec 05, 2007 at 16:55 UTC
    You could also alias vi to run a script that will execute vi, then chmod the file if appropriate. Here's a start:
    #!/bin/sh cmdline="" while [ $# -gt 0 ]; do case "$1" in --) break;; [+-]*) cmdline="$cmdline $1"; shift;; *) break;; esac done fn="" if [ $# -gt 0 ]; then fn=$1 fi vi $cmdline $fn ec=$? if [ -n "$fn" -a -f "$fn" ]; then case "$fn" in *.pl) chmod +x "$fn";; esac fi exit $ec
      I am very appreciative of this site and all who have responded to help.
      I've experimented with the various suggested methods and I think this one may be the best for me.
      Thank you for your assistance.
Re: perl file permissions
by johngg (Canon) on Dec 05, 2007 at 16:33 UTC
    Have a look at this thread.

    Cheers,

    JohnGG

Re: perl file permissions
by KurtSchwind (Chaplain) on Dec 05, 2007 at 16:41 UTC

    In *nix land, your file permissions are based on your umask.

    Having said that, you cannot set a umask in a way that will toggle the x flag. I like the .bashrc solution since you mentioned that you are using bash as your shell.

    Note: It's not a failing of umask to not let you default to +x on a file. That's a GoodThing(tm). Having +x by default is a BadThing(tm). But if you just want a shortcut to editing perl and adding the +x, then the alias is your best bet.

    --
    I used to drive a Heisenbergmobile, but every time I looked at the speedometer, I got lost.
      you cannot set a umask in a way that will toggle the x flag

      I s'pose it depends on what you mean by toggle. Only programs like compilers set execute permission on in the first place. The umask can only remove permissions that the original utility sets, it can't add them.

        I'm not sure what you mean by that. You can set ANY file in unix to execute (assuming you have the perms to do so.) You can make an mp3 file 'executable' if you want to. You certainly don't need a compiler.

        umask [-p] [-S] [mode] The user file-creation mask is set to mode. If mode begins with a dig +it, it is interpreted as an octal number; otherwise it is interpreted as a symbolic mode mask + similar to that accepted by chmod(1). If mode is omitted, the current value of the mask is printe +d. The -S option causes the mask to be printed in symbolic form; the default output is an octal + number. If the -p option is supplied, and mode is omitted, the output is in a form that +may be reused as input. The return status is 0 if the mode was successfully changed or if no m +ode argument was supplied, and false otherwise.

        --
        I used to drive a Heisenbergmobile, but every time I looked at the speedometer, I got lost.
Re: perl file permissions
by kyle (Abbot) on Dec 05, 2007 at 16:33 UTC

    I usually avoid this problem by typing 'perl' before the script name. That is:

    perl foo.pl

    Then it doesn't matter what the permissions are.

    I don't know of a way to automatically create .pl files with particular permissions, but you could make a cron job that does it or an alias. It would look something like:

    find ~ -name \*.pl -print0 | xargs -0 chmod 755

    I don't recommend that, however. It's a little too blind for my taste.

Re: perl file permissions
by hsinclai (Deacon) on Dec 05, 2007 at 17:25 UTC
    I have the following function set up as a bash alias:
    newpl () { [[ -z $1 ]] && { echo "usage: newpl FILENAME" return } [[ -d $1 ]] || [[ ! -e $1 ]] && vim $1; [[ -e $1 ]] && { chmod 0755 $1 /bin/ls -al $1 } || { echo "File $1 was not saved" } }

    ..and these 2 lines in my .vimrc:
    ab bbb #!/bin/bash ab ppp #!/usr/bin/perl ^M^Muse strict;^Muse warnings;^M^M
    (those ^M are made inside of vim with CTRL-v, CTRL-m)

    So on the command line just do newpl newfilename
    And when vim starts, <i> ppp <enter>
    Not as comprehensive as some of the other examples here but "works" :)

Re: perl file permissions
by tcf03 (Deacon) on Dec 05, 2007 at 18:37 UTC
    alias xp='chmod 755 *.pl' alias x='chmod 755 '
    will save you some keystrokes.
    Ted
    --
    "That which we persist in doing becomes easier, not that the task itself has become easier, but that our ability to perform it has improved."
      --Ralph Waldo Emerson
Re: perl file permissions
by BadMagic (Initiate) on Dec 06, 2007 at 12:18 UTC
    Hi all,

    I've been tinkering with my .vimrc file all afternoon and found a perfect solution to my permissions problem and thought I'd post it in case it can help someone else who uses vim to create perl files. It also puts in the #!/usr/bin/perl, use strict; and use warnings; lines:

    I put the following line in my .vimrc file:
    imap ,perl #!/usr/bin/perl<CR><CR>use strict;<CR>use warnings;<CR><CR> +<esc>:w<ENTER>:!chmod 755 %<ENTER><ENTER>i

    (You also need to add 'set autoread' to your .vimrc file otherwise Vim will complain about the permissions change during editing.

    Now all I have to do when I'm in a newly created file is go into '--INSERT--' mode and type ',perl' and the file gets filled out, saved, chmods it to 755 and returns you to '--INSERT--' mode at the bottom of the file, ready to start typing.
    I hope others who use Vim may benefit from this as well. I'd like to know if anyone thinks this would come in handy.

    I thank everyone for their assistance. This is one of the best Web sites for Perl I've ever seen.

    Cheers, BadMagic