User Tools

Site Tools


ower_own_perl_wiki

Differences

This shows you the differences between two versions of the page.

Link to this comparison view

Both sides previous revisionPrevious revision
Next revision
Previous revision
ower_own_perl_wiki [2007/09/25 21:43] roumaniower_own_perl_wiki [2008/09/29 03:02] (current) cs243081
Line 1: Line 1:
 ====== Our Own Perl Wiki ====== ====== Our Own Perl Wiki ======
 +
 +
 +
  
  
  
 ===== Differences from Java ===== ===== Differences from Java =====
-  * Type: Variables are either **scalars** prefixed by **$****arrays** of scalars prefixed by **@**or **hashes** of scalar pairs prefixed by **%**.+  * Type: Variables are either **scalars** prefixed by **$****arrays** of scalars prefixed by **@**or **hashes** of scalar pairs prefixed by **%**.
  
-  * Operators: same as Java except **eq**, **gt**, ... for strings.+  * Operators: same as Java except **eq**, **gt**, **lt**, ... for string comparisons.
    
-  * Control Structures (selection and iteration): same plus **foreach** for hashes.+  * Control Structures (selection and iteration): same as Java plus **foreach** for hashes.
  
-  * Cool stuff: interpolation, here docs, regex, and web goodies such as param and &ENV.+  * Cool stuff: interpolation, here-docs, regex, and web goodies such as param and %ENV.
  
 ---- ----
  
 +===== File I/O in Perl =====
 +**Default file handlers:**
 +  * STDIN: Standard input from console
 +  * STDOUT: Standard output to console
 +  * STDERR: Standard error output to console
 +
 +**Opening a file**
 +<code perl>
 +open(FILEHANDLE, filename)
 +</code>
 +Where FILEHANDLE specifes the name for the file handler in which you will refer to your file and filename specifies the location of your file.
 +
 +In the file name one can specify the various read/write modes.
 +^ Example      ^ Description       ^
 +| open(FILEHANDLE, <filename)      | opens the file for reading    | 
 +| open(FILEHANDLE, >filename)     | opens the file for writing    |
 +| open(FILEHANDLE, >>filename)     | opens the file for appending    |
 +| open(FILEHANDLE, +>filename)     | opens the file for reading and writing    |
 +| open(FILEHANDLE, +<filename)     | opens the file for reading and writing    |
 +| open(FILEHANDLE, +>>filename)     | opens the file for reading and writing    |
 +
 +File locking for I/O is provided using the **flock()** function. It is suggested to do the lock on a seperate file to prevent race conditions.
 +<code perl>
 +open(LOCK, ">write.lock")
 +flock(LOCK, LOCK_EX);
 +open(DATA, myFile);
 +#...do work with the DATA file...
 +close LOCK;
 +close DATA;
 +</code>
 +Closing the **LOCK** handler automatically unlocks the semaphore. One can also use **flock(LOCK, LOCK_UN)** to do the unlocking specifically. The flock function takes the following operation parameters:
 +^ Operation      ^ Description       ^
 +| LOCK_SH      | requests a shared lock for a file, will block until lock is obtained    | 
 +| LOCK_EX      | requests an exclusive lock for a file, will block until lock is obtained   
 +| LOCK_UN      | unlocks the previously locked file    | 
 +| LOCK_NB      | should be bitwise 'or'-ed with either LOCK_SH or LOCK_EX and will result in a non-blocking lock    | 
 +\\ 
 +----
 +
 +===== Array manipulation: Removing duplicate array items =====
 +
 +So by now we know what an array looks like and maybe we've learned a few  ways to manipulate it.  Well, here's a trick to remove duplicate items in an array while preserving the item's order by using a hash.
 +
 +Here's a couple of things you ought to know to fully understand the code:
 +
 +  * A hash is like an arrays except that they link a key to a value and the key is unique.
 +  * $_ is a special perl variable known as the default or implicit variable.  In our example below it'd be the implicit value of the current @array1 item. 
 +
 +<code perl>
 +@array1 = qw(one two one three four two);
 +
 +foreach (@array1) {
 +    push(@array2, $_) unless ($seen{$_}++);
 +}
 +
 +print "@array2";
 +</code>
 +
 +**output:** one two three four
 +
 +The reason that we're using a hash here is to use its limitation (each key must be unique) to our advantage.  In this example we use a hash called "seen" with keys set to each item in our array. For each string we test to see if that key already refers to a non-false value. The post-increment operator (++) adds one to that key's value, but only after the condition is checked. The result is that the first time a key string is encountered the condition is false (since the value is undefined) and the key is therefore pushed onto our second array. The key is then incremented and any further test of the key's value will evaluate "true" (i.e. non-zero, and non-empty).
 +
 +__Reference:__ http://www.perlcircus.org/arrays.shtml
ower_own_perl_wiki.1190756616.txt.gz · Last modified: 2007/09/25 21:43 by roumani

Donate Powered by PHP Valid HTML5 Valid CSS Driven by DokuWiki