Friday, September 24, 2010

The comma operator

Earlier in this chapter I stated that the comma operator (not the comma separator, which is used to separate definitions and method arguments) has only one use in Java: in the control expression of a for loop. In both the initialization and step portions of the control expression, you can have a number of statements separated by commas, and those statements will be evaluated sequentially.

Using the comma operator, you can define multiple variables within a for statement, but they must be of the same type.

Thursday, September 23, 2010

Casting

Java allows you to cast any primitive type to any other primitive type, except for boolean, which doesn’t allow any casting at all. Class types do not allow casting. To convert one to the other, there must be special methods.

Casting from a float or double to an integral value always truncates the number. If instead you want the result to be rounded, use the round( ) methods in java.lang.Math

Wednesday, September 22, 2010

Unsigned right shift operator

The signed right shift >> uses sign extension: If the value is positive, zeroes are inserted at the higher-order bits; if the value is negative, ones are inserted at the higher-order bits. Java has also added the unsigned right shift >>>, which uses zero extension: Regardless of the sign, zeroes are inserted at the higher-order bits. This operator does not exist in C or C++.

If you shift a char, byte, or short, it will be promoted to int before the shift takes place, and the result will be an int. Only the five low-order bits of the right-hand side will be used. This prevents you from shifting more than the number of bits in an int. If you’re operating on a long, you’ll get a long result. Only the six low-order bits of the right-hand side will be used, so you can’t shift more than the number of bits in a long.

For example:

int i = -1;
i >>>= 123; // For int, only low-order 5 bits of 123 will be used: 27
// So it is actually i >>>= 27
// Result is 31
System.out.println(i);


Refer to Page 43 for primitive type in Java

Tuesday, September 21, 2010

Shell Tips - Would be growing continuously

Every time looking for a small useful but seldom used tips all over the internet I hate it.


How to output backslash escapes useing echo?


From echo(1):

-e enable interpretation of backslash escapes
-E disable interpretation of backslash escapes (default)

So by default there is no backslash escapes! If you want, you have to do like this:

echo -e "\b\c"


What does exec do?


From bash(1):

exec [-cl] [-a name] [command [arguments]]
If command is specified, it replaces the shell. No new process is created. The
arguments become the arguments to command. If the -l option is supplied, the
shell places a dash at the beginning of the zeroth arg passed to command.
This is what login(1) does. The -c option causes command to be executed with
an empty environment. If -a is supplied, the shell passes name as the zeroth
argument to the executed command. If command cannot be executed for some reason,
a non-interactive shell exits, unless the shell option execfail is en-
abled, in which case it returns failure. An interactive shell returns failure
if the file can not be executed. If command is not specified, any redirections
take effect in the current shell, and the return status is 0. If there is a
redirection error, the return status is 1.

exec is useful when you want to set script redirection for all your scripts in current shell, otherwise you will have to set redirection for each.

exec 1>STDOUT_GOES_TO_HERE 2>STDERR_GOES_TO_THERE

After this, all following scripts running from this shell will redirect their STDOUT to STDOUT_GOES_TO_HERE and STDERR to STDERR_GOES_TO_THERE


Where to find ASCII?



man ascii


Using a Control Character in a Script


From UNIX POWER TOOLS
There are times when you need to use non-printing control characters in a script file. If you type them directly into the file, they can be invisible to printers and on your screen - or, worse, they can cause trouble when you print or display the file.

One time you might need to store control characters in a script is when you're writing sed substitution commands; you don't know what delimiters to use because the strings you're substituting could contain almost any text:

sed "s/$something/$whoknows/"

Because sed can use almost any character as the delimiter, you can use a control character like CTRL-A instead of the slash (/). Another time you might also need to use non-printable strings of characters is for controlling a terminal; you won't want to type an Escape character directly into the file.

The answer is to use a command that will create the control characters as the script runs - and store them in shell variables.

If your version of echo interprets an octal number in a string like \001 as its ASCII value, the job is easy. An octal-to-ASCII chart shows you that 001 is CTRL-A. You can store the output of echo in a shell variable, and use the variable wherever you need a CTRL-A character:

ca=`echo '\001'` # control-A character
...
sed "s${ca}$something${ca}$whoknows${ca}"


Remove ^M characters at end of lines in vi


UNIX treats the end of line differently than other operating systems. Sometimes when editing files in both Windows and UNIX environments, a CTRL-M character is visibly displayed at the end of each line as ^M in vi.

To remove the ^M characters at the end of all lines in vi, use:

:%s/^V^M//g


The ^v is a CONTROL-V character and ^m is a CONTROL-M. When you type this, it will look like this:

:%s/^M//g


In UNIX, you can escape a control character by preceeding it with a CONTROL-V. The :%s is a basic search and replace command in vi. It tells vi to replace the regular expression between the first and second slashes (^M) with the text between the second and third slashes (nothing in this case). The g at the end directs vi to search and replace globally (all occurrences).

__CONTINUED__

Wednesday, September 15, 2010

COPY support of PostgreSQL

For details refer to DBD::Pg document and

http://www.postgresql.org/docs/current/static/sql-copy.html


$dbh->do("COPY mytable FROM STDIN");
$dbh->pg_putline("123\tPepperoni\t3\n");
$dbh->pg_putline("314\tMushroom\t8\n");
$dbh->pg_putline("6\tAnchovies\t100\n");
$dbh->pg_endcopy;

Thursday, September 2, 2010

DBIx::Class primary key if underlying RDBMS table lack one

The Significance and Importance of Primary Keys

The concept of a primary key in DBIx::Class warrants special discussion. The formal definition (which somewhat resembles that of a classic RDBMS) is a unique constraint that is least likely to change after initial row creation. However this is where the similarity ends. Any time you call a CRUD operation on a row (e.g. delete, update, discard_changes, etc.) DBIx::Class will use the values of of the primary key columns to populate the WHERE clause necessary to accomplish the operation. This is why it is important to declare a primary key on all your result sources even if the underlying RDBMS does not have one. In a pinch one can always declare each row identifiable by all its columns:


__PACKAGE__->set_primary_keys (__PACKAGE__->columns);