Thursday, March 13, 2008
Ruby VS Python: The discussion
Thursday, March 6, 2008
Learning Perl, 4th Edition Notes 0x9
LABLENAME: unless { while { redo LABLENAME; } }* The value of logical operator "&&" and "||" is the last evaluated part. And they could be written in words ("and", "or"). * The ternary operator (?:), as it in C, is availble in perl.
Sunday, February 24, 2008
Learning Perl, 4th Edition Notes 0x8
@list = split /seperator/, $string;* Yielding a scalar, the join operator glues a list of strings with the given glue string between the pieces:
$scalar = join $glue_string, @list;* Used in a list context, the "m//" operator returns a list of matching variables created with the parentheses withing the pattern. * To make the quantifiers non-greedy, put a qustion mark (?) after them. * The modifer "m" changes the "m//" and "s///" operator into operating each line within a variable, instead of the whole variable. * By default, the value of $^I is undef. But when it is assigned with any string, that string will become the suffix of the backup of the files that are read in via the diamond operator and modified. When this happens, the default output is changed into those files from the standard output. * The code operating on a serial of files
#!/usr/bin/perl # Part 1 $^I = ".bak"; # Part 2 while( <> ) { # Part 3 s/Randal\ the\ grey/"Randal the white"/g; # Part 4 print; # Part 3 } #Part 3is equivalent to the following in command line:
$ perl -p -i.bak -e 's/Randal\ the\ grey/"Randal the white"/g' The_serial_of_filesAs the comments show, the arguments in the command line contributes to differnt parts in the code. To clarify, part 1-4 are each contributed by "perl", "-i.bak", "-p", "-e ...". This is hard to put shortly, but in addtion, using -n instead of -p will take the "print" line out of the code and leave other things the same. * There's a way to group things in Regular Expression without triggering the memory variables. To achieve this, lead whatever is grouped with "?:".
Learning Perl, 4th Edition Notes 0x7
Thursday, February 21, 2008
Learning Perl, 4th Edition Notes 0x6
Wednesday, February 20, 2008
Learning Perl, 4th Edition Notes 0x5
$hash_name{$key}* Accessing an unassigned hash element will yield undef. * Hash name is leaded by a percent sign (%) when treated as a variable, like the at sign (@) for arrays. * In a list context, hash appears as a list that consist of key/value pairs in a random sequence. * To switch kay and value in hashs, use reverse:
%a_hash = reverse %a_hash;
* For convenience, the big arrow (=>) can be used to show the relation between kay and value in literal hashs.
$a_hash = ( "localhost" => '172.0.0.1', );* the key function returns a key list of a hash, while the the values function returns the values. * The each function can be used to iterate a hash, it returns a list consists of two elements each time it is used: a pair of kay/value. * The existence of a key can be tested by the function exists. * delete function will make a key disappear, but not assign undef to it.
Monday, February 18, 2008
Learning Perl, 4th Edition Notes 0x4
while (<STDIN>) { print; }* while reads a line and start an iteration each time. In contrast, foreach dosen't start the loop until it read the whole thing. * The diamond operator (<>) reads in the files listed in invocation arguments, and returns a list. * The invocation arguments are automatically stored in the array @ARGV. * When printing an interpolated array, there will be spaces between each elements. * A C-like printf is available in perl, which requires a format string and a list of things to print. * A trick to print arrays with printf:
$format_string = "something first".("%s " x @the_array_to_be_printed); printf $format_string, @the_array_to_be_printed;* A filehandle is the name in a Perl program for an I/O connection between Perl process and the outside world. * A Perl process automatically inherits, at least, three filehandles from its parent process when starts: STDIN, STDOUT, STDERR. * To open a filehandle, use open, followed by the filehandle name and a double quoted file name:
open NAME, "file_name";The file name can be leaded by ">", "<" or ">>", which stands for overwrite, read from or add to a file. Another way to do this it to use three arguements:
open NAME,">>",$file_name;* open will return a false value if it opens a "bad" filehandle. * To close a filehand, use close to lead the filehandle name. * die accepts a string to print out into STDERR before it ends the process. It also stores the error message given by the system into $!; * worn works in the same way as die, but it dosen't quit the program. * A filehandle name following print or printf will change the latters desination. * select changes the default output filehandle:
select FILEHAND;* If a filehandle is reopened, the original one will be hang up, and will return when the new one terminates.
Thursday, February 7, 2008
Learning Perl, 4th Edition Notes 0x3
Tuesday, February 5, 2008
Learning Perl, 4th Edition Notes 0x2
* Lists, which means ordered collections of scalars, are often used as if it is interchangeable with arrays, while the latter are actually the data structure, which, worthy enough to be pointed out to those C programmers, are considered as variables.
* Numbers, strings and undef values can be held in a same array.
* The syntax for refering an array is
@array_names
while the way to access an elements is usually
$array_names[index]
* The array is automatically extended as needed. The following code
$a_array[0] = 'the value';
$a_array[100] = 'the end value';
creates 99 array elements whose value is undef.
* A list literal is a list of comma-separated values enclosed with parentheses. ".." is a range operator. It creates a list of values by counting from the left scalar, which must be smaller than the latter, up to the right ones.
* A great shortcut to generate a list of simple words is to use qw. Which saves the quotes marks and commas. Whitespaces between words are discarded. Any punctuation character can be used as the delimiter instead of parentheses. Put a backslash before the delimiter when it appears in the list.
* List assignment is applied by using, not surprisingly, the equal character.
list_one = list_two.
The list can be either arrays or list literals.
* Using arrays as stacks, pop and push operator works on the last elements of an array.
$scalar = pop @an_array; # take the last elements off and return it.
push @an_array,@another_array; # add the second array(which can also be a scalar) to the end of the first one.
* shift operator takes off the first element of an array and return its value. unshift do the opposite thing.
* Arrays may be interpolated into space-separated string in a double quoted string.
* Syntax of foreach control structure:
foreach $iterate_scalar ( iterated_list ) {
code block;
}
When the iterate scalar is omitted, the default $_ will be there instead.
* The reverse operator reverses the order the elements in a list.
* The sort operator sorts the elements of a list into dictionary order
* reverse operator generates a scalar when used in scalar context.
* A scalar is automatically promoted to make a one-element list in list context.
* to force a scalar context: put scalar before the array.
*
Thursday, January 31, 2008
Learning Perl, 4th Edition Notes 0x1
$perl -w programor, for more details:
$perl -Mdiagnostics ./programRequest on #! line:
#!/usr/bin/perl use progma
#!/usr/bin/perl use warnings;or, for more details:
#!/usr/bin/perl use digonostics;* Output with print
print list;list is a series of scalars seperated by commas. * Double-quoted string literal is subject to variable interpolation. Curly braces can be used as the delimiter of variable names. For instance:
print "This is $whats"; #print the value of $whats print "This is ${what}s" #print the value of $what* Perl has the same precedence and associativity as C. * Numeric and string comparison operators: == eq (equel) != ne (not equel) <> gt (greater than) <= le (less or equel) >= ge (greater or equel) * For boolean values, 0 or empty string means false, other number or string means true; *
Learning Perl, 4th Edition Notes 0x0
Thursday, January 3, 2008
Read CHM file with Firefox in Linux
- Add the extension CHM Reader
- Save the following shell script as readchm.sh (or any other name you like) to /usr/local/bin (or any place you like).
#!/bin/sh
firefox "chm:file:///$1"exit 0
- Change the script to excutable file:
chmod 755 /usr/local/bin/readchm.sh
- Right click a chm file,choose "Open with another Appication",click "Usa a custom command",type in
/usr/local/bin/readchm.sh
- Click "Open".