Introduction to Perl Programming

April 24, 2018 | Author: George | Category: Command Line Interface, Perl, String (Computer Science), Control Flow, Array Data Type
Share Embed Donate


Short Description

Introduction to Perl Programming - perl 5...

Description

Introduction to Perl Programming  ( perl 5 ) Contents  Basics Variables and Operators  Branching   Looping  File Test Operators  Regular Expressions  Input and Output   Processing files mentioned on the Command line Get Filenames  Pipe input and ouput from/to Unix Commands  Execute Unix Commands The Perl built-in Functions  Subroutines  Some of the special Variables Forking   Building Pipes for forked Children  Building a Socket Connecting to another Computer  Get User and Network Information  Arithmetics Formatting Output with "format" Commandline Switches

 Basics Scripts Perl is a script language, which is compiled each time before running. That unix u nix knows that it is a perl script there must be the following header at the topline of o f every perl script: #!/usr/bin/perl where the path to perl has to be correct and the line must not exeed 32 charachters.

Comments and Commands After the header line: #!/usr/bin/perl there are either empty lines with no effect or  command lines or commentary lines. Everything from and behind a "#" up to the end of  the line is comment and has no effect on the program. Commands start with the first non space charachter on a line and end with a "; ";". So one can continue a command over  several lines and terminates it only with the semicolon.

Direct commands and subroutines  Normal commands are executed in the order written in the script. But subroutines can be  placed anywhere and will only be evaluated when called from a normal command line. Perl knows it's a subroutine if it the code is preceded with a "sub" and enclosed in a block  like: sub name { command;}

Other special lines Perl can include other programming code with: require something or with use something.

Quotations Single quote: '' or: q// Double quote: "" or: qq// Quote for execution: `` or: qx// Quote a list of words: ('term1','term2','term3') or: qw/term1 term2 term3/ Quote a quoted string: qq/"$name" is $name/; Quote something wich contains "/": qq!/usr/bin/$file is readdy!;

Scalar and list context

That perl distinguishes between scalar and list context is the big feature, which makes it unique and more useful then most other script languages. A subroutine can return lists and not only scalars like in C. Or an array gives the number  of elements in a scalar context and the elements itself in a list context. The enormous value of that feature should be evident.

Variables and Operators General There are scalar variables, one and two dimensional arrays and associative arrays. Instead of declaring a variable, one precedes it with a special character. $variable is a normal scalar variable. @variable is an array and %variable is an associative array. The user of   perl does not have to distinguish between a number and a string in a variable. Perl switches the type t ype if necessary.

Scalars Fill in a scalar with: $price = 300; $name = "JOHN"; Calculate with it like: $price *= 2; $price = $oldprice * 4; $count++; $worth--; Print out the value of a scalar with: print $price,"\n";

Arrays Fill in a value: $arr[0] = "Fred"; $arr[1] = "John"; Print out this array: print join(' ',@arr),"\n"; If two dimensional: $arr[0][0] = 5; $arr[0][1] = 7;

Hashes (Associative Arrays) Fill in a single element with: $hash{'fred'} = "USA"; $hash{'john'} = "CANADA"; Fill in the entire hash: %a = ( 'r1', 'r2', 'r3', );

or with: %a = (

'this is val of r1', 'this is val of r2', 'this is val of r3',

r1 => 'this is val of r1', r2 => 'this is val of r2', r3 => 'this is val of r3', );

Assignments Put something into a variable with a "=" or with some combined operator which assigns and does something at the same time: $var = "string"; Puts the string into $var  $var = 5; Puts a number into $var  $var .= "string"; Appends string to $var  $var += 5; Adds number to $var  $var *= 5; Multipliy with 5 $var ||= 5; If $var is 0 make it 5 $var x= 3; Make $var to three times $var as string: from a to aaa

Modify and assign with: ($new = $old) =~ s/pattern/replacement/;

Comparisons Compare strings with: eq ne like in: $name eq "mary". Compare numbers with: == != >= right adjustment, @##.## is for numericals and @||| centers.

Command line Switches Show the version number of perl:

perl -v;

Check a new program without runing it:

perl -wc ;

Have an editing command on the command line:

perl -e 'command';

Automatically print while precessing lines:

perl -pe 'command' ;

Remove line endings and add them again:

perl -lpe 'command' ;

Edit a file in place:

perl -i -pe 'command' ;

Autosplit the lines while editing:

perl -a -e 'print if $F[3] =~ /ETH/;' ;

Have an input loop without printing:

perl -ne 'command' ;

View more...

Comments

Copyright ©2017 KUPDF Inc.
SUPPORT KUPDF