An Introduction to Perl – Part IScalar DataScalar Data (cont’d.)VariablesOperatorsOperators (cont’d.)Automatic ConversionStandard Input/OutputControl Structures: if-elseControl Structures: whileLists and ArraysLists and Arrays(cont’d.)Questions?RefrencesEmily GoldbergScalar – holds one piece of data•Numbers:Floating-Point Integer Non-decimal3.11023 -12 0377 octal-2.7e32 3_814_524_705 0xff hex0b11111111 binary•Strings:Single-QuotedDouble-Quoted'hello\nthere' hello\nthere “hello\nthere” hellothere$string="world"; print "hi $string \n!"; hi world!Use $ prefix to denote a variable•Ex.- $_points=5; $name2=‘Larry’;Variable names can contain letters, numbers, and underscores, but may NOT begin with a number.Number OperatorsOperation Operator ExampleAdd +Subtract -Multiply *Divide /Modulus % 10.5%3.2 = 1Exponentiation ** 2**3 = 8Assign =, *=. +=Comparison ==, !=, <,>,<=,>=String OperatorsOperation Operator ExampleConcatenation . "hello"."world" helloworldRepetition x “hello”x3 hellohellohelloComparison eq, ne, lt, gt, le, ge Assignment =, *=. +=, .= $name2.=‘ Lee’larry LeeOperations Results"hello“ . 3 * 5 “hello15"“12” * “3” 36"12fred34" * " 3" 3612 - ”fred” 12Output•Ex. - print "The answer is ", 6 * 7, ".\n";Input•Ex. - $line=<STDIN>;•Remove “\n”- chomp($line);if ($language eq “Perl”){ print “Always use curly braces!” ;}else{ print “ Who knows?!”;}while(defined($line=<STDIN>)){chomp($line); print “This line was: $line \n”; }while(<>){ chomp; print “This line was: $_ \n”; }A list is an ordered collection of scalars. An array is a variable that contains a list.A list with five elements@numbers=(1,2,3,4);@numbers=(1..4);numbers[0]=‘one’;print $numbers[$#numbers]; $a=pop( @numbers);push( @numbers, $a+3);Learning Perl by Randal Schwartz and Tom Phoenix (Via Drexel Library electronic resources)“Perl Basics”
View Full Document