Perl Programming – Notes and Stuff ~circa May, 2002 Colin Charles, Scalars · A scalar is a single value · Numbers (evaluated as a double always), strings, references · Scalar variables begin with the $ character · Numbers and strings are converted as and when required - $a = 87 is almost the same as $a = “87” · Strings – not an array of characters, basic data type, no pointers needed · Single-quoted delimited – all characters aren’t interpolated (‘\n’ is backslash followed by n). Except \’ = ‘. And so on. · Double quotes interpolate scalar and array variables. “it’s \$35.50” = it’s $35.50 · Numerically, 7 is not greater than 30. “7” gt “30” is true. Because of the way gt uses ASCII. Must use correct comparison operator. · Scalar values used without being defined contain undef · length $string – returns length. uc/lc/ucfirst/lcfirst – upper/lower/upper_first_char/lower_first_char cases Lists and Arrays · List - an ordered sequence of scalars · Literals enclosed in parentheses (-5.3, $a+10) · Array is a Perl var containig a list. Begins with @ sign. @names, @_, ... · Copying arras is simple. Just assign them! @copy_of_days = @days · Lists cannot hold arrays inside them. · Last element is $#array. · Can have array slices. @days[1] can return a single value of what's in positio 1 in @days · Arrays can be interpolated. $alldayss = "@days"; will print out the days of the week · Braces can be used to disambiguate. ${days}[1]. · print @lines will work. · @sorted_day_names = sort @days · @flip = reverse @flip · push, pop - adds elements/removes elements from the right hand side of array. push can add several items at once · unshift,shift - Adds/removes elements from the left hand side of array. Same syntax as push/shift. · Context (l03,s13). for(i=1,j=1;a[i]==a[j];i++,j++) actually works' · In list context, @elements will receive a three-letter list. In scalar contet, $elements return the first value. · Refer llama3, p51 + slide 15 l03 Regular Expressions • Perl tries to match a regex with $_ • Enclose patterns in slashes ( /pattern/) • First left-most successful match is considered a matchin substring • /abc/ matches abc. /123/ matches the string 123. • /\{moo\}/ - matches {moo} • . - period - matches any character except newline. /d.g/ = dog, dig, d2g, ... • [letters] matches one of the enclosed letters. [a-z] • [^letters] matches any one character except those enclosed in the argument • /\s/ # space - same as /[ \r\t\n\f]/ • /\d/ # digit - same as /[0-9]/ • * means zero or more • + means one or more • ? means zero or one • Multipiers are greedy. ^ forces match at start of string (/^a/) and $ forces match at end of string (/!$/ #matches $_ to end in !) • | separates alternaties (like or) • () parenthesis used for grouping. /(cat|sel)fish/ #matches catfish selfish • Regular expressions are normally case sensitive. Use /a/i (the i switch) to ignore case. • Substitution: s/pattern/replacement • Substitution normally only occurs for first match in a string. Have to use a g modifier to make sure it repeats as oten as needed. s/cat/dog/g - g as in global • =~ (binding operator) - l06,25 • split /pattern/,string - breaks up string into parts separated by patter, returning the parts as a list • join string, list - glues the elements of list together with string.