perl note
[dotfiles.git] / study-note.org
1 #+TITLE: Study Note
2 #+AUTHOR: Peng Li
3 #+EMAIL: seudut@gmail.com
4 #+DATE: 2016-08-18
5
6 * Programming Languages
7 ** Perl 
8 *** One line command
9 **** perl help
10 #+BEGIN_SRC sh :exports both :results output replace
11   perl --help
12 #+END_SRC
13
14 #+RESULTS:
15 #+begin_example
16
17 Usage: perl [switches] [--] [programfile] [arguments]
18   -0[octal]         specify record separator (\0, if no argument)
19   -a                autosplit mode with -n or -p (splits $_ into @F)
20   -C[number/list]   enables the listed Unicode features
21   -c                check syntax only (runs BEGIN and CHECK blocks)
22   -d[:debugger]     run program under debugger
23   -D[number/list]   set debugging flags (argument is a bit mask or alphabets)
24   -e program        one line of program (several -e's allowed, omit programfile)
25   -E program        like -e, but enables all optional features
26   -f                don't do $sitelib/sitecustomize.pl at startup
27   -F/pattern/       split() pattern for -a switch (//'s are optional)
28   -i[extension]     edit <> files in place (makes backup if extension supplied)
29   -Idirectory       specify @INC/#include directory (several -I's allowed)
30   -l[octal]         enable line ending processing, specifies line terminator
31   -[mM][-]module    execute "use/no module..." before executing program
32   -n                assume "while (<>) { ... }" loop around program
33   -p                assume loop like -n but print line also, like sed
34   -s                enable rudimentary parsing for switches after programfile
35   -S                look for programfile using PATH environment variable
36   -t                enable tainting warnings
37   -T                enable tainting checks
38   -u                dump core after parsing program
39   -U                allow unsafe operations
40   -v                print version, patchlevel and license
41   -V[:variable]     print configuration summary (or a single Config.pm variable)
42   -w                enable many useful warnings
43   -W                enable all warnings
44   -x[directory]     ignore text before #!perl line (optionally cd to directory)
45   -X                disable all warnings
46   
47 Run 'perldoc perl' for more help with Perl.
48
49 #+end_example
50
51 **** Used as =grep=
52 #+BEGIN_SRC sh
53   perl -ne 'print if /expression/' xxx.log;
54 #+END_SRC
55
56 #+BEGIN_SRC perl :results output replace
57   my $tt = "hello";
58   ($tt =~ /h(.*)o/ ) and print $1;
59 #+END_SRC
60
61 #+RESULTS:
62 : ell
63
64 Or 
65 #+BEGIN_SRC sh
66   perl -wnl -e '/keyword/ and print;' xxx.log
67 #+END_SRC
68
69 **** Used as =awk=
70 #+BEGIN_SRC sh
71   perl -ne 'print "$1\n" if /aaa=(\d+)\s.*/' xxx.log;
72 #+END_SRC
73
74 #+BEGIN_SRC fundamental :tangle ./temp/aa.txt
75   aa: 1
76   bb: 2
77   cc: 3
78   dd: 4
79 #+END_SRC
80
81 #+BEGIN_SRC sh :results output replace
82   perl -wnl -e 'print "==$.: $_"' ./temp/aa.txt
83 #+END_SRC
84
85 #+RESULTS:
86 : ==1: 
87 : ==2: aa: 1
88 : ==3: bb: 2
89 : ==4: cc: 3
90 : ==5: dd: 4
91
92 ** gnuplog
93
94 1. normal
95 #+BEGIN_SRC gnuplot :exports code :file ./temp/sin.png
96   reset
97   plot sin(x)/x
98 #+END_SRC
99
100 #+RESULTS:
101 [[file:./temp/sin.png]]
102
103 2. plot with data file
104 #+BEGIN_SRC fundamental :tangle ./temp/plot.dat 
105   # x y
106   1 11
107   2 12
108   3 13
109   4 14
110   5 15
111   6 16
112   7 17
113   8 18
114   9 19
115 #+END_SRC
116
117 #+BEGIN_SRC gnuplot :file ./temp/ll.png
118   reset
119   set xrange [0:10]
120   set yrange [0:20]
121   plot "./temp/plot.dat" using 1:2 with lines
122 #+END_SRC
123
124 #+RESULTS:
125 [[file:./temp/ll.png]]
126