| Perl notes |
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
Perl stands for Practical Extraction and Report Language. It was designed by Larry Wall. Perl is an interpretive language. All you need to do is create the program with a text editor mark it as executable and runt it, Perl interpreter takes care of the rest. The first line of a program could be: #!/usr/local/bin/perl # is used for comments, but #! is special. Entering a string and printing it: $text = <STDIN>; print "You just entered $text"; chop ($text); It removes the last character from the string. It's used to remove the new-line character that is added in a <STDIN> input. Conditional Statements: if ($price)>100 { print "too expensive\n"; } else { print "ok, I buy it"; } if ($price==66){ print "its a 66"; } elsif ($price==77){ print "its a 77"; } else { print "not a 66 nor a 77"; } Loops while ($number < 5) { print "the number is low"; $number++; } print "now the number is 5"; The while command loops while it's conditional expression is true. The until command loops as long as it's conditional expression is false. until ($number<5){ } Hexadecimal Notation and others. $value = 0xa; # it's an hexadecimal value. Special arithmetic operators Exponentiation: $y= 2 ** 3; # y is 8 Remainder operator: $y= 21 % 4; # y is 1 String comparison:
String Operators Concatenation . operator: $newstring = "first part" . " second part"; # $newstring is "first part second part" String Repetition x operator: $newstring = "repeat me " x 3; # newstring is "repeat me repeat me repeat me" Concatenation and assigment: $newstring = "first"; $newstring .= "second"; # $newstring = "firstsecond" Conditional operator: $a = $var == 1 ? 4 : 6; # if $var is equal to 1 then a$ = 4 else a$=6 Random numbers. srand(); #initializes the random number generator $randomnumber = int (rand(10)); #genererates a random number between 0 and 9 In this example $number will be a random number between 0 and 10. srand; $number=rand(10); To initializae the random number generator you must use srand. Arrays @array = (2,5,10,30); print $array[0]; #will print: 2 If you assign a value to a higher element, then elements between contains null string. $array[6] = 88; now the array values are: (2,5,10,30,"","",88) Note: Things that reference one value (such as scalar variables and array elements) must start with a $ instead of a @ List Ranges: To assign the numbers 1 to 50 to an array, use the list-range operator, which is .. @list = (1..50); or also: @list = (1,2,3,4,5..47,48,49,50); you can use it on strings: @stringlist = ("aa".."ad"); # stringlist is now: ("aa","ab","ac","ad"); it's posible to copy one array to another easily: @list = @list2; and to assign to multiple arrays ina one statement: @list = @list2 = (1,3,5,7); Using arrays inside lists: @list = (2,4,6); @list2 = (1,@list,7,8,9); # list2 is now: 1,2,4,6,7,8,9 Length of an array: @list = (1,3,5) $var = @list; # $var is now 3, because @list have 3 elements. Subarrays: You can assing to an array a slice of other array: @array = (4,5,6,7,8,9); @subarray = $array[0,2]; # subarray is now: (4,6); Assigning to Array slices: You can use this notation to fill an array: @array[0,1] = ("hi", "there"); @array[2..5] = (22,44,55,88); Entering data into an array. The following code let's you input data into an array until ctrl-D is pressed, and then prints it on the screen. @array = <STDIN> print @array; Sort an Array. @array = ( "this" , "is","Perl"); @array2 = sort (@array); #@array2 is now ("is","Perl", "this"); Reverse an Array. @array = ("Hello","programmers","I am", "here"); @array2 = reverse (@array); # now array2 is ("here","I am","programmers","Hello") Using chop on array variables. @array = ("milk","water","wine"); chop (@array); # Now array is ("mil","wate","win") Joining strings or a list. @array = ("hi", "there"); $string = join ("::","words","to","join",@array); # $string is now: "words::to::join::hi::there" The first parameter on the join command is the string to be put between the others. Spit a string into a list. $string ="words::to::join::hi::there"; @array = split (/::/, $string); This puts the words on the string on every @array position. And splits it by the string :: The string /::/ is a regular expresion pattern. It's what you are looking for match on the split. Files Opening a file: open (FILE1, "document.txt"); This statement opens the file document.txt and associate it with the file variable FILE1 There are three file access modes available in Perl: read mode (it's the default behaviour) write mode (put a > character in front of the filename) append mode (put two > characters in front of the filename) open (OUTFILE, ">document.txt"); # this will allow you to write into the file. When you open a file for writing, any existing data on the file is destroyed, so use it only the first time you want to write on that file. You cannot read and write to the same file at the same time. When you open a file in append mode, the data inside are not destroyed, but you only can write to it. you can not read. open (FILE,"document.txt"); $line = <FILE>; # $line contains the first line of document.txt unless (open (FILE,"book.txt")) { die ("cannot open input file\n"); } # die terminates the program and shows the passed message on the screen. To read an entire file and store it into an array: @input = <FILE>; # every element of the array contains now a line of text from file. Example: Printing all the lines in a text file. unless (open (FILE1,"/usr/share/games/fortune/kernelnewbies")){ die ("Can not find this file, good bye\n"); } $line=<FILE1>; while ($line ne ""){ #when $line is not equal to "" it means it's EOF printf $line; $line=<FILE1>; } close (FILE1); Writing to a file: open (FILE, ">document.txt"); print FILE ("this is a line.\n"); Redirecting Standard Input and Standard Output. perl myprogram <input.txt >output.txt now all the <STDIN> and <STDOUT> will refer to the file assigned Closing a file. close (FILE); File test operators. If you want to know the status of a file, there are some operators to use in Perl. if (-e "/home/david/file1.txt") print "the file exists"; Some of them are:
Parameters in the command line program file1.txt file2.txt file3.txt @array = <> ; now array contains all the lines of the three files. <> is a special operator that means to do the task with all files passed as a parameter There are an special array used for command line arguments, its called @ARGV @ARGV contains ("file1.txt","file2.txt","file3.txt") So the valueo of $ARGV[1] is file2.txt Single Line Conditional Statements. This is another way to write conditional statements if it's only 1ine. print ("this is two"\n) if ($var==2); Loops, for. It's like C. for ($a=1;$a<5;$a++){ print "$a"; } Loops do. It' will do it one time sure, and must evaluate expression to do it more. It's used with "while" or "until" do { your code } while or until (expression); Loops, foreach foreach $b (@words) { if ($b eq "horse") { print "found the word 'horse'\n"); } } The $b variable is used only in the foreach, then it will be set to it's lastest value before the loop. Terminate the loop. You can use the "last" statement with a single line conditional to terminate the loop last if ($var == 3); To start the next iteration of a loop, you can use: next; It will bypass this loop and continue to iterate. redo; Will do this iteration again. Subroutines. To call a subroutine put an & before it's name. Example #!/usr/bin/perl &helloworld; sub helloword { print "hello\n"; } The last evaluated expression in the subroutine is the returned value. So if the last line in the subroutine says: $a=15; then the returned value will be 15 or $a. A more readable way is to use the statement: return (valueyouwant); Local variables in subroutines. my statement, defines variables that exist only inside a subroutine. local statement defines variables thta do not exist inside the main program, but inside the subroutine and any subroutines called by the subroutine. The only difference between my and local is that variables created with my are not known outside the subroutine. Example: my ($a, $b, $bar, $foo, @array, @anotherarray); my ($a)=55; #also initializes the variable with the number 55 Passing values to a subroutine. &count_total (5,8,3); #we call here the subroutine with 3 parameters sub count_total { my ($number1, $number2, $number3) = @_; $total=$number1+$number2+$number3; return ($total); } @_ is created always then a subroutine is called with arguments, so @_ contains the list (5,8,3) $_[0] also refers to the first element of the array variable @_, also does $_[1] to the second, and so on. Passing arrays by name using aliases. &subcalculate(@array); When this subroutine is called, the list stored in the array is copied to the variable @_ defined in the subroutine, when the array is large this could be slow. We can call the subroutine passing it's parameters by reference using the *. &subcalculate(*array); We are passing the parameter by reference, pointing to the memory address where the array is, so changing the array values inside the subroutine, will change also in the main program where it was called from. To colled the data inside the array use this syntax: subcalculate { my (*thearray) = @_; } Passing arrays by name using aliases allows you to pass more than one list to a subroutine: &thesubroutine (*array1, *array2) Special subroutines. Perl 5 defines three subroutines that are executed at specific times. BEGIN called when your program starts runnig END called when your program terminates AUTOLOAD when your program can not find the subroutine it's supposed to execute. Associative arrays. Associative arrays have no index and to distinguish them from an ordinary array variable, Perl uses the % character instead of the @ $fruit{"apple"} = 1; The element apple is just created, and the value 1 is assigned to it. @fruit is the array. This arrays are stored in random order. You can use the statement foreach to loop in the array. To create an associative array and assign values to them, use: %fruit = ("apples", 3, "oranges",7,"bananas",9); a more readable way to do the same is: %fruit = ("apples" => 3, "oranges" => 7, "bananas" =>9); To delete an element of the associative array: delete($fruit{"orange"}); The "key" function, retrieves a list of the elements without the values. Example %fruit = ("apples", 7, "bananas",9); @fruittypes= keys(%fruit); # the array @fruittypes is now: ("apples","bananas"); the function sort can be used to retieve the list in alphabetical order. Remember that the list is in random order. The "values" functions, retrieves the values without the elements @fruittypes=values(%fruit); # the array is now: (7,9) Looping using an associative array. %distances = ("sevilla", 1200,"barcelona",12,"girona", 17); foreach $element (keys(%distances)){ #code goes here } # $element is a variable only used for the loop, like variables used in for loops. The each statement. while (($city,$km) = each(%records)) { #code goes here } # Every time the each statement is reached, two values are retrieved from the array Creating data structures using associative arrays. To simulate a wide variety of data structures found in high-level programming languages, you can use associative arrays. Linked Lists. Each element in the list is linked to the next element. Example: %words = ("abel","baker","baker","charlie","charlie","delta",""); $header ="abel"; Structures. In C language you could do this: struct { int filed1; int field2; int field3; } my structvar; To simulate this using an associative array, define an associative array of three elements, and set the subscripts for these elements to field1,field2,field3. Example: %mystructvar = ("field1","", "field2","","field3",""); # the initial values are the null string. To assign a value of an element do like this: $mystructvar{"field1"} = 23; Trees. A tree is similar to a linked list with the difference that each element of a tree points to more than one other element. The simplest tree is a binary tree, where each element points to two children of its own. An elegant solution is that each parent have to elements. If the father is alpha and the sons are beta and gamma. alphaleft, alpharight are the parent and alphaleft will point to beta, alpharight will point to gamma. So there will be betaleft and betaright to point to its sons. Databases. A database could also be simulated. Example: A students databasae: name, surname, sex,yearborn This is done by appending to the students code the fileds. $students{"001"."NAME"}="john"; $students{"001"."SURNAME"}="smith"; #so we have 001NAME element with value="john", and 001SURNAME element with value="smith" Text output format. In Perl you can define print formats, the syntax is: format yourformatname = linesofoutput . The line with a single dot character an it's the end of the statement. This lines can be anywhere on your program, but usually are placed at the beginning or the end. Displaying a print format. To display output using a print format, you need: Set the system variable $~ to the format you want to use Call the built-in function write. Example: $a=44;$b=99; $~ = "MYFORMAT"; write; format MYFORMAT = ========================== The text I want to display goes here @<<< @<<<< $a,$b ========================== . Value-field format. This are the valid value-field formats to use with the write command.
The @ character ies the line-fill character and text formatting is not performed on its position. This character is included when counting the number of characters in the value field, for example @>>> is 4 characters long. Any floating-point number you print is rounded up then necessary, so 12.999 in the value field @#.##, appears as 13.00 If a string countains a new line character, the only way to display it all is to use the @* multiline value field, else only text before the new line character will be displayed. To write to other output files, the simplest way is to pass the file to write as an argument to the function write. Ex: write (MYFILE); But the $~ system variable, only works with the default file variable, that's the file variable to which write sends output. To change the default file variable use the built-in function select. Ex: select (MYFILE); it will set the default file variable to use when writing. select (MYFILE); $~ = "MYFORMAT"; write; Because select has changed the file to be written to, you must call: select (STDOUT); to reset the write file to be the standard output file. Changing the write file using select, not only affects write, it also affects print. Page header. To define a page header for a file, create a a print format definition with the special name: filename_TOP where filename is thename of the file variable you are writing to. You can also use the name STDOUT_TOP for standard output. Page numbers are stored in the system variable $% format STDOUT_TOP = Sorgonet.com 2005 Annual Report Page @<<. $% . Setting the Page Length. By default, it's 60 lines. To modify it, change the value stored in the system variable $= $= = 67; # set the page length to 67 lines. Using print with pagination. You will not want to use print if you are using pagination, because Perl interpreter keeps track of the current line numbers on the page by monitoring the calls to write. But you can adjust the line count by changing the value of the system variable $- which indicates the number of lines between the current line and the bottom of the page. A 0 value indicates a new page. Formating long character strings. The @* value filed prints multiple lines of text, but to fit as many word as possible into the outputline, change the initial @ character by the ^ character. format OUTLINE = ^<<<<<<<<<<<<<< $string . To remove blank lines, put the character ~ at the beginning of the line and it will remove it if it's empty. Formatting output using printf. printf used in C programming language is the same as printf in Perl. Field specifiers for printf are: %c single character %d integer in decimal base-10 format %e floating point number in scientific notation %f floating point number in normal notation %g floating point number in compact format %s character string %u unsigned integer Ex: printf ("I want to print the number: %d.\n",$number); Operations with files. To open a file for read and write access, specify +> before the filename: open (READWRITE, "+>file1"); This enables you to overwrite portions of a file, this works best in conjunction with the libraty functions seek and tell. seek and tell functions. The seek function moves backward of forward in a file. seek (filevar,distance,relativeto); filevar, is the file variable to use. distance, is an integer representing the number of bytes (characters) to skip relativeto, 0,1 or 2, 0 means beginning of the file, 1 means relative to current position, 2 menas end of file. seek (MYFILE, 0, 0); # skips back to the beginning of the file MYFILE seek (MYFILE, 80,1); # skips forward from the current position 80 bytes seek (MYFILE, 80,-1); # skips backward 80 bytes from current position. seek (MYFILE, 0,2); # skips to the end of the file (useful when it was opened for reading and writing). The seek functions returs true (nonzero) if it was successful, and 0 if it failed. The tell function returns the distance in bytes between the beginning of the file and the current position of the file. tell (filevar); For example, to retrieve the current position of the file MYFILE: $offste = tell (MYFILE); Reading Characters. $singlechar = getc(INFILE); getc function is useful for "hot key" applications. To put the computer into "hot key" mode, use the following commands: system ("stty cbreak"); #tells the system to get only 1 character at a time. system ("stty -echo"); #do not echo the typed characters on the screen. To restore the normal system behaviour, do this before exit: system ("stty -cbreak"); system ("stty echo"); Directory functions. mkdir (dirname,permissions); mkdir ("/home/user/newdir/",755); chdir (dirname); chdir("/home/user/games"); opendir (variable,directory); opendir(MYNAME, "/home/user/games"); closedir (variable); variable=readdir (mydir); You can open a directory without changing the working directory, it's acomplished by using opendir function, then you can read the contents of the directory with readdir, then you must close it with closedir. File Attribute functions. rename ("oldname", "newname"); if "newname" already exists it will destroy it. It returns true when it's succesful To delete a file or a list of files, use: number=unlink (filelist); Ex: @deletelist = ("file1","file2"); unlink (@deletelist); File-Permission Functions Like the Unix commands, where filelist could be an @array of files chmod (permissions, filelist); chown (userid, groupid, filelist); Miscellaneous Attribute Functions truncate, reduces the file length. Ex: truncate ("/home/user/file5", 5000); stat returns a list containing information about a particular file. stat (file); Process and Data Manipulation The eval function treats a character string as an executable Perl program. Ex: $lineofcode = "print (\"hello\\n\");"; eval ($lineofcode); if no error has ocurred $@ contais the null string, else $@ contains the text of the error message. @array=`ps -aux`; Putting a system command between back quotes, executes the command and returns the result to the @array. Now @array contains the list of running process. Ex: @array=`ps -aux`; foreach $element (@array){ if ($element=~/crond/){ print $element; } } system ('program'); executes a linux progam and returns. The exec function is similar to the system function, except that it terminates the current program before starting the new one. fork creates two copies of your program , the parent process and the child process, and executes simultaneously both. $procesid =fork(); fork returns 0 to the child process, and nonzero to the parent process. this enables you to determine which process is the child process and which is the parent. Ex: $retval = fork(); if ($retval ==0) { #this is the child process exit; #this terminates the child process } else { #this is teh parent process } pipe function I'ts for use with the fork function, it allows communication between processes. 1 - call pipe (infile, outfile); 2 - call fork(); to split the program in parent and child processes. 3- have one of the processes close infile and the other close outfile, so the process in which outfile is open can now send data to the process in which infile is still open. Terminating a program process. The die function terminates the program and prints an error message to the standard error file. die (message); The warn function prints an error message, but does not terminate the program. warn (message); The exit function terminates a program. You can specify a return code to be passed to the system. exit (returncode); The kill Function enables you to send a signal to a group of processes. kill (signal, processlist); Signal is the numeric signal to send, for example, a signal 9 kill the listed processes. Processlist is a list of process IDs (such as the chlid process ID returned by fork). Execution control functions. sleep (time); time is the number of seconds to suspend program execution. processid=wait(); The wait function suspends execution and waits for a child process to terminate (such as a process created by fork). When a child process terminates, wait returns the process ID, of the terminated process. If no child exist, wait returns -1. waitpid (processid, waitflag); The waitpid funcion waits for a particular child process. Waitflag usually is 0 (normal wait) see wait 4 manual page. Process and program related actions whocalledme=caller(); caller returns the name and the line number of the program that called the current running subroutine. It's a 3 element list: - Name of the package where it was called. - Name of the file from which the subroutine was called. - Line number of the subroutine call. chroot ("/home/sorgonet/scripts"); This directory becomes the root directory for the program. local ($variable); Creates a copy of the $variable for use inside a statement block. Any other copies of $localvar that exist are not afected by the changes. timelist= times; Returns a 3 argument list with 4 floating-point numbers: - The user time consumed by this program. - The system time consumed bye this program. - The user time consumed by the child process, if they exist. - The system time consumed bye the child process, if they exist. Random Numbers. number=rand(num); number is a random floating point number between 0 and num. String Manipulation Functions. $position=index("this is a test","hi"); Searches the substring "hi" inside the string "this is a test" and returns it's position, if it's not found retuns -1. Giving a third parameter to the function you can tell how many characters to skip before starting to search: $position=index("this is a test","i",4); It will found the second "i" which is: 5 Remember using this functions that 0 is the first character position. By using this third parameter you can search all the ocurrences of a substring is a text, with a few lines of code. The rindex function is the same as index but it starts searching from right to left. $result=length("this is a string"); The length function returns 16 that's the number of characters of the string. The pos functions returns the location of the last pattern match in a string. while ($string =~ /i/g) { $result=pos(string); print ("matched at: $result\n"); } $string="hi there"; $substring1=substr($string,1,3); $substring2=substr($string,3); The function substr takes a substring from a given string. $substring1 is "i t" and $substring2 is "there". The first argument is the string, the second is where to start (it begins at 0) and the optional third argument is how many characters you want to get, if not given it will get all the characters until the string end. Another interesting use for substr is by replacing the substring by other string, this is acomplished by using substr on the left of the assignment operator = , the resulting string grows or shrinks automatically. $string="it's windows rules"; substr($string,5,7)="linux"; It changes $string begining at position 5 and taking 7 characters , so $string is now "it's linux rules" which is much better. You can specify a position by using negative numbers so it begins to count from left to right. Case conversion lc is for lowercase conversion. uc is for uppercase conversion. lcfirst is for lowercase conversion of the first character. ucfirst is for lowercase conversion of the first character. sprintf function is like printf but it will not write the result to a file, instead it'll return the resulting string. $number=12; $result=sprintf("%d = %x hexadecimal\n",$number,$number); print ($result); Scalar Conversion and List Manipulation chop ($a); Removes the last character from a scalar value. Prints "hell". Common used to remove the trailing newline character from an input line. Ex: $a=<STDIN>; chop ($a); Be carefull because chop returns the removed charater, so you can use it in two ways: $lastcharacter=chop("this string"); where $lastcharacter will be "g" chop($variable); where you remove the last character from $variable and the string with the removed character is stored again in $variable. Used on a list, chop will remove the last character from every element of the list. Ex: @list=<STDIN>; chop (@list); $encryptedstring=crypt("this is a string","ab"); This will crypt the string using the DES algorithm, the second parameter "ab" could be any two character string that will make it more difficult to decode. Hexadecimal to Decimal: $decimalnumber=hex("FFAA"); Float to Integer: $intnumber=int(66.3); Octal to Decimal: $decimalnumber=oct("22"); $asciivalue=ord("~"); print $asciivalue; Prints (126), the numeric ascii table value for the character ~ $charactervalue=chr(126); print $charactervalue; Prints (~), the character value corresponding to the number 126; Scalar function @array1=@array2; Copies the entire content from @array2 to @array1. $numberelements=@array; Assigns $numberelements the number of elements in the @array. @array1=scalar(@array2); Assigns the number of elements of array2 to the @array1. defined function $array[4]=66; if (defined ($array[0])){ print "the value is $array[0]); } else { print "value not defined"; } If a value it's assigned to nothing the defined function knows it. undef function To assing a variable to nothing, you must call the undef function undef($array[3]); Array and List functions grep function @list=("one","two","three"); @result=grep(/^t/,@list); Will return @result with all elements of @list that begins with letter t, grep acts as the unix function grep. splice function @array=("1","2","3"); splice(@array,1,2,("dos","tres")); splice enables you to modify the list stored in and array, add elements in the middle, delete a portion or replace. In the example@array is the array variable to work on, 1 is the number of elements to skip before splicing, 2 is the number of elements to replace, ("dos","tres") is the newlist to splice in (can be also an array like @thislist) Now @array is: "1","dos","tres" To delete elements, just call splice without the newlist argument, Ex: @deleted=splice(@list,5,2); @deleted contains the two deleted elements and @list the new list without the two removed elements. shift function @list=("one","two","three"); $firstvalue=shift(@list); Now $firstvalue is "one" and @list is "two","three", so the array shifted to the left. If shift is called without arguments, then the system array variable @ARGV is used (it's the variable containing the passed arguments to the program) Ex: while (1) { $currentargument=shift; last if (!defined($currentargument)); print ("$currentagument\n"); } Note: shift function is equivalent to this call to splice: splice(@array,0,1); unshift function $count=unshift(@array,$elementtoadd); Undoes the effect of a shift function. Where $elementtoadd could be an @array and $count is the number of elements added. push function push(@array,$element); Unlike unshift, push adds elements to the end of a list. pop function $element=pop (@array) Undoes the push function, $element is the removed item from the end of @array map function @list=(10,20,30); @result=map($_+1,@list); Applies an operation to all the elements of the @list, the example will return @result: (11,21,31) To use map with a subroutine, just pass $_ to the subroutine: @result=map($thesubroutine($_),@list); System Library Emulation Functions getgrent Retrieves information from the user group file. (gname,gpasswd,gid,gmembers)=getgrent; Each call to getgrent returns another line from the /etc/group file setgrent Rewinds the group file, so when getgrent is called, it will return ther first group of the /etc/group file endgrent Frees the memory used to access the group file. getgrnam Retrieves the group file entry corresponding to a particular groupname. (gname,gpass,gid,gmembers)=getgrnam(name); getgrid Similar to getgrnam but using the group id. getlogin Returns the user ID you are logged in. getpgrp Retrieves the process group ID for a particular process. $processgroup=getpgrp(pid); getservent Enables you to search through the system services database, which is /etc/services (name, aliases, portnumber, protocolname)=getservent(); Also related: getservbyname, getservbyport chroot("/home/david"); Changes the root directory for the program. So when your program tries to access thedirectory /things/pictures, it will access to /home/david/things/pictures You need to be root and have execute permission on the specified root directory. Socket Manipulation Functions The socket function socket (socket, domain, type,format); socket is a file variable to be associated with the new socket. domain is the protocol family to use, valid values are in: /usr/include/sys/socket.h which are reperesented by the constans PF_UNIX, PF_INET, PF_IMPLINK and PF_NS. type is the type of socket to create, they are also listed in /usr/include/sys/socket.h which are represented by the consants: SOCK_STREAM, SOCK_DGRAM, SOCK_RAW, SOCK_SEQPACKET, SOCK_RDM. format is the number of the protocol to be used with the socket. It's normally retrieved by calling getprotobyname. The socket function returns nonzero value if it has been created and zero if an error occurs. The bind function. After you use the socket function, the next step is to bind the socket to a particular network address by using the bind function. bind (socket, address); socket is the file variable used in the socket function address is the network address consisting of this elements: - The address type which is always AF_INET - The number of the port to use when connecting. - The packed four-byte representation of the internet address to connect. This function returns a nonzero value if the operation succeeds and zero if fails. To create an address suitable for pasing to bind, call pack. $address =pack("Sna4x8",2,$portnumber,$IP); The pack format specifier "Sna4x8" indicates and unsigned short integer, followed by a short integer in network order (the port number), a four-byte ASCII string (the IP), and eight null bytes. This is the format that bind expects when binding an address to a socket. The listen function. After an address has been bound to the socket associated with each of the machines that are to communicate, the next step is to define a "listening" process which waits for connections (it will be the server). To define this listening process call the listen function. listen (socket, number); socket is the socket created by the socket function. number is the maximun number of processes that can be queued up to connect to this process (maximum is 5). listen returns nonzero on sucess, zero of failure. The accept function. After the listening process is running and recieves a petition, the next step is to call the accept function. accept waits until a process wants to connect and then returns the address of the connecting process. accept (procsocket,socket); procsocket is a previously undefined file variable that is to represent the newly created connection. The listening process can then send to or receive from the other process using the file variable specified in procsocket. This file variable can be treated like any other file variable, the program can send data through the socket by calling write or print, or can read data using the <> operator. socket is the socket created by socket function and bound to an address by bind. The following example is a simple server. Ex: $line= "Hi there!\n"; #the line to be print $port=2000; #the port number to use #Now we are going to check if port 2000 could be used by another program this checks the file /etc/services and if could be used by another program, then adds +1 to the port to use while (getservbyport ($port,"tcp")){ $port++; } #now the protocol number associated with the TCP protocol is retrieved from /etc/protocols, note that $d1, $d2 are dummy variables because we do not want them. ($d1,$d2,$protocoltype)=getprotobyname("tcp"); #we need to know the network address of the machine this server is running, note that `hostname` using back quotes calls to the system function hostname which returns the name of the machine running the server, so you can run this program on any machine without specyfing the server name. ($d1,$d2,$d3,$d4,$rawserver)=gethostbyname (`hostname`); #now the variable $rawserver contains the IP, we need to call to pack to convert the address type, the port number and this IP into the form for be understood by the operating system. The number 2 is the local value of AF_INET, which is the only address type supported. $serveraddress=pack("Sna4x8",2,$port,$rawserver); #the file variable SSOCKET will represent the "master" socket that will listen for connections. The values 2 and 1 are the local values of the constans PF_INET (indicates internet style protocol) and SOCK_STREAM (transmission in the form of streaming bytes). 99% of times you will need to use this constants. socket(SSOCKET,2,1,$protocoltype) || die ("No socket"); bind (SSOCKET,$serveraddress) || die ("Can not bind"); #number 1 indicates only one client at time listen (SSOCKET,1) || die ("Can not listen"); #next line waits until a client process wants to connect to this server. When connection is stablished, accept creates a new socket associated with this connection and uses the file variable SOCKET to represent it. The address of the client coneection is returned in $clientadress. ($clientaddress=accept (SOCKET,SSOCKET)) || die ("Can not accept"); #now SOCKET is like any other file variable, you can read or write data to it. #we need to turn off bufferint for SOCKET, to be sure that data sent through the socket is sent right away. select (SOCKET); $|=1; #$|=1 tells Perl that the current selected file does not need to be buffered, $| is a special variable, so it does not wait until the buffer is full. #we write to the file variable SOCKET the $line containing "Hi there!" print SOCKET ("$line\n"); close (SOCKET); close (SSOCKET); Send and receiving data through the same socket is dangerous, because the risk of deadlock which is when both the client and the sever thinks that the other is going to send data. Neither can proceed until the other does, the only way to get out of a deadlock is try to kill the process. The connect function. In the server side, we call listen to indicate that it's the listening process, and then accept to wait for a connection from another process. We need now the process that connects to the server. It's called client. To connect to a process that has called accept and is now wainting for a connection, use the connect function. connect (socket, address); socket is a file variable represnting a socket created using socket and bound using bind. address is the internal representation of the internet address to which ou want to connect. This address needs to have been passed to bind to bind it to a socket, and the socket must have been specified in calls to listen and accept. After connect has been called, the caller can send to or receive data from the other process bye means of the file variable specified in socket function. The following is a simple client example. Ex: #Look for a port $port=2000; while (getservbyport($port, "tcp")){ $port++; } (undef,undef,$prototype)=getprotobyname("tcp"); (undef,undef,undef,undef,$rawclient)=gethostbyname(`hostname`); #the next line: "localhost" must be changed for the server name (undef,undef,undef,undef,$rawserver)=gethostbyname("localhost"); $clientaddr=pack("Sna4x8",2,0,$rawclient); $serveraddr=pack("Sna4x8",2,$port,$rawserver); #creating the socket socket (SOCKET,2,1,$prototype) || die ("no socket"); #binding the SOCKET to the client address bind (SOCKET,$clientaddr) || die ("can not bind"); connect (SOCKET,$serveraddr); #getting the input from the SOCKET file variable $line=<SOCKET>; print ("$line\n"); close (SOCKET); The shutdown function. When two processes are communicating using a socket, data can be sent in either direction. The shutdown function enables you to indicate that the traffic in one or both directions is no longer needed. shutdown (socket, direction); socket is the file variable. direction is one of this values: - 0 the program can send but can not receive. - 1 the program can receive but can not send. - 2 the program can not send and can not receive. The socketpair function. It's similar to socket, but it creates a pair of sockets socketpair (socket1, socket2, domain, type, format); Parameters are the same as socket and it's often used to create bidirectional communication channel between a parent and a child process. (some machines that support socket, do not support socketpair). The getsockopt and setsockopt functions. Enable you to obtain and set socket options. To obtain the current value of a socket option in your environment, call: retval= getsockopt(socket, opttype, optname); socket is the file variable associated with the socket you want to query. opttype is the type of option. Posible values are in: /usr/include/sys/socket.h optname is the name of the option to be retrieved. To set a socket option, call setsockopt. setsockopt (socket, opttype, optname, value); Values are the same as getsockopt. Note: socket options are system dependent. The getsockname and getpeername functions. Enable you to retrieve the addresses of the two ends of a socket connection. retval=getsockname(socket); As in the other socket functions, socket is the file variable associated with a particular socket, retval is the returned address. The returned address is in packet format. Ex: $rawaddress=getsockname(SOCKET); (undef,undef,@addressbytes)=unpack("SnC4x8",$rawaddress); $readable=join(".",@addressbytes); Note that you will already have the address returned by getsockname, because you need to pass it to bind. To retrieve the address of the other end of the socket connection, call getpeername. retval=getpeername(socket); Note: The address returned by getpeername is normally identical to the address returned by accept. Perl command line options. perl options program
System variables. Global Scalar Variables - Can store only one scalar value. - On a program only exists one copy of the variable.
Pattern matching variables. When using pattern matches the matching patterns enclosed in parenthesis are stored into $1 $2 $3 and so on, they are visible only in the current scope {}, when a pattern is matched successfully, the variable $& contains the entire matched pattern. The unmatched text preceding the match is stored in the $` variable and the unmatched text following the match is stored in the $' variable. The $+ variable matches the last subpattern enclosed in parentheses. File system variables. When the write statement sends formatted output to a file, it uses the value of the $~ system variable for that file to determine the print format to use. The $= variable defines the page length (numer of lines per page) for a particular output file, normally it's inicialized to 60, that includes paged headers. The $- variable associated with a particular file variable lists the number of lines left on the current page of that file. Each call to write subtracts the number of lines printed from $- when it's zero, a new page is started. The page header print format: $^ when write starts a new page, you can specify the page header that is to appear on the page. To do this, define a page header print format for the output file which the page is to be sent, the system variable $^ contains the name of the print format to be used for printing page headers, by default the copy of $^for a particular file is set equial to the name of the file variable plus the string _TOP Bufferint output: $| When you send output to a file by using print or write, the operating system could not write it right away, some systems send it to a buffer and when it's full, it's written all at once. Maybe you don't want to use a buffer, the system variable $| indicates whether a file is being buffered or not, by default it's set to 0 and uses buffering. Change it to 1 to don't use buffering, but you must do it before writing to the file for the first time. The current page number is stored in the $% variable, and its associated to each file opened by Perl. Array system variables All the following array variables are automatically defined for use in Perl programs. All of these variables, exept for the @_ variable, are global variales, so their value is the same throughout a program. The @_ variable It's defined inside each subroutine, is a list of all the arguments passed to the subroutine. Ex: &my_sub("hi",66,$var); Inside the subroutine @_[0] value will be: "hi", @_[1] will be: 66, @_[2] will be: $var Most subroutines, assign @_ to locally defined scalar variables using the local function: sub my_sub{ local ($arg1,$arg2,$arg3)=@_; } The @ARGV variable When you run a Perl program, you can specify the values to be passed to the program. Ex: myprogram "hi" 888 Inside the Perl program they are stored in @ARGV array The @F variable When using the -n or -p option in Perl, you can also supply the -a option. It's used to break each input line into individual words (without spaces or tabs). All these words are stored in the built-in array variable @F. The @INC variable Contains a list of directories to be searched for files requested by the requiere function. The list contains the following intems in this order: - Directories specified by the -I option - The Perl library directory - The current working directory The %INC variable The build-in associative array %INC lists the files requested by the requiere function that have already been found. The %ENV variable It's an associative array listing the environment variables defined for this program and their values. The %SIG variable You can control how your program responds to signals it recieves (UNIX only), Ex: $SIG{"INT"}="IGNORE"; Build-in file variables STDIN, STDOUT, STDERR Used for standard input, standard output and standard error. ARGV Is a special file variable for the file being read by the <> operator. This lines are equivalent: $line= <>; $line= <ARGV>; DATA It's used with the __END__ special value, which can be used to indicate the end of a program. This enables you to have your program and data in the same file. EX: $line= <DATA>; print ("$line"); __END__ This is a line of data. Underscore _ Represents the file specified by the last call to either the stat function or a file test operator
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
by: DrDoom at Sorgonet.com
|