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

simple Perl script template

by Dumu (Monk)
on Apr 09, 2015 at 10:47 UTC ( [id://1122901]=CUFP: print w/replies, xml ) Need Help??

I wrote this script because I just wanted to automate typing two lines of Perl every time I wrote a small test-case script.

This is dead simple and doesn't really count as 'cool'. However, in the words of the Greek poet Callimachus and E.F. Shumacher, "small is beautiful".

The script simply prints its own top two lines into every new file specified on the command line, unless the file already exists.

Any and all feedback will be welcomed.

#!/usr/bin/env perl use Modern::Perl; while (<@ARGV>) { my $fname = $_; unless (-e $fname) { open my $fh, '>', $fname; say $fh "#!/usr/bin/env perl"; say $fh "use Modern::Perl;\n\n"; say STDOUT "created $fname"; } else { say STDOUT "*** didn't overwrite $fname"; } }

Replies are listed 'Best First'.
Re: simple Perl script template
by jeffa (Bishop) on Apr 09, 2015 at 15:04 UTC

    It's a nice idea, but surely your editor of choice already has the means to produce a template for you, determined by the extension of the new file you are creating. I use perl-support.vim which provides an uber template file for producing .pl, .pm, .t files and others. Here's the the .pl template that i use, notice how it is large enough to warrant a template and not line by line print statements:

    Perhaps you noticed the <CURSOR> token in the middle? That's where my editing cursor appears, ready to insert code. Very handy.

    jeffa

    L-LL-L--L-LL-L--L-LL-L--
    -R--R-RR-R--R-RR-R--R-RR
    B--B--B--B--B--B--B--B--
    H---H---H---H---H---H---
    (the triplet paradiddle with high-hat)
    

      My editor of choice is also Vim, so I shall investigate perl-support.vim.

      I wanted this because I kept writing little scripts to test Perl and CPAN dist features with the two lines at the top of this script. I thought automating this in Vim would mean hacking Vimscript, and I just thought "hang on, automating text files - why not use Perl?". I should have known that indomitable Vim would include templating support or have a plugin for it.

      This script does exactly what I need for writing little scripts though, so I shall probably continue to use it - probably alongside perl-support.vim if it meets my needs.

      After all, sometimes I do use ed instead of Vim :D

Re: simple Perl script template
by roboticus (Chancellor) on Apr 09, 2015 at 11:11 UTC

    Dumu:

    You may want to compare line 8 with line 1 and make appropriate changes.... ;^D

    ...roboticus

    When your only tool is a hammer, all problems look like your thumb.

      You may want to compare line 8 with line 1 and make appropriate changes....

      ... or use $^X.

      Alexander

      --
      Today I will gladly share my knowledge and experience, for there are no sweeter words than "I told you so". ;-)

        Or just print the first two lines of the script itself in order to avoid inconsistencies:

        #!/usr/bin/env perl use Modern::Perl; seek DATA, 0, 0; my $header = join '', (<DATA>)[0..1]; for my $fname (@ARGV) { unless (-e $fname) { open my $fh, '>', $fname; print $fh $header; say STDOUT "created $fname"; } else { say STDOUT "*** didn't overwrite $fname"; } } __DATA__
      Thanks roboticus. I have updated the script from :
      say $fh "#!/usr/bin/perl env";
      to:
      say $fh "#!/usr/bin/env perl";
      8^)
Re: simple Perl script template
by pme (Monsignor) on Apr 09, 2015 at 11:40 UTC
    Hi Dumu,

    You should check the state of file opening:

    open my $fh, '>', $fname or die "Cannot open $fname - $!\n";
    Unless Modern::Perl does that. I've never used Modern::Perl.

    Update: 'warn' changed to 'die' and '\n' were appended.

      Modern::Perl basically enables strict, warnings, and features for an appropriately modern version of Perl.
      That's an interesting design choice. Why do you want to punish the remaining files (die) just because one of them turns out to be unopenable?
        I like the idea of just warning, in case only one of the files can't be written.

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others goofing around in the Monastery: (4)
As of 2024-04-24 03:40 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found