Why Use Perl Scripts? - Department of Computer Science ...

whooploafSoftware and s/w Development

Dec 13, 2013 (3 years and 10 months ago)

81 views

CS342:Object-OrientedSoftwareDevelopmentLab
Perl
DavidL.Levine
ChristopherD.Gill
DepartmentofComputerScience
WashingtonUniversity,St.Louis
levine,cdgill@cs.wustl.edu
http://classes.cec.wustl.edu/
￿
cs342/
CS342:OOSoftwareDevelopmentLabToolsandTechniques
WhyUsePerlScripts?
￿
PracticalExtractionandReportLanguage
￿
Toautomaterepetitiveand/orcomplextasks
￿
PortabletoallUnixsystems,NT,
etc.
.
￿
WhyPerl?
MuchmorecapableandpowerfulthanBourneshell ￿
Highlevel,interpreted
Includesfeaturescommontomanyprogramminglanguages ￿
Lowlevel,accesstopowerfulbuilt-inandsystemfunctions
Copyright
c
￿
1997-2000Dept.ofComputerScience,WashingtonUniversity1
CS342:OOSoftwareDevelopmentLabToolsandTechniques
PerlStrengths
￿
Textprocessing
ProvidesfunctionalityofUnixawk,sed,tr,
etc.
.
Regularexpressions
￿
Reportgeneration
￿
Scripting
Full-featuredlanguage
Andthensome:thereareusuallymanywaystodosomething
￿
Rapidprototyping
Weaklytyped
Interpreted,though
very
fast
Supportsobjects
GUIinterfacesareavailable
Copyright
c
￿
1997-2000Dept.ofComputerScience,WashingtonUniversity2
CS342:OOSoftwareDevelopmentLabToolsandTechniques
PerlWeaknesses
￿
Uglysyntax
LikeC++,thoughitcleansupafewthings.Forexample,if
statementsmustalwayshave{}aroundtheirbodies.That
avoidssurprisesfromstatementslike:
if(x==0)//NotvalidPerlsyntax!
if(y==1)
y1();
else
x1();//x1()getscalledonlyif
//x==0andy!=1!!!!
￿
Write-Once
Weaklytyped
Manywaystodosomething
Copyright
c
￿
1997-2000Dept.ofComputerScience,WashingtonUniversity3
CS342:OOSoftwareDevelopmentLabToolsandTechniques
PerlDataTypes
￿
Scalar
Conventionalscalarvariables,
e.g.
,
$x=33;
$s='foo';
￿
Array
Arraysofanytype,
e.g.
,
$x[3]=33;####Setthirdelement.
@x=();####Removeallelements.
￿
Hash
Hashtable,
e.g.
,
$map{'foo'}=$x;####Map'foo'tothecontentsof$x
.
%map=();####Removeallelements.
Copyright
c
￿
1997-2000Dept.ofComputerScience,WashingtonUniversity4
CS342:OOSoftwareDevelopmentLabToolsandTechniques
PerlRegularExpressions
￿
Search,
e.g.
,majordomoconglter:
#taboo_headers[regexp_array](undef)<resend>
#Ifanyoftheheadersmatchesoneoftheseregexps,
#thenthemessagewillbebouncedforreview.
taboo_headers<<END
/From:.*postmaster/i
/From:.*news\.blue-sky\.com$/i
/From:abuse\@zNET\.com$/i
/Precedence:bulk$/i
/Subject:.*Articlerejectednull/i
/Subject:.*Automaticreply$/i
/Subject:.*undeliverable/i
/To:\s*undisclosed-recipients/i
END
Copyright
c
￿
1997-2000Dept.ofComputerScience,WashingtonUniversity5
CS342:OOSoftwareDevelopmentLabToolsandTechniques
PerlRegularExpressions,(cont'd)
￿
Searchandreplace,
e.g.
,turnaPostScriptleupsidedown:
perl-pi-e\
's%/landplus90{false}%/landplus90{true}%g;'\
file[...]
Copyright
c
￿
1997-2000Dept.ofComputerScience,WashingtonUniversity6
CS342:OOSoftwareDevelopmentLabToolsandTechniques
PerlExample:Hostname/AddressLookup
####IPnameoraddress?
@ip_addr=($host=m/(\d+)\.(\d+)\.(\d+)\.(\d+)$/o);
if(@ip_addr){
($hostname,$junk,$junk,$junk,@hostaddrs)=
gethostbyaddr(pack('C4',@ip_addr),&AF_INET);
}else{
($hostname,$junk,$junk,$junk,@hostaddrs)=
gethostbyname($host);
}
foreach$addr(@hostaddrs){
$ip_addr=join('.',unpack(C4,$addr));
print"$hostname($ip_addr)\n";
}
Copyright
c
￿
1997-2000Dept.ofComputerScience,WashingtonUniversity7
CS342:OOSoftwareDevelopmentLabToolsandTechniques
PerlFoundations
￿
Helptheprogrammer.
It'softeneasiertodealwithweaklytypedchunksofdatathan
integers,characterarrays,
etc.
.
￿
Designforchange.
Forexample,newkeywordscanbeaddedtoPerlbecause
variablesarespeciallyidentied.
￿
Bepostmodern.
Differentwaysofexpressionareusefulfordifferentproblemsand
differentproblemsolvers.
￿
Perliscomposedofequalpartscomputerscience,linguistics,art,
andcommonsense.(Notethatcomputerscienceisoutnumbered
three-to-one.)
Copyright
c
￿
1997-2000Dept.ofComputerScience,WashingtonUniversity8
CS342:OOSoftwareDevelopmentLabToolsandTechniques
ForFurtherInformation
￿
manperl,especially:
manperldata
manperlfunc
manperlre
manperlop
manperlfaq
￿
O'ReillyCamelbook
￿
comp.lang.perlnewsgroup
￿
http://www.perl.com/perl
￿
http://www.plover.com/mjd/perl/IAQ/IAQlist.html
(InfrequentlyAskedQuestions)
Copyright
c
￿
1997-2000Dept.ofComputerScience,WashingtonUniversity9