Title: Forget Shell Scripts
1Forget Shell Scripts!
Mike Schilli, 06/16/2008 Yahoo!
2Overview
- Pros and cons of shell/Perl scripts
- How to whip up Perl scripts like Shell scripts
- How to create reusable scripts in Perl
3Where Shell Scripts shine
- Fast to whip up
- Running virtually everywhere (if you stick to
/bin/sh syntax)
4Where Shell Scripts shine
- Dont need to quote strings
- Dont need to separate arguments by commas
- Shell cmd a b
- Perl cmd(a, b)
5Where Shell Scripts shine
- Dont need to quote strings
- Dont need to separate arguments by commas
- Shell cmd a b
- Perl sub cmd
- cmd a, b
6Where Shell Scripts Suck
- Can be written nicely, but mostly arent (see
autoconf) - Modularity
- Debugging
- Logging
- No CPAN to share stuff with
- Speed (interpreted line by line)
7Where Shell Scripts Suck
- Horrid syntax pitfalls
- var foo
- echo var
8Where Shell Scripts Suck
- Horrid syntax pitfalls
- var -n woah!
- echo var
9Where Shell Scripts Suck
- Horrid syntax pitfalls
- var -n woah!
- echo Xvar sed s/X//
- (Source Gnu Autoconf, Automake, and Libtool,
Chapter 21, Writing portable bourne shell)
10Shell Programming gone wild!!!
11Shell Programming gone wild!!!
12Challenges with Perl Scripts
- Dont run everywhere
- but Linux distros come with perl already
- but there might be missing CPAN modules
- Lots of typing (open/read/close, error handling)
13Reusable one-off Perl scripts
- !/usr/local/bin/perl
- use SysadmInstall qw(cp)
- cp foo, bar
14Reusable one-off Perl scripts
- !/usr/local/bin/perl w
- use strict
- use SysadmInstall qw(cp)
- cp foo, bar
15Reusable one-off Perl scripts
- !/usr/local/bin/perl
- use SysadmInstall qw(all)
- cp foo, bar
- mv bar, baz
16SysadmInstall commands
- cp (copy)
- mv (move)
- untar (tar zxf )
- slurp (file to string)
- blurt (string to file)
- cd (with cdback())
- make (call make)
17SysadmInstall commands
- Run a shell command
- (stdout, stderr, rc)
- tap ls, -R
18SysadmInstall commands
- In-place-edit of a file
- pie sub s/foo/bar/ _ ,
- file.dat
- Process a file
- plough
- sub print if /foobar/ ,
- file.dat
19SysadmInstall commands
- answer ask Proceed, y
- Proceed y?
- picked pick prompt, \_at_choices, 3
- 1 Foo
- 2 Bar
- 3 Baz
- What now 3?
20SysadmInstall commands
- rmf()
- mkd()
- perm_set()
- perm_get()
21Enable Logging
- /usr/local/bin/perl w
- use strict
- use SysadmInstall qw(cp)
- use LogLog4perl (easy)
- LogLog4perl-gteasy_init(DEBUG)
- cp foo, bar
- cp abc, xyz
- my s slurp foo.dat
22Enable Logging (output)
- foo-script
- 2008/06/10 014814 cp abc def
- 2008/06/10 014814 cp ghi jkl
- 2008/06/10 014814 slurp foo.dat param1 123\n
param10 345
23Raising errors by default (shell)
- /bin/sh
- cp foo bar
- cp abc xyz
24Raising errors by default
- /usr/local/bin/perl w
- use strict
- use SysadmInstall qw(cp)
- cp foo, bar fails
- cp abc, xyz
- foo-script
- Cannot copy abc to def (No such file or
directory) at foo-script line 4
25Keep it Clean
- Exporting all vs. just what you need
- use SysadmInstall qw(all)
- use SysadmInstall (cp untar)
26Install on Debian
- apt-get install libsysadm-install-perl
27Additional Development Tips
28Use main()
- Make a script reusable, put your code in main()
- sub main
- some_function(a)
- some_other_funtion(b)
-
29Use main()
- Make a script reusable
- script1
- main() if !caller()
- sub main some_function()
- sub some_function
- 1
30Use main()
- Make a script reusable
- script1
- main() if !caller()
- sub main some_function()
- sub some_function
- 1
- Somewhere else
- other script
- require ./script1
- some_function()
31Refactor
- If a script turns out useful, refactor it to a
module - Use functions to encapsulate
- Add quick docs (use templates)
- Re-write the script to test the new module
- Push to CPAN if useful
32Get started quickly with new scripts
- tmpl do-this-task
- tmpl p MyTask.pm
- tmpl pm FullyQualified
- http//perlmeister.com/scripts/downloadl/tmpl
- Use vim shortcuts
33Portability tricks
- PAR creates portable module bundles
- PARPacker creates self-contained executables
- pp o fooscript.exe fooscript
- pp M IOZlib o foo.exe foo
- Caveat Same architecture
- Similar systems Use oldest to compile
34The End
- Proudly wear your Say No To /bin/sh logo ?
35The End
36The End
- Slides http//perlmeister.com/talks.html