您好,欢迎访问三七文档
byDarwin11/09/2007AShallowIntroductiontoPerlandPythonAgenda•Howto‘Taste’aNewLanguage?•ABriefIntro.toPerl•ABriefIntro.toPython•Tech.ProblemsSumup•Questions&AnswersTastingaNewLanguage•WhataretheStrengthAndWeaknessofIt?•HowtoWritea“Hello,World!”Program?•WhatDoesItLooksLikeWhen‘Unarmed’?•WhatSpecial‘Gifts’DoesItHave?•WhatKindof‘HeavyWeapons’DoesItProvide?•IsThereany‘MilitaryPractice’WeCanWatchAndLearn?ABriefIntro.toPerl•“Hello,World!”•Perl’sStrengthandWeakness•The‘Unarmed’SimplePerl•‘Magic’and‘Gifts’ofPerl•Perl’s‘HeavyWeapons’“Hello,World!I’mPerl!”•OurFirstPrograminPerl#!/usr/bin/perl#hello.plprintHello.'World!';•WeCouldLearnFromtheAbove…•ShebangLine•PerlComment•PerlStatement•AndWhat’sMore…•PerlisShortfor“PracticalExtractionandReportLanguage”•PerlCombinesGoodFeaturesofC,sed,awk,Andsh•“TimToday”isActually“ThereIsMoreThanOneWayToDoIt.”Strength&Weakness•WeCanBenefitFromIts...•EasytoLearnAndUse•VeryFlexibleSyntax•QuickImplementation•ExtensiveSupportLibs•We'llBeDrivenCrazyforIts...•OverFlexibility(asshownontherightofthisslice-)•DifficultytoRead,ModifyAndMaintain$a=cpuu\bmftp\bgcff\bs;$b=poui\bfxbm\bm;$c=Ypvublfp\bofepxoqb\bttjubspvoe;$a=~s/\n//;$a=~s/\s+//g;$b=~s/\n//;$b=~s/\s+//g;$c=~s/\n//;$c=~s/\s+//g;$a=~y/b-z/a-z/;$b=~tr/b-z/a-z/;$c=~tr/b-z/a-z/;for($d=100;$d0;$d--){print$d$a$b$d;print$a,\n$c,;print($d-1);print$a$b.\n;}$x=cjc;$y=dobbz;$z=com;print\n;print-$x\@$y.;print$z\n\n;Strength&Weakness•It'sSuitablefor...•SystemAdministration•InternetProgramming•Parsing/ExtractingTextFiles•Playing,Drawing?•ItPerformsPoorlyin...•BuildingBigSoftwareSystem•SolveComplexEngineeringProblemsUnarmedPerl:DataTypes•BasicDataTypesScalars:$a,$b,$var=2342;Array/Lists:@a,@b,@array,$array[3]=3.1415926;Hashes:%a,%b,%hash,$hash{'RT'}=ReutersTechnology;•DerivativeDataStructures@ArrayOfArray=([2,3],[4,5,7],[0]);%HashOfArray=(flintstones=[fred,barney],jetsons=[george,jane,elroy],simpsons=[homer,marge,bart]);%HashOfHash=(flintstones={lead=fred},simpsons={wife=marge,kid=bart,});...Andthere’reevenmoreyoucanguess…UnarmedPerl:DataTypes•EvenMoreElaborateDataStructure…•$rec={TEXT=$string,SEQUENCE=[@some_values],LOOKUP={%some_hash},THATCODE=\&some_function,THISCODE=sub{$_[0]**$_[1]},HANDLE=\*STDOUT,};•ForMoreDetailedInfo.,Referto…:FlowControls•if/elsif/else$age=25,$max=30,$min=18,$name=“Darwin;if($nameeqDarwinand$age$max){printTooold!\n;}elsif($nameeqDarwin&&$age$min){printTooyoung!\n;}else{printJustright!\n;}•for/foreach/while$sum=0;for$i(1..100){$sum+=$i;}print$sum;#Isn’tforeachmoreseriousthanfor?Buttheyarethesame…foreach$person(@names){print$person;}while(DATAFILE){printf%2s:$_,$.;}UnarmedPerl:FlowControls•last(breakfromaloop)@names=('MrsZhao',‘DrQian','MrSun','DrLi');@medics=('DrZhou','DrWu','DrZheng','DrLi');foreach$person(@names){print$person\n;lastif$person=~/Dr/;}LBL:foreach$person(@names){print$person\n;if($person=~/Dr/){foreach$doc(@medics){print\t$doc\n;lastLBLif$doceq$person;}}}UnarmedPerl:Subroutines•sub(shortforsubroutine)$num=10;#sets$numto10&print_results;#printsvariable$num$num++;#self-incrementoperation&print_results;$num*=3;&print_results;subprint_results{print\$numis$num\n;}$str=hello,$str++;#waitaminute,what’sthis?&print_str($str);subprint_str{print\$stris@_\n;}UnarmedPerl:FileAccess•ReadFiles…$infile=stuff.txt;openIN_FILE,$infileordieCannotopen$stuffforread:$!;while(IN_FILE){printLine$.is:$_;}•WriteFiles…$outfile=out.txt;openOUT_FILE,$outfileordieCannotopen$outforwrite:$!;for$i(1..10){printOUT_FILE$i:Thetimeisnow:,scalar(localtime),\n;}PerlGifts:‘$_’andRegularExpression•‘$_’,“OurMagic,YourNightmare”...•‘$_’'sMagicforeach(@names){print$_;}foreach(FILE){print$_;}•RegularExpressioninPerl$_=perlforWin32;#setthestringtobesearchedif($_=~/perl/){printFoundperl\n};#checkif'perl'in$_if(/perl/){printFoundperl\n};#sameastheaboveif(/PeRl/){printFoundPeRl\n};#thiswillfailif(/er/){printFounder\n};#thiswillworkif(/win32/i){printFoundwin32(i)\n};#work(note/i)if(/([a-z]+)\sfor\s([a-z0-9]+)/i)print$2for$1\n;#sowhatthehellisthis?!PerlHeavyWeapons:SupportingLibrariesandCommunities•SupportingLibraries(Justtonameafew…)•Encode,Storable•HTML::TreeBuilder,HTML::Entities,XML::Parser,XML::Simple••CPAN(Ratherthanac-shapedpancake,itis…)•ComprehensivePerlArchiveNetwork,•WhatYouDon’tKnowAboutPerlFAQsis…•ItAlwaysAnswersYourSillyQuestions…•ForaQuickHands-onCourse,Referto…••“Hello,World!”•Strength&Weakness•DataTypes•FlowControls•SupportLibraries/CommunitiesPython:“Hello,World!”•OurFirstPrograminPython#!/usr/bin/python#hello.pyprintHello,World!“•AndWeHaveaScientificCalulatorNow![user@host~]$python2**1010242**1001267650600228229401496703205376LPython:Strength&Weakness•Strength•Simple,ButPowerful•GeneralPurpose,EspeciallyGoodatScientificComputing•Interpreted,NoCompilingTroubles•Object-OrientedLanguage•ExtensiveSupportingLibraries•Weakness•Weird…?Can
本文标题:[public]-A-Shallow-Introduction-to-Perl-and-Python
链接地址:https://www.777doc.com/doc-5751475 .html