Jump to content


Check out our Community Blogs

Register and join over 40,000 other developers!


Recent Status Updates

View All Updates

Photo

Lets Learn Perl - Beginning Requirements


  • Please log in to reply
No replies to this topic

#1 Showstopper

Showstopper

    CC Addict

  • Advanced Member
  • PipPipPipPipPip
  • 145 posts

Posted 14 October 2012 - 01:42 PM

It has been awhile since I have been around the community and even longer since I have given back, so here is my go at a series of Perl lessons for you guys.

NOTE: THIS ONLY CONTAINS SOMEWHAT BORING, BUT RELEVANT INFORMATION AND WILL NOT HAVE YOU CODING YET.

Scalar Data


Numbers:

Scalar is often a number or string. I will be covering them separate though. Also worth noting is that Perl internally computes with double-precision floating point values.( There are no integer values internal to Perl)

Floating Point Literals:

Liters are the way a value is represented in the source of a Perl program. It is not the result of an I/O operation and its directly written within the source.

Ex.

4.56
456.000
456.0
4.56e45 # 4.56 times 10 to the 45th power

Integer Literals:

0
2001
-50


Nondecimal Integer Literals:

All of the below equal the same value in Perl.
0377
0xff
0b11111111

Numeric Operators

Perl provides the normal operators(+-*/) as well as a modulus operator(%) which would be the remainder.

Ex:
11%3 is the remainder of 11/3

Single Quoted Strings:

'Joe'
'Bloe'
' ' #Null string
'This is one backslash \\'
'hello\n' #Hello, followed by a backslash, followed by an N
'Hello
There' #Hello, new line, There


Double Quoted Strings:

The backslash in Double Quoted Strings take on full power to specify certain control characters.

"Joe"
"Hello World\n" #Hello World, followed by a new line
"The last character is a quote mark \" "



Backslash Usage:
\n - Newline
\r - Return
\t - Tab
\f - Form Feed
\b - Backspace
\a - Bell
\e - Escape
\007 - Any Octal Value
\x7f - Any Hex Value
\cC - "Control" Character
\\ - Backslash
\" - Double Quote
\l - Lowercase next letter
\L - Lowercase all following letters until \E
\u - Uppercase next letter
\U - Uppercase all following until \E
\Q - Quote nonword chracters
\E - End \L, \U, \Q


. Operator

Strings can be separated with the . operator

"hello" . "world"; #helloworld
"hello" . ' ' . "world"; #hello world


String Repetition Using X

"joe" x 3; #joejoejoe

Automatic conversion of numbers and strings

If an operator expects a number such as + does, it will see the value as a number. If it expects a string such as the . operator, it will see it as a string.

"11" * "3";#Returns value 33


Scalar Variables

A variable is a "container" that holds one or more values. The name stays, but the value is usually changed throughout your programs.

$Variable;
$This_is_a_variable;
$This_is_another_variable;
$Variable2;

Assigning Scalar Variables


$variable = 1; #give variable the value of 1
$variable2 = 'hello'; #give variable2 the string 'hello'


Binary Assignment

Expressions such as $variable = $variable + 1 happen frequently. In this case we can use binary assignment.

$variable = $variable + 1 #Not using binary assignment
$variable = += 1;
$string .= " ";


Output

Output and Perl can be done by using print.

print "hello world"


Interpolation


When a string is double quoted, it is subject to variable interpolation.

$variable = "Joe";
$variable2 = "This is now $variable"; #This is now Joe

Operator Precedence

Operator precedence determines with operations in a group happen first. An example of this is in general math if you had 11+11*11, do you perform the addition or multiplication first? Obviously we do the multiplication first, then add. This is how Perl does things. If you wanted to have Perl add before, you would need to do something like, (11+11)*11, which would tell Perl, like in general math, to do that first.

While that example is easy, you can start to run into problems with more complex situations. The way to solve this is to go to the Perl Operator Precedence chart.

																		
[i]Operator[/i]										
[i]Associativity[/i]										
[i]What it means[/i]														
->										
left										
Dereference operator (Day 19, "Working with References"														
++ --										
non										
Increment and decrement														
**										
right										
Exponent														
! ~ \ + -										
right										
Logical not, bitwise not, reference (Day 19), unary +, unary -														
=~ !~										
left										
Pattern matching														
* / % x										
left										
Multiplication, division, modulus, string repeat														
+ - .										
left										
Add, subtract, string concatenate														
<< >>										
left										
Bitwise left shift and right shift														
unary operators										
non										
Function-like operators (See today's "Going Deeper" section)														
< > <= >= lt gt le ge										
non										
Tests														
== != <=> eq ne cmp										
non										
More tests (<=> and cmp, Day 8, "Data Manipulation with Lists")														
&										
left										
Bitwise AND														
| ^										
left										
Bitwise OR, bitwise XOR														
&&										
left										
C-style logical AND														
||										
left										
C-style logical OR														
..										
non										
Range operator (Day 4, "Working with Lists and Arrays")														
?:										
right										
Conditional operator (Day 6, "Conditionals and Loops")														
= += -= *= /=, etc.										
right										
Assignment operators														
, =>										
left										
Comma operators (Day 4)														
list operators										
non										
list operators in list context (Day 4)														
not										
right										
Perl logical NOT														
and										
left										
Perl logical AND														
or xor										
left										
Perl logical OR and XOR						

Comparison Operators

For comparing numbers, Perl has the logical comparison operators that remind of your school days: < <= == >= > !=. Each of them will return a true or false value, but we will be going deeper into this later on.




That is all for this one guys. I know this was really boring and it was quickly done, but the real lessons will start on my next post. This is just some basic information that you will need to know.

Cheers.
  • 0




Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download