A scalar in Perl is any one value: number, string or reference.
Stored in scalar variable, $x or $name or $number or $really_big_variable_123.
Valid scalar names: start with letter, followed by letter/number/underscore for as many as you want (up to 255). Case sensitive.
Always refered to with the $, unlike shell/Tcl which don't always have it there.
Later will learn about arrays, hashes, filehandles, subroutines, etc. All have separate namespace, can have scaler and hash and subroutine all with same name, but probably not the best idea.
Assignment:$x = 7; # x is now 7 $x = 4 + $x; # x is now 11 $y = $x = 9; # x and y now 9 $y = ($x = 9); # same as above
All stored as doubles internally.
$n = 7; $n = 97.3; $n = 7.6e23; $n = -17; $n = 0777; # octal $n = 0xbeef; # hex
Usual set of C-like operators to play with numbers.
$x = $y + 7; $x *= 3; $x++;
Use parenthesis to alter order of operations.
Comparisons:== : equal != : not equal < : less than > : greater than <= : less than or equal >= : greater than or equal
Sequence of 8-bit values, not limited to printable ASCII, can have embedded NULL (0x00) values.
Single-Quoted:$str = 'hi'; $str = 'hi there\n'; # \n literal, not a newline $str = 'hi \'there\''; # use \ to get a single quote $str = '\\'; # use \\ to get one \Double-Quoted:
$str = "hi there"; $str = "hello\n"; # now we get a newline
Double-quotes strings allow variable interpolation (substitution).
Lots of backslashed special chars, \n = newline, \t = tab, \a = bell, etc.
Special ones:\l = lowercase next letter \L = lowercase all letters following until \E \u = upcase next letter \U = upcase all letters following until \E \Q = Backslash-quote everything following until \E \E = terminate \U \L or \Q $str = "\ugreg"; # str now has Greg $str = "\Ugr\Eeg"; # str now has GReg
The dot "." is the string concatenation operator.
$str = "greg" . " " . "long"; # str now "greg long" (minus quotes) $str = $str . " " . "is cool"; # now "greg long is cool" $str .= ", not!"; # now an insult
The "x" means repeat previous string.
"greg" x 4 # means "greggreggreggreg" " " x 13 # 13 spaces $str x= 2; # double the string
Comparisons:
eq : equal ne : not equal lt : less than gt : greater than le : less than or equal ge : greater than or equal (note opposite from shell)
Perl good at guessing what you mean. If you use a string value in a numeric operation, it will automatically convert string to a number to do it. Throws away stuff it doesn't recognize as numeric from end of string. Things entirely non-numeric get converted to zero.
Same thing the other way, numbers or numeric operations used near a string operation get converted as expected.
$str = "hi " . (1 + 2); # str now "hi 3"
chop function removes last char from string, chomp removes last char if and only if it is a newline char.
$str = "greg\n"; chop($str); # str now "greg" chop($str); # str now "gre" $other = chop($str); # str now "gr", other now "e", returns chop'd letter $str = "long\n"; chomp($str); # str now "long" chomp($str); # str still "long"
By default both of them operate on magical "$_".
length - works on any scalar, but typically used with strings. Returns number of bytes in the scalar (strlen()).
Have index function to find location of a substring in a string, and substr function to find/replace substrings in a string.
#!/usr/local/bin/perl -w $str = "UMBC is a cool school"; $idx = index $str, "ool"; print "ool found at: $idx\n"; # 11, zero-based counting $idx = index $str, "fool"; print "fool found at: $idx\n"; # -1, not found $newstr = substr $str, 11; # string, start print "substring is: $newstr\n"; # ool school $newstr = substr $str, 11, 3; # string, start, length print "substring is: $newstr\n"; # ool $newstr = substr $str, 11, -3; # string, start, chop 3 print "substring is: $newstr\n"; # ool sch
Perl uses the value "undef" to mark variables that are not defined, or for operations that are returning no real value (EOF, "false", etc).
You can assign undef to something to make it undefined.
You can test something with "defined" function.
undef serves as "false" in tests.
#!/usr/local/bin/perl -w
$x = 7;
print "\$x is $x\n";
$x = undef;
print "\$x is $x\n"; # prints "$x is", -w yells at you
if ($x) { # not really what you want to do (what about $x = 0 ?)
print "x is defined\n";
}
else {
print "x is not defined\n";
}
if (defined($x)) { # better
print "I've got an x\n";
}
<STDIN> will read a line from standard input.
$a = <STDIN>; # a now is whatever line user typed, or redirected in
print function will print whatever you tell it to, to stdout by default.
print "hi";
print "hi" . " " . "there";
print "hi", " ", "there"; # a list of values
print("hi"); # optional parens
Also have printf, sprintf for more control over printing.
Perl provides alternative forms for various quoting methods, choose whichever one suits the situation you are in and minimizes the backslashing of special characters.
Single Quotes
$str = 'the value of $x is $x'; # no interpolation $str = q/the value of $x is $x/; # same $str = q#the value of $x is $x#; # same
Double Quotes
$str = "the value of \$x is $x"; # interpolation done $str = qq/the value of \$x is $x/; # same $str = qq#the value of \$x is $x#; # same
Command Execution
$str = `date +%S`; # execute command, vars interpolated $str = qx/date +%S/; # same $str = qx#date +%S#; # same
Word Lists
@words = ("Hi", "there"); # build list of quoted words
@words = qw(Hi there); # same
@words = qw#Hi there#; # same