Beefy Boxes and Bandwidth Generously Provided by pair Networks
There's more than one way to do things
 
PerlMonks  

Some automation on Perlscripts?

by matze77 (Friar)
on Dec 04, 2008 at 15:30 UTC ( [id://728002]=perlquestion: print w/replies, xml ) Need Help??

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

Hello Seekers of pure Wisdom!

Since i am really lazy and find it annoying to do this for every new perlscript:

#!/usr/bin/perl use strict; use warnings;

So i consider to insert the above lines (if not already at the first line of the file #!/usr/bin/perl)
in every file in directory $directory = "~/perl" , afterwards if the file is *not executable* i want it to "chmod u+x"


I think i have to open the file?
Some hints on this would be fine?
Thanks in Advance MH

Replies are listed 'Best First'.
Re: Some automation on Perlscripts?
by Fletch (Bishop) on Dec 04, 2008 at 15:39 UTC

    Hrmm, perhaps if there were some sort of programming language which was handy for manipulating text and interacting with files you could write a program to do this . . .

    (See How (Not) To Ask A Question. Show what you've tried. Also note that many editors allow creating skeleton project files for use as starting points.)

    The cake is a lie.
    The cake is a lie.
    The cake is a lie.

      Yeah, like javascript. Or wait let me guess.. You probably had microsoft visual basic in mind.

        That's absurd! C# was basically developed for this exact purpose! :P

        I'm so adjective, I verb nouns!

        chomp; # nom nom nom

Re: Some automation on Perlscripts?
by almut (Canon) on Dec 04, 2008 at 16:16 UTC
    So i consider to insert the above lines...

    Sounds like a good example of those little not-entirely-hypothetical problems you were looking for recently ;)

    Kidding aside... I think your learning progress would benefit most from first trying yourself — in particular your heuristic skills of how to approach a problem (yeah, asking on PerlMonks right away could be considered a good heuristic skill, too (it's efficient in that it often gives you a solution within minutes), but I think you know what I mean... :)  You can always come back here in case you should get stuck.

      You are right, i have to work on my "soft skills" too (not starting too many things in parallel), i started in the lama book and since there are many practicing scripts to do i thought of this initially since i got something like a typewriter's cramp (its more a mouse users cramp, which is work related) but you are right i think i have to do a lot of work and i maybe wont bother you for a while now ...

      Thanks almut for your always useful replies!, and all others too for their patience
      MH
Re: Some automation on Perlscripts?
by leocharre (Priest) on Dec 04, 2008 at 15:48 UTC

    I think scanning a directory for files and making them executable without knowing what is really in there- is a security risk. If you already know what's in there, using chmod 0755 or whatever should not be a big deal.
    I'm assuming here your ~/perl are scripts written- not something out of a package.

    Consider putting your stuff in cvs/svn, making packages out of them- then you use your Makefile.PL to get those to install properly and chmod etc.

    As for issue #1, I understand your point. I even agree. However, I feel this is a waste of time. For reference, may I suggest 'The Mythical Man Month' by Fred Brooks. He talks about this kind of thing. How coders are toolmakers, and when we go out to chop wood, we first sharpen the axe. Then we find a better way to sharpen the axe. Then we find a better way to prepare the better way to sharpen the axe. Then..

    Anyhow. I think the solution for that shortcut you mention is better left to people writing the innards of perl itself. This is the kind of thing amateurs like us would just be shooting ourselves in the foot with.

    ++ For your ambition and curiosity. I applaude it.

      Thanks for your entertaining reply. Is it right like i seemed to have learned that it is "somewhat complicated" to manipulate files with Perl and Perl is better suited for "manipulating text" so i would use Perl for inserting the text and bash (or anything else) for manipulating file permissions, or i entirely use bash ...? What do you think of using "syscalls" from Perl to start a shell function?


      Sorry for asking a no go question ...

      I never was aware before that some things you would better not do in (or are even impossible) language "x" but that answers the questions why there are so many languages out there ...

      Thanks for sharing your thoughts.
      MH

        Using syscalls from perl to shell..
        From a point of view of getting things done, do it.

        If you're developing software for release and installating in multiple machines over which you have no control- there's a few things you gotta do extra in your software. Like, checking that the stuff exists, etc.

        Perl, CPAN, will check great for perl module dependencies. But it's little bit harder for system applications. Purely perl speaking.

        The things I pointed out, it's not that you should never do it. It's about picking your battles.
        A system to properly do this, without corrupting data, etc is harder than it sounds.

        There's a difference between something that *looks* like it works, and something that does.

        And then, being on posix, that's part of what the whole thing is about, you use all these things together. Perl, bash, c, whatever.

        You can manipulate files and set permissions fine with perl. Fletch was cracking a joke. Perl is the answer to the question.

Re: Some automation on Perlscripts?
by JavaFan (Canon) on Dec 04, 2008 at 15:53 UTC
    You'd obviously need the shell for that. Something like:
    for f in *; do (echo '#!/usr/bin/perl'; echo 'use strict;'; echo 'use warnings;'; + (tail +2 $f | grep -v 'use strict' | grep -v 'use warnings')) > $f.$ +$; mv $f.$$ $f; chmod +x $f; done
    Adjust to taste.
Re: Some automation on Perlscripts?
by n3toy (Hermit) on Dec 04, 2008 at 16:08 UTC
    Yea, there is a possible security risk involved with your method. Plus a lot of typing *everytime* you add a new file to your directory. That seems like a lot of work. :-)

    I use an IDE for all of my work related Perl and it has an option for a new file template. But I have never found typing the above mentioned two lines a burden.

    Jamie

      i thought of filling the directory with e.g. 20 empty files which i name Perlex(1-19) (this can go automatic really easy) then they should be filled with the "skeleton text" then after i compose my scripts i would simple rename them, security: since it is my home directory of my "testing box" which is not a server i see no security risks at all ...

      MH
Re: Some automation on Perlscripts?
by lakshmananindia (Chaplain) on Dec 05, 2008 at 04:03 UTC
    If you are using Vim then use the Perl support plugin .
    Or otherwise use the Autocommand in the vimrc.
    Example
     autocmd bufnewfile *.pl so /home/lakshmanan/Bash/source_for_perl
    source_for_perl
    :insert #!/usr/bin/perl use strict; .
    Now if I open a new *.pl file, then the text in the source_for_perl will be there.
      You are my new hero. This is one of the coolest since moving ranges of lines with
      :21,25m40
Re: Some automation on Perlscripts?
by Dranzaz (Sexton) on Dec 04, 2008 at 18:36 UTC
    Hello,

    I like the lazy part, but I also like order and backups. For this reason I have a generic file (newscript.pl) with the following default code:
    #!/usr/bin/perl -w ####################### # set Modules to Use; ####################### use strict; use warnings; use CGI qw(:standard); ####################### # Establish variables, arrays, ect. ####################### # do some stuff here ####################### # Begin main body ####################### #do more stuff here ####################### # Functions & Subroutines ####################### #do the detailed work here

    This file is stored on my thumbdrive and I use Notepad++ for all my editing. I do not use VI unless I have to. I edit the files locally on my laptop/pc and SCP/SFTP the files as they are edited. This allows me to get the "pretty colors" in the editor helping me know what is a variable, operator, modules, etc. but also ensures that I have a backup copy of all my data should there be a server failure with no data recovery available (it has happened). I do try and keep a simular if not the exact directory structure on the thumbdrive to help in restoring the files if need be.

    Just my $0.02....Thanks for your time.

      sometimes the simple approach is the best. I like your idea of a file newscript.pl. it is easy to use ":w newfilename.pl" in vi. Unfortunately there is no notepad++ in my Debian repositories (on windows machines i like notepad++).
      But i am fine with vi this helps me too: .vimrc for perl programmers


      Thanks for this great tip.
      MH

        Do you run vim in vi-compatible mode or im vim-mode?

        you can configure your vim, so it reads from a template file if you create a new .pl file

        .vimrc:
        if has("autocmd") " load template file for new file and jump to line # autocmd BufNewFile *.pl r ~/.vim/templates/pl | 1d | 5 endif

        if vim opens a non-existant file (filename ending with ".pl"),
        it inserts the content of file "~/.vim/templates/pl" after the current line
        as we opened a new buffer, the current line was 1; that is now empty and is to be deleted
        as a last action the cursor is positioned in line 5

        (please note, that the pipe is the command separator in vim)

        ~/.vim/templates/pl:
        #!/usr/bin/perl use strict; use warnings; #> global variables (the less, the better) #> ------------------------------------------------------------------- +------- #> sub routines #> ------------------------------------------------------------------- +------- # main script #> ------------------------------------------------------------------- +------- __END__

        See :help autocmd in vim for more details of that feature.
        I think you can even configure it so it adjusts the file permissions on open/save/whatever....

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others lurking in the Monastery: (8)
As of 2024-04-19 07:42 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found