Open Chinstrap

April 11, 2008

Error Installing Perl Tk on Ubunutu

Filed under: Linux, Perl, Ubuntu — t-roy @ 11:26 pm

I received the following error installing Tk from CPAN:

CPAN reported “Problem gettimeofday()” which seems to indicate that I was missing some include files. I found the following post which recommended installing the libx11-dev packages which could include the required development header files:

http://www.perlmonks.org/index.pl?node_id=673577

I used the Synaptic Package manager to install libx11-dev. I then tried installing Tk again using CPAN:

$ sudo cpan -i Tk

The install progressed much further than it did the first time but it still ended up failing with the following error:

make[1]: *** [ClientWin.o] Error 1k …

I discovered a Ubuntu post related to this error that recommended installing TK using apt-get:

$ sudo apt-get -install TK

But when I went to install the package from the command line there was no package called TK. Finally I did a search for Tk using the Synaptic Package manager. I found a package called perk-tk and installed it.

 

 

 

 

Run the following “Hello World” test program from this TK User Guide:

#!/usr/bin/perl -w

use Tk;

use strict;

my $mw = MainWindow->new;
$mw->Label(-text => ‘Hello, world!’)->pack;
$mw->Button(
-text => ‘Quit’,
-command => sub { exit },
)->pack;
MainLoop;

Troy

April 7, 2008

Configuring Eclipse EPIC on Ubuntu

Filed under: Apache, Linux, Perl, Ubuntu, Web Development — t-roy @ 7:14 pm

E-P-I-C is a good Perl plugin for Eclipse (Version: 3.3.2 Build id: M20080221-1800) . You can use EPIC to develop perl scripts and Perl CGI (Common Gateway Interface) scripts. To install the plugin you can use the update site located here:

http://e-p-i-c.sf.net/updates

Creating a Perl project is very straight forward: File>New>Perl Project

New Perl Project

 

 

 

I will focus on configuring a CGI script since the regular Perl scripts are fairly straight forward . First we need to create our script by right-clicking on the project folder and selecting New>Perl File:

New Perl File

 

 

 

Name the file test.cgi and copy and paste the following simple CGI script:

#! /usr/bin/perl

print “Content-type: text/plain \n\n”;
print “Hello World!\n”;
print “Script Name: $ENV{SCRIPT_NAME}”;

Save the file (CTRL-s) and now we are ready to set up the Perl CGI Configuration. But before we do that it is important to record the full path of the Perl Eclipse project and remember the name of the script you want to run. Eclipse usually creates a workspace directory in your home directory unless you specify otherwise. In my case my workspace is located in:

/home/scottt/workspace

Therefore my Perl project is located in:

/home/scottt/workspace/perlwebscripts/

And the path to my CGI script is:

/home/scottt/workspace/perlwebscripts/test.cgi

Now that we have this information we can configure our CGI script. Right-click on the project folder or any file in the project and select Run As>Open Run Dialog…. :

Perl CGI Configuration

 

 

 

 

In the Main tab you need to select the project folder name in my case it would be perlwebscripts. The Web Server tab (shown above) contains the following information:

  • HTML Root Directory: /home/scottt/workspace/perlwebscripts
  • HTML Startup File: /home/scottt/workspace/perlwebscripts/test.cgi
  • CGI Root Directory: /home/scottt/workspace/perlwebscripts

All paths are related to your Eclipse CGI project. The final thing to configure is the browser tab which is where the result of the CGI Script is displayed. I used the Firefox adapter. I was not able to get the Internal Perl Browser to work. The Firefox browser works fine but it is a bit of a pain to Alt-Tab between Eclipse and Firefox while you are developing. Hit apply once the configuration is finished and then you can click Run As. Here is a screenshot of the test.cgi script :

CGI Test Script

 

 

 

 

EPIC also provides a good user guide which you can download from here.

Troy

April 3, 2008

Using the split function with a Scalar Variable

Filed under: Linux, Perl — t-roy @ 11:27 pm

The split function is very useful for parsing or “splitting” a string into a list of strings based on a regular expression (delimiter). For example you could have a string like this:

“apple, orange, pear”

To parse this list with the split function you would do this:

print split /,/ , “apple, orange, pear”;

This will print a list with 3 elements. If you assign the result of the split function to a scalar you will simply get the number of elements or the count of items in the list. Usually you assign the result of the split function to an array:

