Title: Whats a Scalar
1Whats a Scalar?
- Simplest kind of data Perl manipulates
- Either a number or a string of characters (Perl
uses them interchangeably) - Can use operators on scalars, usually generating
another scalar - Can store scalar value into scalar variable
2Scalar Variables ()
- To assign into a scalar variable, use a sign
- foo 27 a scalar number
- bar 'Joel Grow' a scalar string
3Numbers
- 3
- -9000000000000
- 3.1415926536
- 3.46e10
- For the C geeks/curious really all are
internally double precision floats
4Numerical Operators
- 2 3
- 5.1 - 2
- 3 12
- 10.2 / 3
- 2 3
- 10 3
5Strings
- Sequences of 0 or more characters
- ""
- 'Joel'
- "This is a long string with numbers 8 1 33 and
!_at__at_ characters" - Any character (8 bit, 0 thru 256)
- Not just ASCII 32 thru ASCII 126
- Can be single quoted or double quoted
6Single-Quoted Literal Strings
- Does NOT do variable interpolation--treats the
string as literal - 'camel' five chars, 'c', ..., 'l'
- 'don\'t' includes single quote char
- '' null string, no chars
- 'a\\b' 'a', '\', 'b'
7Double Quoted Literal Strings
- DOES do variable interpolation
- backslashes are special
- "big camel\n" 2 words and a newline
- "bond \007" 007 is bell char in octal
- "coke\tsprite" tab between words
- "\cGding" bell char (ctrl-C) and ding
- "a\\b" 'a', '\', 'b'
- "c\temp" what's this?
- "hello foo\n" replaces foo with it's value
8Operators for Strings
- "tall" . " turtle" concatenation
- 'what "color" is that?' . "\n"
- "a" . " " . "b"
- a eq 'bcd'
- f ne g
- g gt "mcgd"
- jake lt fred
- a le b
- c ge d
- (32) x 4 is "5555
- "-" x 50 is what?
9Numeric vs String comparisons...
- Context is very important in comparisons!
- a 5
- b 16
- c 'joelg'
- d 'joel'
- if ( a lt b ) numeric, true
- if ( a lt b ) string, false
- if ( c gt d ) string, true
- if ( c gt d ) numeric, false
10More on numeric vs. string comparisons
- Here's a common gotcha, using instead
- of eq, (or vice versa)
- a 5
- b 16
- c 'joelg'
- d 'joel'
- if ( c d ) numeric, true!
- if ( c eq d ) string, false
11Alternative Quotes
- Can use q like single quotes and qq like
double quotes - you can actually use any matchable delimiter (
) ltgt - qhello 'world'!
- qqhello foobar!
- qA 'starring' \role\
- qqDon't "quote" me!
- qltDon't "quote" me!gt
12Assigning into a variable
- one 1
- two 2.0
- first_name "Joel"
- last_name 'Grow'
- another_one one
- copy_last_name last_name
13Controlling Case examples
- big uc('hello') HELLO
- little lc('WorlD') world
- big "\Uhello" HELLO
- big "\uhELLo" HELLo
- little "\LWorlD" world
- little "\lwORLD" wORLD
- beast "dromedaRY"
- capitalize ucfirst(beast) DromedaRY
- capitalize "\u\Lbeast" Dromedary
14Here Documents
- Can print multi-line string easily
- print ltltEnd_of_HTML
- ltHTMLgt
- ltBODYgt
- ltTABLEgtltTRgtltTDgtHello world!lt/TDgtlt/TRgt
- lt/TABLEgt
- lt/BODYgt
- lt/HTMLgt
- End_of_HTML
15Scalar Operators and Functions
- a 4
- b a 3
- b b 2
- d (c 4) assign 4 to c and d
- d c 4 same thing
- b 4 (a 3) what is a? b?
- Note, just because you can do something in
- Perl doesn't always mean you should. That
- last line is not very obvious to the code
- reader.
16Binary Assignment Operators
- a 3 same as a a 3
- b - 1 b b - 1
- d / 3 d d / 3
- r 4 r r 4
- b (a 4) valid, but weird.
- Please don't do this
17More binary assignment
- concatenate
- str . 'foobar'
- same as str str . 'foobar'
18Autoincrement/autodecrement
- means 'add one' -- means 'subtract one'
- a 2
- a same as a a 1, a now equals 3
- pre and post increment
- print a prints 3, changes a to 4
- print a prints 5, a now 5
- print --a prints 4, a now 4
- print a-- prints 4, a now 3
19Scalar Interpolation
- a "joel"
- this will print "hi joel!" and a newline
- b "hi a!\n"
- c "no such variable foo\n"
- if foo is undefined then c is
- "no such variable \n"
20More scalar interpolation
- x 'fred'
- y "hey x" y is 'hey fred'
- if you don't want interpolation
- z "hey \x"
- z is what?
21More scalar Interpolation
- we want to assign "It's payday" to barney
- fred "pay"
- fredday "Monday"
- barney "It's fredday!"
- what is barney?
22More scalar interpolation
- fred "pay"
- fredday "Monday"
- use if needed
- barney "It's fredday!" Its payday!
- or do this
- barney "It's fred"."day!"
23Chop
- chop removes (and returns) the last
- character in a string.
- x "hello world"
- chop(x) x is now "hello worl"
- last_char chop(x) what is last_char?
- x chop(x) replaces x with its last
- character. weird but valid
24Chomp
- var "abc\n"
- chomp(var) var becomes "abc"
- chomp only removes the "record separator
- character", usually a newline.
- It's a safe chop.
- chomp is much more common than chop.
- you will probably use chomp a lot and chop
- very little, if ever
25Scalar ltSTDINgt
- Reads input from the user
- (on the STDIN filehandle--more on filehandles in
a few weeks) - x ltSTDINgt wait for user to type
- something...
- chomp (x) get rid of newline
- or, do above in one step
- chomp (x ltSTDINgt)
- print "You just typed 'x'\n"
26Scalars in Action
my name "Joel Grow" my email 'joelg' my
age 28 my GPA 3.72 if ( GPA gt 3 )
print "Good job name!\n" print "I'll send
email to email.\n" else print "You need
to study more name!\n"
27Where do we go from here?
- Arrays--a big ol' list of scalars!