Installing Perl Modules on Windows with PPM

If you installed Perl on Windows with ActivePerl, you have a tool known as PPM to install modules.

Links:

Example usage from command prompt:
ppm search Image
ppm install Image-Size

Notes – Java Web Services: Up and Running Chapter 1

A simple web service:

HelloWorldServer.java
package com.codelol.hws;

import javax.jws.WebService;
import javax.jws.WebMethod;
import javax.jws.soap.SOAPBinding;
import javax.jws.soap.SOAPBinding.Style;
/*
* The Service Endpoint Inteface (SEI) for the
* HellowWorldServer
*/

@WebService
@SOAPBinding(style = Style.RPC)
public interface HelloWorldServer {
@WebMethod String getHelloWorld();
@WebMethod String getTimeString();
@WebMethod int getRandomInteger();
}

HelloWorldServerImpl.java
package com.codelol.hws;

import java.util.Date;
import java.util.Random;
import javax.jws.WebService;
/*
* The Service Implementation Bean (SIB) for the HelloWorldServer
*/

@WebService(endpointInterface =”com.codelol.hws.HelloWorldServer”)
public class HelloWorldServerImpl implements HelloWorldServer {
public String getHelloWorld() {
return “Hello World!”;
}

public int getRandomInteger() {
Random r = new Random(123);
return r.nextInt();
}

public String getTimeString() {
return new Date().toString();
}
}

HelloWorldServerPublisher.java
package com.codelol.hws;

import javax.xml.ws.Endpoint;

// Local host is 127.0.0.1:9876

