-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbasics.pl
executable file
·96 lines (55 loc) · 1.68 KB
/
basics.pl
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
#!/usr/bin/perl
# PERL BASICS
# (sections are divided by three empty lines between them)
# $ for scalar variable, a single value like a string or a number
$hello = "hi mom";
# string interpolation
print "$hello, perl is cool! \n";
my $local = "I am a local variable";
our $global = "I am a global variable";
# @ for array, a collection of values
@nums = (10, 20, 30);
# the index of any item in the array can be accessed with brackets
@nums[1];
# % for hash, key-value pairs
%friends = ('Larry', 67, 'Ken', 79);
# Use braces to access one of its keys
%friends{'Larry'};
# "things that are different, should look different"
# conditionals
if (5 > 10) {
print "hi";
}
# can be refactored to one-liners
print "hi" if 5 > 10;
# and can be made even more concise using the ternary operator
$result = (5 > 10)? "hi" : "bye";
# functions in Perl are defined as subroutines
sub PerlCanBeFun {
print "this is function\n";
# instead of defining parameters within parentheses,
# any arguments passed to the function can be accessed
# with the @_ array
my ($n1, $n2) = @_;
print $n1 + $n2;
}
PerlCanBeFun(2, 3);
# regex is built-in for parsing text
# the binding operator =~ can be used to match a string
# to a regular expression
if ($text =~ /cool/) {
# do this
}
# autovivification: bring missing data to life
# if we try to increment the value in an array
# that doesn't exist, it will automatically bring
# to life all the elements in between
@counter = (1..10);
$counter[20++];
$len = @counter;
print $len;
# to run code either made the script executable and simple
# run it in the folder
# $ ./basics.pl
# or run the interpreter
# $ perl basics.pl