@mylist = split /,/, “apple, orange, pear”;

Once you do this you could then assign it to a scalar like this:

$fruit = $mylist[0];

This would assign the value of apple to the $fruit scalar. However, I find that I use the split function to parse one value from a string. For example the url from a web page document:

<a href=”http://mysite.campfirenow.com/”>redirected</a>

In this case I want to parse out the http://mysite.camfirenow.com value using the split function and assign it to a scalar all in one line of code. To accomplish this you can write the code like this:

$address = (split /”/, “<a href=”http://mysite.campfirenow.com/”>redirected</a>”)[1];

In this example it is important to use the second element of the list. The first element ([0]) is undefined or empty. The second element ([1]) contains the url value.

Troy

Making a POST with HTTP::Lite

Filed under: Linux, Perl — t-roy @ 7:45 am

HTTP::Lite is a Perl module that, and as the name implies, it is a lightweight implementation for HTTP support. I wanted to write a program to automate a GET or POST to a website. For example you may want to have a script that runs and logs onto a site with your username and password and then grabs some information like stock or weather data. This data could then be used in a report or loaded into a database.

To try this out you can use any website that allows you to fill out a form. I used my account at www.campfirenow.com . Campfirenow is a web service which provides online chat. You can sign up for a free user account if you want to try it out but any website with a form will work for this example. Make sure that HTTP::Lite is installed on your system:

perl -MHTTP::Lite -e 1

If this does not return an error then the module is installed otherwise you can view this post if you do not know how to install it . Before we dive into the code here is a quick check list for the program

  • Import the HTTP::Lite module
  • Create a hash for the input values in the form (its a good idea to use a simple form)
  • POST the form values to the webpage
  • Display the response from the web site (Display the web page)

# Import the module
use HTTP::Lite;

# Create a hash for the form
# Input: email_address
# Input: password

%login = (
“email_address” => “myemail\@domain.com”,
“password” => “mypassword”
);

$http = new HTTP::Lite;

# Create the POST for the form

$http->prepare_post(\%login);

$req = $http->request(“http://myaccount.campfirenow.com/login”)
or die “Error retrieving URL: $!”;
print “req: $req\n”;

# Display the web page (response)

print $http->body() , “\n”;

Unfortunatley the output from this example is not too exciting. Once the user logs in they are redirected to http://myaccount.campfirenow.com. So the HTML that is displayed is for the redirect. Secondly in order to make additional request to the web site a session_id cookie must be passed for all future requests. In my next post I will discuss how to get and set a cookie from the header using the HTTP::Lite module.

This process is more involved. First I will have to login to the site and then retrieve the session cookie from the response header. When I make another request I will have to set the cookie in the request header or the request will fail (it results in a redirect or a 302 STATUS) . This will allow me to extract data from other web pages on the campfirenow web site.

Troy

Check for Existing CPAN Modules

Filed under: Linux, Perl — t-roy @ 1:48 am

Before installing a CPAN Module it is important to check your system to see if it already exists. In my case I wanted to install the Syntax::Highlight::HTML module . First I run the following command to see if it exists on my system:

$ perl -MSyntax::Highlight::HTML -e 1

If I do not receive an error then the module is already installed. If I receive an error that looks something like this:

Can’t locate Syntax/Highlight/HTML.pm in @INC ….

This could mean that the module is not installed or the module is not in the @INC path.

Check for Existing CPAN Modules

To install the module type:

$ sudo cpan -i Syntax::Highlight::HTML

Install Module with Using cpan

Now the module is installed and ready to use. For more information about installing perl modules type:

$ perldoc perlmodinstall

or try this website

http://theoryx5.uwinnipeg.ca/CPAN/perl/pod/perlmodinstall.html

Troy

March 26, 2008

Installing perldoc on Ubuntu from the Command Line

Filed under: Linux, Perl, Ubuntu — t-roy @ 10:29 pm

perldoc is not installed in Unbuntu 7.10 or at least it is not completely installed.  When I typed:

$ perldoc

I recieved the following message:

You need to install the perl-doc package to run this program.

However, when I type whereis perldoc the system does return the path of the perldoc program.  To install the perldoc package from the command line type:

$ sudo apt-get install perl-doc

perldoc install

Now perldoc should work.

Troy

Blog at WordPress.com.