-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathperl.pl
executable file
·129 lines (107 loc) · 2.53 KB
/
perl.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
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
#!/usr/bin/perl
my $line = <STDIN>;
print $line;
chop ($line);
open (my $fh, "<", $file)
or die "Could not open file $file";
while (my $line = <$fh>) {
chomp $line;
process_line ($line);
}
#hash
my %hash;
$hash{'fname'} = "Gilbert";
$hash{'lname'} = "Gong";
# sort keys
foreach my $key (sort keys %hash) {
print ("${key}: $hash{$key}\n");
}
#array
my @array = (1, 2, 3, "this is an element: ${element}");
@array = split (/ /, $line);
my $val = $array[3];
my ($login, $passwd) = split(/:/);
my $joined = join (',', @csv, $h, $d, $m);
# subroutine and elsif
sub strip_put {
my $line = shift;
# if not enabled, just return unchanged
if (!$opt_strip_put) {
return $line;
}
if ($line =~ /^put /) {
$line = substr ($line, 4);
} elsif {
} else {}
return $line;
}
for (my $i=0; $i <= 9; $i++) {
print "$i\n";
}
# $#array
for (my $i = 0; $i <= $#array; $i++) {
print ("element $i: $array[$i]\n")
}
# array slices
@subarray = @array[0..19] # first 20 elements
@subarray = @array[4,1,3] # 3 of the elements pulled out
@elems = (4,1,3);
@subarray = @array[@elems] # same
@array[1,2] = @array[2,1] # swap two elements
# split
($t, $t, $token) = split (/ /, $line);
($t, $iops) = split (/=/, $token);
# append
$read .= $iops;
# socket
use IO::Socket::INET;
my $port=4243;
my $listen_ip="127.0.0.1";
my $autoflush = 1;
my $listen_queue = 10;
# creating a listener
my $listener = new IO::Socket::INET (
LocalHost => $listen_ip,
LocalPort => $port,
Proto => 'tcp',
Listen => $listen_queue,
Reuse => 1
);
die "cannot create listener $!\n" unless $listener;
$listener->autoflush($autoflush);
sub handle_client {
my $client_socket = shift;
my $line = "";
while ($line = $client_socket->getline()) {
$line = strip_put ($line);
print ($line);
}
# other side closed connection, now we close our side
shutdown($client_socket, 1);
}
##### main loop
while(1) {
my $client = $listener->accept();
handle_client ($client);
}
$listener->close();
#process
my $val = `echo hello, world!`;
print ("val is set to ${val}\n");
system("echo", "hello, world!") # runs this, then returns to perl
my $retval = fork();
if ($retval = 0) {
#this is the child
} else {
#this is the parent
}
# tie dbm hash
use AnyDBM_File;
use Fcntl;
tie (my %hash, "AnyDBM_File", "/tmp/tmpdbm", O_CREAT|O_RDWR, 0600)
or die "Can't open database: $!\n";
# initially blank, but populated on second run
print ("name in hash is: $hash{'name'}\n");
$hash{'name'} = "Gilbert";
print ("name in hash is: $hash{'name'}\n");
untie %hash;