public class HelloWorldServerPublisher {
public static void main(String[] args){
Endpoint.publish(“http://127.0.0.1:9876/hws”, new HelloWorldServerImpl());
}
}

Notes: Thinking in Java 3rd Edition – Chapter 2-6

Chapter 2: Everything is an Object
Where storage lives
1. Registers – the fastest and most limited; you don’t have direct control of this
2. The stack – lives in RAM; very fast, second only to registers; moves stack pointer up or down to release or create new memory respectively; some Java storage exists on the stack, like object references but Java objects themselves are not placed on the stack
3. The heap – general purpose pool of memory; also in RAM; much more flexible than the stack because the compiler does not need to know how much storage or how long the storage must stay ont he heap; it takes more time to allocate heap storage than the stack
4. Static storage – contains data that is available the entire time a program is running; use the static keyword, however Java objects are never placed into static storage
5. Constant storage – constant values are often placed directly into the program code although it can optionally be placed into ROM in embedded systems
6. Non-RAM storage - lives completely outside of the program; two examples: streamed objects (e.g. bytes sent from another machine) and persistent objects (e.g. placed on disk)

Fields and methods
When you define a class you can put two elements into your class: fields (or data members) and methods (or member functions).  The fundamental parts of a method are the name, the argument, the return type, and the body

The static keyword
Two reasons:
1. Have only one piece of storage for particular piece of data regardless of how many objects are created, or even if no object is created (static data)
2. You need a method that you can call even if no object is created (static method)

Chapter 3: Controlling Program Flow
Ternary if-else operator

boolean-exp ? value0 : value1

Literals
Use literals when the type is ambiguous by giving the compiler additional information.  For example:
public class Literals {
char c = 0xffff; // max char hex value
byte b = 0x7f; // max byte hex value
short s = 0x7fff; // max short hex value
int i1 = 0x2f; // Hexadecimal (lowercase)
int i2 = 0X2F; // Hexadecimal (uppercase)
int i3 = 0177; // Octal (leading zero)
// Hex and Oct also work with long.
long n1 = 200L; // long suffix
long n2 = 200l; // long suffix
long n3 = 200;
//! long l6(200); // not allowed
float f1 = 1;
float f2 = 1F; // float suffix
float f3 = 1f; // float suffix
float f4 = 1e-45f; // 10 to the power
float f5 = 1e+9f; // float suffix
double d1 = 1d; // double suffix
double d2 = 1D; // double suffix
double d3 = 47e47d; // 10 to the power i.e. 27 x 10^-47
}

A situation where this would be required is float f = 1e10f, because exponent numbers are expected to be doubles and without this suffix, a casting error will occur

Promotion
If you perform mathematical or bitwise operations on char, byte, or short, the result will be int.  You will need to explicitly cast back to the required type (although you may lose information).  Promotion generally means the largest data type in an expression will determine the size of the result of that expression.

Java has no “sizeof”
C and C++ have sizeof for portability since different data types might be different sizes on different machines.  Java does not need sizeof beacuse all the data types are the same size on all machines.

Example of a multi-variable for statement
for(int i = 0, j = 1;
i < 10 && j != 11;
i++, j++)
/* body of for loop */;

Labels
Labels are used with loops to provide additional control flow and can be called by break or continue.  For example:
outer:
for (int i=0;i<5;i++)
{
for (int j=0;j<5;j++)
{
System.out.println(“j:” + j);
if (j == 2)
{
continue outer;
}
}
}

Math.Random
Math.Random() generates values from [0,1), that is 0 is inclusive.

Chapter 4: Initialization & Cleanup

Overloading
Often the same word expresses a number of different meanings is overloading

Calling constructors from constructors
Use the this keyword with the parameters for the required constructor
e.g.
Cellphone(int cost){
// do stuff
}
Cellphone(String name){
this(500);
// do more stuff
}

Array declaration
You can use both int[] arr; or int arr[]; there are two styles to conform to what C and C++ programmers expect

Chapter 5: Hiding the Implementation
Nothing

Chapter 6: Reusing Classes
Lazy initialization – creating the object only when necessary to reduce overhead.  That is, don’t automatically create an expensive object before the constructor or in the constructor if it may not be used.

The final keyword
1. Final data
Tell the compiler the data is constant, can be compile-time constant or a value that doesn’t change after it has been initialized during runtime.  With primitives the value is constant, however for final objects, the reference is constant but the object itself can change.  This is important when thinking about arrays, which are objects (you cannot stop the array values from changing!)

Blank final
Fields declared as final but with no initialization value

Final arguments
You cannot change what the argument reference points to

2. Final methods
Two reasons: prevent overriding by inheritance and and for efficiency.  For efficiency, making a method final turns any calls to that method as an inline call.  That is, the method call will be replaced with a copy of the code from the body.  However, the JVM is usually smart enough to detect this, so you should not worry about it.

Final and private
Any private method is implicitly final

3. Final classes
You don’t want to allow subclassing

Notes: Thinking in Java 3rd Edition – Chapter 1

Just a few notes from Bruce Eckle’s Thinking in Java 3rd Edition, mostly terminology that I forgot the proper term for.

Chapter 1: Introduction to Objects

Why Hide Implementation
1. To prevent client programmers from touching things they shouldn’t touch
2. Allow library designers to change the internal workings without affecting the client programmer

You can hide implementation with access specifiers like public, private, and protected.

Reusing the Implementation
Composition (“has a” relationship; a car has an engine) occurs when you have an existing class in a new class.  If it happens dynamically, it is aggregation.

Is-a vs is-like-a Relationship
If the derived type is the same type as the base class and has the exact same interface, it is called pure substitution and is known as the “is-a” relationship.  If you decided to add new methods to the derived class, then it is a “is-like-a” relationship.

Early Binding vs Late Binding
For early binding, the compiler knows at compile time what code will be executed.  For late binding (like OOP languages), the code being called isn’t known until run time.

Polymorphism
Definition: treat an object not as the specific type that it is, but instead as its base type
Treating a derived type as though it were the base type is known as upcasting.  This occurs when doing polymorphism.

Interface vs Abstract Class
Abstract classes can have non-abstract methods implemented and leave the abstract methods to be implemented by the derived class.  Interfaces cannot have methods implemented.  You can combine multiple interfaces together whereas you cannot inherit from multiple regular or abstract classes.

Useful link

Downcasting
When you retrieve an Object from a container and need to return it back to the original type, you need to perform downcasting.  This isn’t true if you use parametrized types.

What is OOP?
Technically OOP is about abstract data typing, inheritance, and polymorphism.

Using CSS for an Image with Semi-Transparent Caption

This effect is similar to the front page of AllKpop, which has an image link with a transparent black text box with white text on top. This effect can be done using CSS, with code taken from the WordPress theme Infinity.

What is important is the (.thumb width) = (.thumb title h2 a width) + 2*(.thumb title h2 a padding).  This ensures the text is properly centered in the image.

The basic CSS is as follows:

.thumb {
  background: #fff;
  display: block;
  width: 190px;
  overflow: hidden;
  height: 250px;
  position: relative; }
.thumb-title {
  margin-bottom:0px;
  background:#000;
  bottom:0;
  right:0;
  display:block;
  position:absolute;
  padding:0px;
  filter:alpha(opacity=75);-moz-opacity:.75;opacity:.75;}
.thumb-title:hover { background:#222; }
.thumb-title h2 { margin: 0px;}
.thumb-title h2 a {
  padding: 10px;
  font:14px Arial,helvetica;
  display:block;
  color:#fff;
  text-decoration: none;
  width: 170px;}
.thumb-title h2 a:hover {
  padding: 10px;
  font:14px Arial,helvetica;
  display:block;
  color:#fff;
  text-decoration: none;
  width: 170px;}

Usage example:

<div class="thumb powered">
<a href="http://example.com"><img src="/img/example.jpg"/></a>
<div class="thumb-title"><h2>
<a href="http://example.com">Example</a></h2></div>
</div>

Fixing the RSS Widget Problem in WordPress

Recently, certain feeds no longer worked for me on WordPress. I suspected my host had done something because these errors only occurred for WordPress installations hosted by them (the feeds worked on WordPress.com hosted blogs).

Errors for this problem can result in the text:
Warning: Attempt to assign property of non-object in /home/XXXX/public_html/XXXX/wp-includes/rss.php on line 449
Or
Error: could not find an RSS or ATOM feed at that URL.

Read More

Aligning Images in The Morning After WordPress Theme

One of my favourite grid-based WordPress themes is The Morning After. However, the CSS can be messy to deal with because the theme uses an external CSS file in lib/compressed.css.

In lib/compressed.css, remove the code:

p{margin:0 0 1.5em 0;text-align:justify;}
p.last{margin-bottom:0;}
p img{float:left;margin:1.5em 1.5em 1.5em 0;padding:0;}
p img.top{margin-top:0;}

In stylesheet.css use the following code for alignment:

img.alignright {float:right; margin:0 0 1em 1em}
img.alignleft {float:left; margin:0 1em 1em 0}
img.aligncenter {display: block; margin-left: auto;
margin-right: auto; border: 0px;}
a img.alignright {float:right; margin:0 0 1em 1em; border : 0}
a img.alignleft {float:left; margin:0 1em 1em 0; border : 0}
a img.aligncenter {display: block; margin-left: auto;
margin-right: auto; border: 0px;}

ActionScript 3.0 Day 1 – Concepts and Syntax

I decided to start learning ActionScript today. I will document what I learn as I make my way through ActionScript books and tutorials.

Compilation

  • ActionScript code must be condensed into a binary format known as ActionScript bytecode
  • ActionScript bytecode is wrapped in a binary container known as a .swf file, which is what is executed by Flash runtime
  • Thus, a compiler is required to convert ActionScript to ActionScript bytecode and ActionScript bytecode to .swf

Two-Level Compilation

  • The first compilation is converting ActionScript into a format understood by the Flash runtime (done by the developer)
  • The second compilation is the Flash runtime automatically compiling the ActionScript bytecode to a format understood by the computer hardware (using just-in-time compilation)

Syntax
The syntax of ActionScript 3.0 is similar to Java.  A syntax comparison between Java 5 and ActionScript 3.0 can be found here.

The Attribute Internal
Internal is more restrictive than Public, as the code cannot be in a different package than the definition.  It is more lenitant when compared to Protected, as it can be in a different class in the same package as the definition.

Copies and References
Copies are created for String, Boolean, Number, int, or unint.  Custom classes and all other classes use references.

Day 1…a bit disappointed
Both the books I’m reading is heavy on the syntax without any programs to build and try out.  Maybe it will get better in the next 1 or 2 chapters.

Bulk Image Upload and Insert in WordPress

Occasionally I’ll need to upload and insert more than 100 photos into a single WordPress post. I haven’t seen any good bulk image upload and insert plugins, so it has been a gruesome task. However, with the use of Perl, BIMP Lite, and FileZilla the annoying aspects of this task have been automated.

Requirements
1. BIMP Lite – a free tool for Windows which I found from Smashing Magazine’s 15 Useful Batch Image Processors
2. An installation of Perl on your computer
3. FileZilla, or any FTP client
4. The Image-Size Perl Module, created by Randy R Jay, installed on your computer

The Basic Idea
The idea is to have a folder with all the images you need to upload. First, you use BIMP Lite to generate the thumbnails for you into a subdirectory called “thumb.” Next, upload all these images to your WordPress image uploads directory using your FTP client. Finally, use the Perl script below to generate the WordPress HTML.

Generator.pl

# Generates the HTML for bulk image upload and insertion for WordPress
#
# @author Cody (codelol.com)
# @date February 21, 2009

use Image::Size;

# The directory where the images are stored
$image_dir = ".";

# The directory relative to $image_dir where the thumbnails are stored
$thumb_dir = '/thumb/';

# The WordPress image uploads directory
# e.g. http://example.com/wp-contents/uploads/2009/02/
$wp_image_dir = "http://example.com/wp-contents/uploads/2009/02/";

# Currently only uses JPEG.  Update the regular expression as needed
opendir(DIR, $image_dir);
@files = grep(/\.jpg$/,readdir(DIR));
@files = sort(@files);

opendir(DIR, $image_dir.$thumb_dir);
@files_thumb = grep(/\.jpg$/,readdir(DIR));
@files_thumb = sort(@files_thumb);

$output = "";

$size = @files;
$size_thumb = @files_thumb;

# A very BASIC check to ensure there is the correct number of thumbnails
if($size != $size_thumb)
{
  # If the size does not match, exit
  die "The number of thumbnails (".$size_thumb.
  ") does not match the number of images. (".$size.")\n";
}

# Generate the WordPress code
for($i=0;$i<$size;$i++)
{
  # Determine the dimensions of the thumbnail
  ($width, $height) = imgsize($image_dir.$thumb_dir.$files_thumb[$i]);

  # Escape a single space by using HTML URL encoding
  # You may need to escape additional characters if necessary
  $file_escaped = $files[$i];
  $file_escaped =~ s/ /%20/g;
  $files_thumb[$i] =~ s/ /%20/g;

  $output .= '<a href="'.$wp_image_dir.$file_escaped.'">';
  $output .= '<img src="'.$wp_image_dir.$files_thumb[$i].'" ';
  $output .= 'alt="'.$files[$i].'" title="'.$files[$i].'" ';

  $output .= 'width="'.$width.'" height="'.$height.'" ';

  # You may need to edit the class attribute if you aren't using
  # algincenter or size-medium
  $output .= 'class="aligncenter size-medium" /></a>';
}
print $output;

print "WordPress code generated successfully!\n";
open(FILE, ">generate.txt") or die("Error");
print FILE $output;

Instead of manually uploading and inserting each image, these steps will basically automate most of these tasks…so you can go drink a beer and watch TV while waiting for the images to be uploaded by FTP.

Instructions
1. Make sure you meet the requirements
2. Run BIMP Lite to create thumbnails in a subdirectory in your images directory
3. Upload the original images and thumbnails to your WordPress images upload directory
4. Copy the Generator.pl into your images directory
5. Update $wp_image_dir in Generator.pl to your image upload directory
5. Execute the Perl file by double clicking on it
6. The generated HTML will be put into a new file called generate.txt
7. Open generate.txt and copy this code into your WordPress post

Greasemonkey and Productivity

A few months ago, I read a news article on Lifehacker which mentioned the ban time-wasting web sites Greasemonkey script.

My lack of will-power and my growing obsession with blogs and forums has gone to such a pathetic state, that I must resort to productivity increasing scripts.

I’ve never used Greasemonkey before but now I see how useful it can be.

 Page 2 of 3 « 1  2  3 »
Get updates as often as we post! Subscribe to our full feed or comments feed.