Perl - Instructor Insights
Our instructors have real-world experience in the technologies that they teach. Here they share their insights and discoveries on working with each of these technologies.
Convert UNIX files to Windows files
Perl can be used to convert UNIX text files to Windows text files.
Use this perl one-line program to convert Windows to UNIX text files:
perl -pi -e 's/\r\n/\n/g' dosfile.txt
Use a similar program to convert UNIX to Windows text files:
perl -pi -e 's/\n/\r\n/' unixfile.txt
Parenthesis
Use parentheses to indicate precedence, rather than memorizing the
precedence rules.
This is especially important in Perl, as the precedence rules can be tricky:
print (1 + 2) * 3, "\n";
will not compute (1 + 2) * 3, then print the resulting 9 on a line by
itself. Instead, the (1 + 2) will be assumed to indicate a function call
bound to "print". The remaining "* 3" and newline will processed, but will
result in no actions and the results will be ignored.
Lists
Lists are not the same as arrays.
While lists and arrays are often used interchangeably when talking about
Perl, they are not the same. Examples: an array processed in scalar context
will return the number of items in the array, while a list in scalar context
will return the last item in the list; arrays have changeable length, while
lists have a fixed length; variables can be array types, using the '@'
character — they cannot be lists.
|