Download Day 6 - Training

Survey
yes no Was this document useful for you?
   Thank you for your participation!

* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project

Document related concepts
no text concepts found
Transcript
Perl
Day 6
Multiline Strings
Perl supports a mechanism to deal with multiple
lines of text rather than having to add them one
at a time
 Here text (<<)

$LongVar=<<EOF;
This is in the variable now.
So is this.
Woohoo!
EOF
Running other programs
Sometimes in the middle of running one script,
you decide you need to run something else.
 Perl gives us the command:

system(“iis reset”);
It executes the command, then continues on
with your program.
 If you want it to execute the external command,
then stop your current script:

exec(“iis reset”);
Getting back output

Sometimes there is a built in command
that does everything you want, you just
want to run it, and take it’s output:
@Lines=`netstat –an`;
– This runs “netstat –an”, and puts the output
into an array called Lines.
– Note those are backquotes, not regular
quotes.
Sometimes 1D isn’t enough

Imagine you wanted to keep track of multiple
things about multiple people.
– Ask people 3 questions, and store them in memory
 Name
 Age
 Birthday

You would currently have a hard time storing
that information.
– We could have 3 arrays, @names, @ages, @birthdays
– We’d have to assume that index 0 in each is one person,etc.
 What if one person refuses to enter age? Do we put in a 0?
Better solution

I give you multi-dimensional hashes
– $Info{Enda}{Age}=18;
Think of this as a normal hash, that each
value in the hash, happens to be another
hash.
 Because of the extra {}’s you shouldn’t
put these directly into strings. Always
include them with a dot (.)

#!/usr/bin/perl
while(1)
{
print("Please enter name [or type quit to end input]\n");
$Name=<STDIN>;
chomp($Name);
if($Name=~/quit/i)
{ last; }
print("Please enter age\n");
$Age=<STDIN>;
print("Please enter birthday\n");
$Birthday=<STDIN>;
}
$Answers{$Name}{Age}=$Age;
$Answers{$Name}{Birthday}=$Birthday;
@Names=keys %Answers;
$AllNames=join(',',@Names);
while(1)
{
print("We got in info from $AllNames. Who would you like to see? [type quit to end]\n");
$ShowName=<STDIN>;
chomp($ShowName);
if($ShowName=~/quit/i)
{ last; }
}
print("$ShowName is ".$Answers{$ShowName}{Age}." years old, and has a birthday on
".$Answers{$ShowName}{Birthday}."\n");
Assigning 1 has to another

If you want the create a hash, then assign the whole thing to a key
in the other, you must assign a REFERENCE to a hash.
– A reference is done by putting a \ before the %hashname
$Hash1{num1}=17;
$Hash1{num2}=30;
$Hash2{num1}=7;
$Hash2{num2}=8;
$Hash3{first}=\%Hash1;
$Hash3{second}=\%Hash2;
$Keys=join(',',keys %Hash3);
print("Hash3 has keys of $Keys\n");
print($Hash3{first}{num2}."\n");
Getting back an entire hash

If you wish to retrieve more than a single key from a hash, you have to indicate what
you are getting back is a hash:
–
Yea, it’s a little weird, the outer %{ } sort of say “treat this as a hash”
$Hash1{num1}=17;
$Hash1{num2}=30;
$Hash2{num1}=7;
$Hash2{num2}=8;
$Hash3{first}=\%Hash1;
$Hash3{second}=\%Hash2;
%ReturnedHash=%{$Hash3{first}};
foreach $Key (keys %ReturnedHash)
{
print("$Key has value $ReturnedHash{$Key}\n");
}
Arrays can be like onions…

Arrays can also have multiple dimensions:
– Think of a classroom, it has rows of students
and columns of students. Lets record all their
names.
$Names[0][0]=“Jeff”;
$Names[0][1]=“Michael”;
$Names[0][2]=“Wes”;
$Names[1][0]=“Drew”;
$Names[1][1]=“Shawn”;
Crossing the streams

You can have arrays of hashes
– $Array[0]=\%Hash1;
– $Array[1]=\%Hash2;

Or hashes of arrays:
– $Data{$Name}=\@DataPoints;
Modules

Another big strength of Perl is that no
matter what you want to do, someone has
probably written a module to make it
easier.
– http://www.cpan.org
– http://search.cpan.org/
 Search for smtp
– Net::SMTP
– http://search.cpan.org/~gbarr/libnet-1.22/Net/SMTP.pm
Using a module

If you wish to use an extension module you
simply tell perl to use it:
use Net::SMTP;
Most distributions have the most popular
modules included.
 If the module has not been installed on the box,
may have to install

– Active state: open a command prompt and type ppm
– CPAN: perl -MCPAN -e shell
– Or download the module from CPAN and ./configure;
make; make install
Data::Dumper

Earlier we made a hash of hashes, sometimes that gets hard to
visualize:
use Data::Dumper;
print(Dumper(\%Hash3));
That gives us:
$VAR1 = {
'first' => {
'num1' => 17,
'num2' => 30
},
'second' => {
'num1' => 7,
'num2' => 8
}
};

Net::SMTP
I wish to send mail:
use Net::SMTP;

sub SendMessage
{
my ($dstaddress, $message, $subject) = @_;
my $fromuser = “Enda Sullivan <esullivan\@web.com>";
my $smtp = Net::SMTP->new('mailhub.registeredsite.com');
$smtp->mail(‘[email protected]');
$smtp->to($dstaddress);
}
$smtp->data();
$smtp->datasend("From: $fromuser\n");
$smtp->datasend("To: $dstaddress\n");
$smtp->datasend("Subject: $subject\n");
$smtp->datasend("Mime-Version: 1.0\n");
$smtp->datasend("Content-type: text/html; charset=\"iso-8859-1\"\n");
$smtp->datasend("$message\n");
$smtp->dataend();
$smtp->quit;
DBI

Sometimes you want to connect to a database in
Perl
$dbh = DBI->connect("DBI:mysql:database=migration;host=1.2.3.4",“user",“password");

This makes the connection and give you a variable
called $dbh which is the connection.
– Now you can do many things, but the most common are:
$Query=$dbh->selectall_arrayref("select * from table where column=‘hello'");
 That gives you a variable $Query, which is a 2D array of fields
from the DB.
$dbh->do(“insert into table values (‘1’,’bob’)”);
 That allows you to run queries which don’t produce results.
#!/usr/bin/perl
use DBI;
$dbh = DBI->connect("DBI:mysql:database=migration;host=127.0.0.1","mhsignup",“passwd");
$Result=$dbh->selectall_arrayref(“select dbname,server from Durbo_SQL_Migrations");
foreach $Row (@{$Result})
{
$DB=${$Row}[0];
$Server=${$Row}[1];
}
print("Server: $Server, DB: $DB\n");
CGI

Perl can also be used as a replacement for
CGI language (replacement for php)
– Not saying it should be, just that it can be.
use CGI;
my $q = CGI->new;
@values = $q->param('form_field');