Download presentation
Presentation is loading. Please wait.
Published byColleen Flowers Modified over 8 years ago
1
1 Forget Shell Scripts! Mike Schilli, 06/16/2008 Yahoo!
2
2 Overview Pros and cons of shell/Perl scripts How to whip up Perl scripts like Shell scripts How to create reusable scripts in Perl
3
3 Where Shell Scripts shine Fast to whip up Running virtually everywhere (if you stick to /bin/sh syntax)
4
4 Where Shell Scripts shine Don’t need to quote strings Don’t need to separate arguments by commas: Shell: cmd a b Perl: cmd(“a”, “b”);
5
5 Where Shell Scripts shine Don’t need to quote strings Don’t need to separate arguments by commas: Shell: cmd a b Perl: sub cmd; cmd “a”, “b”;
6
6 Where Shell Scripts Suck Can be written nicely, but mostly aren’t (see autoconf) Modularity Debugging Logging No CPAN to share stuff with Speed (interpreted line by line)
7
7 Where Shell Scripts Suck Horrid syntax pitfalls: var = “foo” echo $var
8
8 Where Shell Scripts Suck Horrid syntax pitfalls: var = “-n woah!” echo $var
9
9 Where Shell Scripts Suck Horrid syntax pitfalls: var = “-n woah!” echo X”$var” | sed ‘s/^X//’ (Source: “Gnu Autoconf, Automake, and Libtool”, Chapter 21, “Writing portable bourne shell”)
10
10 Shell Programming gone wild!!!
11
11 Shell Programming gone wild!!!
12
12 Challenges with Perl Scripts Don’t run everywhere but Linux distros come with perl already but there might be missing CPAN modules Lots of typing (open/read/close, error handling)
13
13 Reusable one-off Perl scripts #!/usr/local/bin/perl use Sysadm::Install qw(cp); cp “foo”, “bar”;
14
14 Reusable one-off Perl scripts #!/usr/local/bin/perl –w use strict; use Sysadm::Install qw(cp); cp “foo”, “bar”;
15
15 Reusable one-off Perl scripts #!/usr/local/bin/perl use Sysadm::Install qw(:all); cp “foo”, “bar”; mv “bar”, “baz”;
16
16 Sysadm::Install commands cp (copy) mv (move) untar (tar zxf …) slurp (file to string) blurt (string to file) cd (with cdback()) make (call make)
17
17 Sysadm::Install commands Run a shell command: ($stdout, $stderr, $rc) = tap “ls”, “-R”;
18
18 Sysadm::Install commands In-place-edit of a file: pie sub { s/foo/bar/; $_ }, “file.dat”; Process a file: plough sub { print if /foobar/ }, “file.dat”;
19
19 Sysadm::Install commands $answer = ask “Proceed”, “y”; Proceed [y]? $picked = pick $prompt, \@choices, 3 ; [1] Foo [2] Bar [3] Baz What now [3]?
20
20 Sysadm::Install commands rmf() mkd() perm_set() perm_get()
21
21 Enable Logging #/usr/local/bin/perl –w use strict; use Sysadm::Install qw(cp); use Log::Log4perl (:easy); Log::Log4perl->easy_init($DEBUG); cp “foo”, “bar”; cp “abc”, “xyz”; my $s = slurp “foo.dat”;
22
22 Enable Logging (output) $ foo-script 2008/06/10 01:48:14 cp abc def 2008/06/10 01:48:14 cp ghi jkl 2008/06/10 01:48:14 slurp foo.dat [param1: 123\n … param10: 345]
23
23 Raising errors by default (shell) #/bin/sh cp foo bar cp abc xyz
24
24 Raising errors by default #/usr/local/bin/perl –w use strict; use Sysadm::Install 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
25
25 Keep it Clean Exporting all vs. just what you need use Sysadm::Install qw(:all); use Sysadm::Install (cp untar);
26
26 Install on Debian apt-get install libsysadm-install-perl
27
27 Additional Development Tips
28
28 Use main() Make a script reusable, put your code in main(): sub main { some_function(“a”); some_other_funtion(“b”); }
29
29 Use main() Make a script reusable: # script1 main() if !caller(); sub main { some_function() } sub some_function { … } 1;
30
30 Use 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();
31
31 Refactor 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
32
32 Get started quickly with new scripts tmpl do-this-task tmpl –p MyTask.pm tmpl –pm Fully::Qualified http://perlmeister.com/scripts/downloadl/tmpl Use vim shortcuts
33
33 Portability tricks PAR creates portable module bundles PAR::Packer creates self-contained executables: pp –o fooscript.exe fooscript pp –M IO::Zlib –o foo.exe foo Caveat: Same architecture Similar systems: Use oldest to compile
34
34 The End Proudly wear your “Say No To /bin/sh” logo
35
35 The End Thanks!
36
36 The End Slides: http://perlmeister.com/talks.html
Similar presentations
© 2024 SlidePlayer.com Inc.
All rights reserved.