1 / 29

Programming Language Concepts Perl Scalar Data

Programming Language Concepts Perl Scalar Data. Adapted by Carl Reynolds from materials by Sean P. Strout. What is Scalar Data. Scalar means “one of something” Number – 255 or 3.25e20 String – hello Singular vs. plural Numbers and strings are interchangeable

hbrewer
Télécharger la présentation

Programming Language Concepts Perl Scalar Data

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Programming Language ConceptsPerl Scalar Data Adapted by Carl Reynolds from materials by Sean P. Strout PLC - Perl Scalar Data (v2)

  2. What is Scalar Data • Scalar means “one of something” • Number – 255 or 3.25e20 • String – hello • Singular vs. plural • Numbers and strings are interchangeable • A string is a sequence of characters, but Perl treats it as a single scalar value. PLC - Perl Scalar Data (v2)

  3. Numbers • Internally, Perl computes with double-precision floating-point values • The perl interpreter is (typically) compiled in C • Most modern systems provide for 15 digits of precision in the range of 1e-100 to 1e100 • There are no integer values visible to the programmer • “A literal is the way a value is represented in the source code of the Perl program. A literal is not the result of a calculation or an I/O operation; it’s data written directly to the source code” PLC - Perl Scalar Data (v2)

  4. Floating Point Literals 1.25 255.000 255.0 7.25e45 # 7.25 times 10 to the 45th power -6.5e24 # negative 6.5 times 10 to the 24th -12e-24 # negative 12 times to the the –24th -1.2E-23 # same as the previous PLC - Perl Scalar Data (v2)

  5. Integer Literals 0 2001 -40 255 61298040283768 61_298_040_283_768 # same as previous PLC - Perl Scalar Data (v2)

  6. Non-decimal Integer Literals • These three values are all the same 0377 # 377 octal, or 255 decimal 0xff # FF hex, or 255 decimal 0b11111111 # 255 decimal (perl v5.6+) • Underscores help with readability 0x13770c77 0x1377_0c77 # same as previous (perl v5.6+) PLC - Perl Scalar Data (v2)

  7. Numeric Operators 2 + 3 # 2 plus 3, or 5 5.1 – 2.4 # 5.1 minus 2.4, or 2.7 3 * 12 # 3 times 12 = 36 14 / 2 # 14 divided by 2, or 7 10.2 / 0.3 # 10.2 divided by 0.3, or 34 10 / 3 # 3.33333… 10 % 3 # 10 modulus 3, or 1 10.5 % 3.2 # 10 % 3, or 1 2 ** 3 # 2 to the power of 3, or 8 PLC - Perl Scalar Data (v2)

  8. Strings • Strings are a sequence of zero or more characters • Shortest string is the empty string • Largest string fills all available memory • “Know thy ASCII table” • \75\110\111\119\32\116\104\121\32\65\83\67\73\73\32\116\97\98\108\101 • Perl can handle non printable characters in a string (values outside ASCII 32-126) • This is perfect for manipulating raw binary data like a JPEG or a compiled program PLC - Perl Scalar Data (v2)

  9. Single-Quoted String Literals • A sequence of characters enclosed in a single quote • Any character besides a single quote, ’ , and a backslash, \ , stands for itself ’fred’ # the four characters: f, r, e, d ’’ # the null string ’Don\’t let an apostrophe end this string!’ ’The last char is a backslash: \\’ ’hello\n’ # hello, backslash, n ’hello there’ # hello, newline, there (11 chars) ’\’\\’ # single quote, backslash PLC - Perl Scalar Data (v2)

  10. Double-Quoted String Literals • A sequence of characters enclosed in double quotes • Backslash now takes on the full power to specify a control character, or any character “barney” # same as ‘barney’ “hello world\n” # hello world, and a newline “The last character is a quote: \”” “coke\tsprite” # coke, a tab, and a sprite PLC - Perl Scalar Data (v2)

  11. Double-quoted String Backslash Escapes ConstructMeaning \n Newline \r Return \t Tab \f Formfeed \b Backspace \a Bell \e Escape (ASCII escape character) \007 Any octal value (here 007=bell) \0x7f Any hex ASCII value (here 7f=delete) \cC A control character (here, Ctrl-C) \\ Backslash \” Double quote \l Lowercase next letter \L Lowercase all following letters until \E \u Uppercase next letter \U Uppercase all following letters until \E \Q Quote non-word chars by adding a backslash until \E \E Terminate \L, \U or \Q PLC - Perl Scalar Data (v2)

  12. String Operators • Strings values can be concatenated with the . operator • Neither string is modified, a new result is formed “hello” . “world” # same as “helloworld” “hello” . ’ ’ . “world” # same as “hello world” ’hello world!’ . “\n” # same as “hello world!\n” • The string repetition operator, x “fred” x 3 # is “fredfredfred” “barney” x (1+1) # “barneybarney” 5 x 4 # What is this??? PLC - Perl Scalar Data (v2)

  13. Automatic Conversion • Perl automatically converts numbers  strings based on the operators. “12” * “3” “12fred34” * “ 3” “fred” * 10 “Z” . 5 * 7 • Perl can notify you of suspicious code with: % perl –w my_program • or: #!/usr/bin/perl -w PLC - Perl Scalar Data (v2)

  14. Scalar Variables • A variable is a name for a container that holds one or more values • Scalar variables hold a single scalar value whose value can change throughout execution of the program $my_cool_scalar_variable_that_ends_in_1 All scalar variables begins with the dollar sign symbol The remainder is the perl identifier, which is a letter or underscore, followed by letters, digits or more underscores. PLC - Perl Scalar Data (v2)

  15. Scalar Assignment • Scalar assignment gives a value to a scalar variable $fred = 17 # $fred can legally drink in 4 short years $barney = ’hello’ # give $barney the 5 character string ’hello’ $barney = $fred + 3 # $barney now holds the value 20 $barney = $barney * 2 # $barney now holds the value 40 $barney = fred + 65 # be careful! • Binary Assignment Operators $fred += 5; # $fred = $fred + 5 $barney *= 3; # $barney = $barney * 3 $str .= “ “; # $str = $str . “ “ $fred **= 3; # $fred = $fred ** 3 PLC - Perl Scalar Data (v2)

  16. Output With print • The print operator can take a scalar argument and print it to standard output, without embellishment print “hello world\n”; print “The answer is ”; print 6 * 7; print “.\n”; print “The answer is ”, 6 * 7, “.\n”; As you can imagine, this is not a scalar argument (it's a list! – later…) PLC - Perl Scalar Data (v2)

  17. Scalar/String Interpolation • Double quoted string literals are subject to variable interpolation $meal = “brontosaurus steak”; $barney = “fred ate a $meal” $barney = ’fred ate a ’ . $meal; $barney = “fred ate a $meat”; # what happens here? print “$fred”; # unneeded quote marks print $fred; # the preferred style $fred = ’hello’; print “The name is \$fred.\n”; # prints dollar sign print ’The name is $fred’ . “\n”; # single quoted string # works same as previous PLC - Perl Scalar Data (v2)

  18. Variable Name Delimeter • Perl provides a delimiter for the variable name. Enclose the name of the variable in curly braces $what = “brontosaurus steak”; $n = 3; print “fred ate $n $whats.\n”; # problem print “fred ate $n $what” . “s.\n”; # cumbersome print ‘fred ate ‘ . $n . ‘ ‘ . $what . “s,\n”; # ugh! print “fred ate $n ${what}s.\n”; # easy easy PLC - Perl Scalar Data (v2)

  19. Operator Precedence and Associativity AssociativityOperators left parentheses and arguments to list operators left -> ++ -- (autoincrement, autodecrement) right ** right \ ! ~ + - (unary operators) left =~ !~ left * / % x left + - . (binary operators) left << >> named unary operators (-X filetests, rand) < <= > >= lt le gt ge (the “unequal” ones) == != <=> eq ne cmp (the “equal” ones) left & left | ^ left && left || right ?: (ternary) right = += -= .= (and similar assignment operators) left , => list operators (rightward) right not left and left or xor PLC - Perl Scalar Data (v2)

  20. Operator Precedence and Associativity • Associativity resolves the order of operations when two operators of the same precedence compete for three operands. 4 ** 3 ** 2 # 4 ** (3 ** 2) 72 / 12 / 3 # (72 / 12) / 3 36 / 6 * 3 # (36 / 6) * 3 • Use parentheses if you are unsure (or untrusting!) PLC - Perl Scalar Data (v2)

  21. Comparison Operators • The logical comparison operators returns true or false ComparisonNumericString Equal == eq Not equal != ne Less than < lt Greater than > gt Less than or equal <= le Greater than or equal to >= ge 35 != 30 + 5 # false 35 == 35.0 # true ’35’ eq ’35.0’ # false (why?) ’fred’ lt ’barney’ # false ’fred’ lt ’free’ # true ’fred’ eq “fred” # true ’fred’ eq “Fred” # false ‘ ‘ gt ‘‘ # true PLC - Perl Scalar Data (v2)

  22. The if Control Structure if ($name gt ’fred’) { print “’$name’ comes after ’fred’ in sorted order.\n”; } else { print “’$name’ does not come after ’fred’.\n”; } • Curly braces are required around all conditional statements (unlike C) • The conditional can come afterthe statement, and the parentheses can be omitted { print “’$name’ > ’fred’.\n” } if $name gt ’fred’; • You can reverse the test sense by substituting “unless” for “if” unless($name le ‘fred’){print “’$name’ > ‘fred’.\n”;} PLC - Perl Scalar Data (v2)

  23. Boolean Values • Boolean values may be stored in a scalar $is_bigger = $name gt ’fred’; if ($is_bigger) {…} • Perl has no separate boolean type. The rules: • The special variable undef is false • Zero is false, all other numbers are true • The empty string (’’) is false; all other strings are normally true • Since numbers and strings are equivalent, the string form of zero, ’0’, has the same value as its numeric form: false • Everything else is true • Use the "unary not” operator, !, to get the opposite boolean value if (! $is_bigger ) {…} PLC - Perl Scalar Data (v2)

  24. Getting User Input • Use the line operator <STDIN>in place of a scalar variable to read the next complete line from standard input (a blocking call) • The string value will maintain the newline character! $line = <STDIN>; if ($line eq “\n”) { print “That was just a blank line!\n”; } else { print “That line of input was: $line”; } • It’s often useful to be able to discard the newline character PLC - Perl Scalar Data (v2)

  25. chomp • The chomp operator removes the newline character from a line $text = “a line of text\n”; # or from <STDIN> chomp($text); # bye bye newline • “Anytime you need a variable in Perl, you can use assignment instead” # $text stores chomped string # $removed is the number of characters removed $removed = chomp($text = <STDIN>); • chomp only removes one newline. • If there is no newline, it does nothing and returns 0 PLC - Perl Scalar Data (v2)

  26. while Control Structure • The classic looping structure: $count = 0; while ($count < 10) { $count += 1; print “count is now $count\n”; } • Truth value works the same as with if control structure • Curly braces are required • The conditional expression is evaluated before the first iteration PLC - Perl Scalar Data (v2)

  27. undef • Variables have a special undef value before they are given a value • As a numeric, undef is treated as 0, and • as a string it is treated as the empty string • But, undef is neither a number nor a string, it’s an entirely separate scalar value $n = 1; while ($n < 10) { $sum += $n; # using undef initially as 0 $n += 2; } print “The total was $sum.\n”; $string .= “more text\n”; # using undef as empty string PLC - Perl Scalar Data (v2)

  28. defined • To tell whether a value is undef and not the empty string, use the defined function, which • returns false for undef and • true for everything else $madonna = <STDIN>; if ( defined($madonna) ) { print “The input was $madonna\n”; } else { print “No input available!\n”; } • To force a variable to be undefined: $madonna = undef; # as if it had never been touched PLC - Perl Scalar Data (v2)

  29. Revision History • Revision History • v1.00, 10/6/2003 2:48 PM, sps Initial revision. • v1.01, 1/14/2004 10:54 AM, sps 20032 updates. • v1.02, 4/6/2004 2:54 PM, sps 20033 updates. • v1.03, 4/7/2004 10:47 AM, sps Touched up some examples. -- v2.0, 1/9/2005, chr PLC - Perl Scalar Data (v2)

More Related