Let's cover Split, Chomp, For loops, and a console input
#!/usr/bin/perl
use strict;
use warnings;
# Split START
my $data = ' a Line of code before split , after the comma selected for a split , line3 , Another Line';
my @values = split(',', $data);
foreach my $val (@values) {
print "$val\n";
}
# SPLIT END
#Chomp START
my @array = ("An\n", " example of separated", " text by commas and \\n next line combined by chomp \n");
print "Before chomp:\n";
print "@array\n";
chomp(@array);
print "After chomp:\n"; # combines the /n next line commands
print "@array\n";
# Chomp END
# For Loop START
my @forlooparray = ("element 1", "element 2", "element 3");
for(my $i=0; $i < 3; $i++)
{
print "@forlooparray[$i] \n"; #show each element in the array starting with @forlooparray[0] then [1] , [2] and print the variable
}
#For loop End
#START input
while (my $text = <STDIN>) {
chomp($text);
print "You entered '$text'\n";
last if ($text eq ''); #exit when don't type anything and press enter
}
#END input
Here is a database Connection with Perl with MySQL
#!"C:\perl64\bin\perl.exe"
use DBI;
#use DBD::mysql;
# HTTP HEADER
print "Content-type: text/html \n\n";
# MYSQL CONFIG VARIABLES
$host = "localhost";
$database = "perl";
$tablename = "perl_table";
$user = "username";
$pw = "password";
# PERL DBI CONNECT()
$driver = "DBI:mysql:database=$database;host=$host";
$dbh = DBI->connect($driver, $user, $pw);
# SELECT - Get Query
# Find all servers with CPU utilization above (>) 90%.
$run_query = $dbh->prepare("SELECT * FROM $tablename WHERE CPU > 90");
# Look where CPU is greater than > 90
$run_query->execute();
#looping and displaying the results
while($result=$run_query->fetchrow_hashref()) {
print "<b>Server_Name:</b> $result->{Server_Name}\n";
print "<b>IP :</b> $result->{IP}\n";
print "<b>Status:</b> $result->{Status}\n";
print "<b>CPU usage above 90%:</b> $result->{CPU}\n";
}
# AVERAGE
# Find the average memory usage for all servers.
$run_query = $dbh->prepare("SELECT avg(memory) as average FROM $tablename");
# AVGerage (of all rows in the column)
$run_query->execute();
#looping and displaying the results
while($result=$run_query->fetchrow_hashref()) {
print "<b>Server_Name:</b> $result->{Server_Name}\n";
print "<b>IP :</b> $result->{IP}\n";
print "<b>Status:</b> $result->{Status}\n";
print "<b>Average memory usage:</b> $result->{average}\n";
}
# Show the server with the most memory utilization.
$run_query = $dbh->prepare("SELECT memory FROM $tablename ORDER BY memory DESC LIMIT 1");
# sort by DESCending order Limit to 1 result
$run_query->execute();
#looping and displaying the results
while($result=$run_query->fetchrow_hashref()) {
print "<b>Server_Name:</b> $result->{Server_Name}\n";
print "<b>IP :</b> $result->{IP}\n";
print "<b>Status:</b> $result->{Status}\n";
print "<b>most memory utilization:</b> $result->{memory}\n";
}
# Show the server with the least amount of disk free.
$run_query = $dbh->prepare("SELECT diskfree FROM $tablename ORDER BY diskfree ASC LIMIT 1");
#Order by column diskfree ASCending Limit to 1 result
$run_query->execute();
#looping and displaying the resulet
while($result=$run_query->fetchrow_hashref()) {
print "<b>Server_Name:</b> $result->{Server_Name}\n";
print "<b>IP :</b> $result->{IP}\n";
print "<b>Status:</b> $result->{Status}\n";
print "<b>most memory utilization:</b> $result->{memory}\n";
}
System
#!"C:\perl64\bin\perl.exe"
#-- calling 'command' with arguments
system("dir c:\\");
Exec
#!"C:\perl64\bin\perl.exe"
exec("dir c:\\");
Back tick commands
#!"C:\perl64\bin\perl.exe"
#-- calling 'command' with arguments
#-- calling with back ticks
$result = `dir c:\\`;
print $result;
READ from a text file
data.txt
Larry larry@example.com 111-1111
Curly curly@example.com 222-2222
Moe moe@example.com 333-3333
parse.pl file you will run
#!/usr/bin/perl
#data.txt
#Larry[TAB button pressed here on keyboard between each field]larry@example.com[TAB button]111-1111
#Curly curly@example.com 222-2222
#Moe moe@example.com 333-3333
open (FILE, 'data.txt');
while (<FILE>) {
chomp;
($name, $email, $phone) = split("\t");
print "Name: $name\n";
print "Email: $email\n";
print "Phone: $phone\n";
print "---------\n";
}
close (FILE);
exit;