Programming Language Concepts Perl Scalar Data Adapted by
Programming Language Concepts Perl Scalar Data Adapted by Carl Reynolds from materials by Sean P. Strout 9/17/2020 PLC - Perl Scalar Data (v 2) 1
What is Scalar Data • Scalar means “one of something” – Number – 255 or 3. 25 e 20 – 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. 9/17/2020 PLC - Perl Scalar Data (v 2) 2
Numbers • Internally, Perl computes with double-precision floatingpoint values – The perl interpreter is (typically) compiled in C – Most modern systems provide for 15 digits of precision in the range of 1 e-100 to 1 e 100 • 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” 9/17/2020 PLC - Perl Scalar Data (v 2) 3
Floating Point Literals 1. 25 255. 000 255. 0 7. 25 e 45 -6. 5 e 24 -12 e-24 -1. 2 E-23 9/17/2020 # # 7. 25 times 10 to the 45 th power negative 6. 5 times 10 to the 24 th negative 12 times to the – 24 th same as the previous PLC - Perl Scalar Data (v 2) 4
Integer Literals 0 2001 -40 255 61298040283768 61_298_040_283_768 9/17/2020 # same as previous PLC - Perl Scalar Data (v 2) 5
Non-decimal Integer Literals • These three values are all the same 0377 0 xff 0 b 1111 # 377 octal, or 255 decimal # FF hex, or 255 decimal # 255 decimal (perl v 5. 6+) • Underscores help with readability 0 x 13770 c 77 0 x 1377_0 c 77 9/17/2020 # same as previous (perl v 5. 6+) PLC - Perl Scalar Data (v 2) 6
Numeric Operators 2 + 3 5. 1 – 2. 4 3 * 12 14 / 2 10. 2 / 0. 3 10 / 3 10 % 3 10. 5 % 3. 2 2 ** 3 9/17/2020 # # # # # 2 plus 3, or 5 5. 1 minus 2. 4, or 2. 7 3 times 12 = 36 14 divided by 2, or 7 10. 2 divided by 0. 3, or 34 3. 33333… 10 modulus 3, or 1 10 % 3, or 1 2 to the power of 3, or 8 PLC - Perl Scalar Data (v 2) 7
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” – 751101111193211610412132658367737 3321169798101 • 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 9/17/2020 PLC - Perl Scalar Data (v 2) 8
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: \’ ’hellon’ # hello, backslash, n ’hello there’ # hello, newline, there (11 chars) ’’\’ # single quote, backslash 9/17/2020 PLC - Perl Scalar Data (v 2) 9
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 worldn” # hello world, and a newline “The last character is a quote: ”” “coketsprite” # coke, a tab, and a sprite 9/17/2020 PLC - Perl Scalar Data (v 2) 10