This repository has been archived by the owner on Oct 15, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1.8k
/
perl.json
224 lines (224 loc) · 7.28 KB
/
perl.json
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
{
"id": "perl_cheat_sheet",
"name": "Perl",
"description": "Essential Perl basic syntax, methods, functions, variables, references and data structure ",
"metadata": {
"sourceName": "Cheatography",
"sourceUrl": "http://www.cheatography.com/mishin/cheat-sheets/perl-reference-card/"
},
"template_type": "code",
"section_order": ["Input/Output", "Scalars and Strings", "Arrays and Lists", "Hashes", "Basic Syntax", "References and Data Structures", "System Interaction"],
"sections": {
"Input/Output": [{
"val": "Open file for input",
"key": "open(INFILE,\"in.txt\") or die;"
}, {
"val": "Open file with encoding",
"key": "open(INFILE,\"<:utf8\",\"file\");"
}, {
"val": "Open anonymous temp file",
"key": "open(TMP,\"+>\", undef);"
}, {
"val": "open in-memory file",
"key": "open(MEMORY,'>', \\$var);"
}, {
"val": "Open output file",
"key": "open(OUT,\">out.txt\") or die;"
}, {
"val": "Open file for append",
"key": "open(LOG,\">>my.log\") or die;"
}, {
"val": "Get next line",
"key": "$line = <INFILE>;"
}, {
"val": "Slurp infile",
"key": "@lines = <INFILE>;"
}, {
"val": "Print to STDERR",
"key": "print STDERR \"Warning 1.\\n\";"
}, {
"val": "Close filehandle",
"key": "close INFILE;"
}],
"Scalars and Strings": [{
"val": "Discard trailing \\n",
"key": "chomp($str);"
}, {
"val": "$v becomes trailing char",
"key": "$v = chop($str);"
}, {
"val": "String comparison",
"key": "eq, ne, lt, gt, le, ge, cmp"
}, {
"val": "Find index of $x in $str,",
"key": "$v = index($str, $x);"
}, {
"val": "Starting from left or right",
"key": "$v = rindex($str, $x);"
}, {
"val": "Extract substring",
"key": "$v = substr($str, $strt, $len);"
}, {
"val": "Count the digits in $sky",
"key": "$cnt = $sky =~ tr\/0-9\/\/;"
}, {
"val": "Change non-alphas to space",
"key": "$str =~ tr\/a-zA-Z\/\/cs;"
}, {
"val": "Format string",
"key": "$v = sprintf(“%10s %08d”,$s,$n);"
}],
"Arrays and Lists": [{
"val": "Array initialization",
"key": "@a = (1..5);"
}, {
"val": "Number of elements in @a",
"key": "$i = @a;"
}, {
"val": "Swap $a and $b",
"key": "($a, $b) = ($b, $a);"
}, {
"val": "Access to index 1",
"key": "$x = $a\\[1\\];"
}, {
"val": "Last index in @a",
"key": "$i = $#a;"
}, {
"val": "Appends $s to @a",
"key": "push(@a, $s);"
}, {
"val": "Removes last element",
"key": "$a = pop(@a);"
}, {
"val": "Remove last char (per el.)",
"key": "chop(@a);"
}, {
"val": "Removes first element",
"key": "$a = shift(@a);"
}, {
"val": "Reverse @a",
"key": "reverse(@a);"
}, {
"val": "Sort numerically",
"key": "@a = sort\\{$ela <=> $elb\\}(@a);"
}, {
"val": "Split string into @a",
"key": "@a = split(\/-\/,$s);"
}],
"Hashes": [{
"val": "Hash initialization",
"key": "%h=(k1 => “val1”,k2 => 3);"
}, {
"val": "Recall value",
"key": "$val = $map\\{k1\\};"
}, {
"val": "Array of keys and values",
"key": "@a = %h;"
}, {
"val": "Create hash from array",
"key": "%h = @a;"
}, {
"val": "Iterate over list of keys",
"key": "foreach $k (keys(%h))\\{..\\}"
}, {
"val": "Iterate over list of values",
"key": "foreach $v (vals(%h))\\{..\\}"
}, {
"val": "Iterate over key-value-pairs",
"key": "while (($k,$v)=each %h)\\{..\\}"
}, {
"val": "Delete key",
"key": "delete $h\\{k1\\};"
}],
"Basic Syntax": [{
"val": "Read command line params",
"key": "($a, $b) = @ARGV;"
}, {
"val": "Define subroutine",
"key": "sub p\\{my $var = shift; ...\\}"
}, {
"val": "Execute subroutine",
"key": "p(“bla”);"
}, {
"val": "Conditional",
"key": "if(expr)\\{\\} elsif \\{\\} else \\{\\}"
}, {
"val": "Negative conditional",
"key": "unless (expr)\\{\\}"
}, {
"val": "While-loop",
"key": "while (expr)\\{\\}"
}, {
"val": "Until-loop",
"key": "until (expr)\\{\\}"
}, {
"val": "Postcheck until-loop",
"key": "do \\{\\} until (expr)"
}, {
"val": "For-loop",
"key": "for($i=1; $i<=10; $i++)\\{\\}"
}, {
"val": "Foreach-loop",
"key": "foreach $i (@list)\\{\\}"
}, {
"val": "End loop, skip to next, jump to top",
"key": "last, next, redo"
}],
"References and Data Structures": [{
"val": "Reference to array",
"key": "$aref = \\@a;"
}, {
"val": "Anonymous array",
"key": "$aref = \\[1,\"foo\",undef,13\\];"
}, {
"val": "Access element of array",
"key": "[$el = $aref->\\[0\\];] / [$el = @\\{$aref\\}\\[0\\];]"
}, {
"val": "Copy array",
"key": "$aref2 = \\[@\\{$aref1\\}\\];"
}, {
"val": "Reference to hash",
"key": "$href = \\%h;"
}, {
"val": "Anonymous hash",
"key": "$href =\\{APR => 4,AUG => 8\\};"
}, {
"val": "Access element of hash",
"key": "[$el = $href->\\{APR\\};] / [$el = %\\{$href\\}\\{APR\\};]"
}, {
"val": "Copy hash",
"key": "$href2 = \\{%\\{$href1\\}\\};"
}, {
"val": "Checks if $r points to hash",
"key": "if (ref($r) eq \"HASH\") \\{\\}"
}, {
"val": "2-dim array",
"key": "@a = (\\[1, 2\\],\\[3, 4\\]);"
}, {
"val": "Access 2-dim array",
"key": "$i = $a\\[0\\]\\[1\\];"
}],
"System Interaction": [{
"val": "System call",
"key": "system(“cat $f|sort -u>$f.s”);"
}, {
"val": "Catch output",
"key": "@a = readpipe(“lsmod”);"
}, {
"val": "Catch output",
"key": "$today = “Today: “.date;"
}, {
"val": "Change root",
"key": "chroot(“\/home\/user\/”);"
}, {
"val": "Operate on all c-files",
"key": "while (<*.c>) \\{\\}'"
}, {
"val": "Delete file",
"key": "unlink(“\/tmp\/file”);"
}, {
"val": "File test",
"key": "if (-f “file.txt”)\\{...\\}"
}]
}
}