120 likes | 140 Views
Learn the fundamentals of Perl scripting language, including scalar data, operators, string handling, input methods, lists, and arrays. Discover how Perl simplifies programming tasks with its user-friendly syntax. Suitable for beginners.
E N D
Introduction to Perl – Part 1 Timothy Hahn
Overview • Perl is a really easy, simple scripting language • It is very friendly to Unix and other operating systems • VERY VERY SIMPLE TO LEARN!!!
Scalar Data • Perl only has two basic data types: numbers and strings • Like Python and Ruby, but not like C++ and Java, Perl does not need to have these data types defined before (e.g. int number or String word) • You can just call things without initializing. • In Perl, you use “$” in front of these basic variables. • e.g. $word = “String!”; • When you call something that’s not yet been defined, Perl will use “undef” instead of crashing completely. With words it will be a blank “” string and with numbers it will be 0 • In Perl, you can simply call a variable inside a print command
Operators • Except for two special operators, Perl has very standard basic operators: • Assignment: = • Arithmetic: +, -, *, / • String: ., x (See example later) • Comparison:
Operators • Except for two special operators, Perl has very standard basic operators: • Assignment: = • Arithmetic: +, -, *, / • String: ., x (See example later) • Comparison:
String Operators • Before I go into string operators, Perl automatically can convert numbers to strings. So the number 5 can be used with these string operators • “.” is a string operator that appends the left hand string to the right hand string • “x” is a string operator that prints the left hand string by the right hand number
Input • Two Methods to doing input: • <STDIN> and <>
Input Shortcut • Using $_ • Just a faster shortcut
Lists • Lists are placed in ( ) • A list of integers from 1 to 10 can be represented as: • (1,2,3,4,5,6,7,8,9,10) • (1..10) • Useful for things like swaps: • ($a, $b) = ($b, $a);
Arrays • Arrays is a different data type that is created using the @ symbol • For example: @numbers = (1..100); • Accessed with $ before the variable name and [n] right after (n being the index that you want to access in the array) • For example: $numbers[2] • Perl starts these arrays at 0. • Accessing an index that is empty will not crash Perl (Like it did in C++) it will simply return undef (so either 0 or “”)