Sunday, March 15, 2015

Experimenting with ncurses on Linux in Eclipse

Today I'll talk about a new project inspired by a co-worker: to write a "graphical" application to monitor network traffic from the console of an embedded device such as a data gateway or a raspberry pi.
The goal is not to produce a star software but to get back in shape programming with C after years of Perl and Python and learning about ncurses along the way.


A few words about ncurses

ncurses is a C library allowing you to abstract the terminal type while writing ASCII graphical interface. It is used in the Linux kernel menuconfig utility for example.
More information about ncurses can be found on the GNU ncurses page and its wikipedia article.


Setting up the environment

I will start with a Hello World program inspired directly from the Linux documentation project.
#include <stdlib.h>
#include <curses>

int main(void)
{
    initscr();
    printw("Hi!\n");
    refresh();
    getch();
    endwin();

    exit(0);
}

      1. Installation of the ncurses library

Your Linux distribution should come with ncurses already installed. If it is not there, follow the nstructions found in paragraph "1.3. Where to get it" of the tldp ncurses how-to webpage.


      2. Creating a new Eclipse project

I am using the Kepler version of Eclipse. First navigate through the top menu commands:
File > New > C project
Then give your project a name (mine is curses), project type = "empty project", toolchains = "Linux GCC" (for building an executable for your dev machine).
Copy-paste or type the Hello World program above in main.c file for example.


      3. Adding the ncurses library to the ld path

Right-click on your project name from the project explorer window and select the bottom option for Properties.
In "C/C++ General" section, select "Paths and Symbols" then click on the "Libraries" tab.
Add ncurses to the list. End result should look as shown below:

Adding the ncurses library to your project


      4. Building your project

Nothing tricky here: Right-click on your project name then "Build Project" or use the hammer icon from the top menu. Build result, as well as gcc command line, will be displayed in a console:

Project has been built


      5. Launching a terminal from Eclipse

To avoid switching back and forth between Eclipse and your terminal, I have used a trick found on stack Overflow: from the top menu, select Run > External Tools > External Tools Configuration...
Create a new configuration (here: Command_prompt) with the location of your terminal binary, the directory where your executable is and additional arguments for your terminal program to execute your program:

Adding external tool configuration for your terminal


To launch your program, Run > External Tools > Command_prompt (or the name you gave your configuration) and a terminal window should appear with your ncurses program running.

Run Command_prompt opens a new terminal with your program


That's it. You are now set to explore ncurses tutorials that you can find on the web!




Saturday, August 18, 2012

edX: College-quality computer science education


A soft  drum roll can be heard throughout the realm. It slowly grows louder as a herald appears on your screen. The Announcer sounds her trumpet, clears her throat. Drums stop and she exclaims:

    - "The blog heretofore known as 'Damien Learns Perl' is now to be referred to as 'Damien Learns Programming'. This broader title means greater variety and more life (or so it is hoped) to the speculations and musings of an eternal student of computer programming."

Crowd cheers.

    - "No camel was hurt in this process. Go home and spread the good word."

The herald lets out a content sigh then disappears in a puff.


-------

I feel like writing about other topics than just Perl. I contemplated starting a new blog for a few seconds but it made more sense to give a new direction to this site.


Evolve or become irrelevant

This could be the motto of the computing/computer science/IT industry.
Tools keep popping out of thin air, new programming languages are invented, operating systems evolve rapidly, new devices appear that can be programmed, etc.
Those changes answer people's ever demanding requests.

You want to keep up but how to go about updating your skills?

The best motivator is to build something that you need. When my wife became pregnant, I started to build a "Baby Tracker" app to learn C++ and to keep tab on the baby's vaccination schedule, teeth growth, stool color, etc. I eventually abandoned the project because (believe it or not) taking actual care of my first daughter became a higher priority. By the time I had bandwidth again, the "baby tracker" need was moot.
Having a purpose makes you want to keep learning and carry on after you have hit a few too many obstacles.

This self-learning path resembles an obstacle course: the end line is visible but you first have to jump over all those hurdles to attain it.

Another way to learn new skills is through online education. Several sites already exist that put emphasis on learning in a fun way:

  • Codecademy (for beginning web programmers)
  • Khan Academy (originally created to teach mathematics but now proposing many subjects, including computer science)
  • Memrise (to help you learn vocabulary for new languages, including programming languages)
  • LCodeTHW (despite its name, offers a great way to start learning about programming)
  • etc.
Those are quality sites and I encourage you to visit them if you are looking for a first acquaintance with a new topic.
However, I want to share here something that I am truly excited about: online computer science college-quality education for free. Back in 2000 I got involved in the PEOI project whose mission was to bring professional education online to the masses (provided access to an internet link). The site still exists but the design has not evolved since its creation and course content was created by volunteers. Its noble goal though has found a champion today: edX.


Get a quality education

edX is a joint online interactive educational project from MIT, Berkeley and Harvard University, some of the best US universities. You can earn certificates along with knowledge. New classes are about to start this fall and include:

So go forth and upgrade your computing education. 
Evolve and ride the relevance wave!



edX: College-quality computer science education


A soft  drum roll can be heard throughout the realm. It slowly grows louder as a herald appears on your screen. The Announcer sounds her trumpet, clears her throat. Drums stop and she exclaims:

    - "The blog heretofore known as 'Damien Learns Perl' is now to be referred to as 'Damien Learns Programming'. This broader title means greater variety and more life (or so it is hoped) to the speculations and musings of an eternal student of computer programming."

Crowd cheers.

    - "No camel was hurt in this process. Go home and spread the good word."

The herald lets out a content sigh then disappears in a puff.


-------

I feel like writing about other topics than just Perl. I contemplated starting a new blog for a few seconds but it made more sense to give a new direction to this site.


Evolve or become irrelevant

This could be the motto of the computing/computer science/IT industry.
Tools keep popping out of thin air, new programming languages are invented, operating systems evolve rapidly, new devices appear that can be programmed, etc.
Those changes answer people's ever demanding requests.

You want to keep up but how to go about updating your skills?

The best motivator is to build something that you need. When my wife became pregnant, I started to build a "Baby Tracker" app to learn C++ and to keep tab on the baby's vaccination schedule, teeth growth, stool color, etc. I eventually abandoned the project because (believe it or not) taking actual care of my first daughter became a higher priority. By the time I had bandwidth again, the "baby tracker" need was moot.
Having a purpose makes you want to keep learning and carry on after you have hit a few too many obstacles.

This self-learning path resembles an obstacle course: the end line is visible but you first have to jump over all those hurdles to attain it.

Another way to learn new skills is through online education. Several sites already exist that put emphasis on learning in a fun way:

  • Codecademy (for beginning web programmers)
  • Khan Academy (originally created to teach mathematics but now proposing many subjects, including computer science)
  • Memrise (to help you learn vocabulary for new languages, including programming languages)
  • LCodeTHW (despite its name, offers a great way to start learning about programming)
  • etc.
Those are quality sites and I encourage you to visit them if you are looking for a first acquaintance with a new topic.
However, I want to share here something that I am truly excited about: online computer science college-quality education for free. Back in 2000 I got involved in the PEOI project whose mission was to bring professional education online to the masses (provided access to an internet link). The site still exists but the design has not evolved since its creation and course content was created by volunteers. Its noble goal though has found a champion today: edX.


Get a quality education

edX is a joint online interactive educational project from MIT, Berkeley and Harvard University, some of the best US universities. You can earn certificates along with knowledge. New classes are about to start this fall and include:

So go forth and upgrade your computing education. 
Evolve and ride the relevance wave!



Sunday, July 1, 2012

Rebel Code

I just finished reading "Rebel Code" by Glyn Moody and I really enjoyed it so here's a short review.
"Rebel Code" is an older book which relates the story of high profile Open Source projects from the beginning of the free software movement up until 2001.
I picked it up at a local Atlanta library and I had little idea about what I was going to find inside. I saw that it talked about the story of GNU/Linux and that was enough to arouse my curiosity.
The book is a great read for those wanting a short history of Open Source development. The author did a good job transitioning between various stories (The Free Software Foundation, Linux, Perl, etc.) and the result is a fun and instructive read. I finished it in 4 days.
It makes me want to read an hypothetical volume two that would span from 2001 up to the present day.

I highly recommend "Rebel Code - Inside Linux and the Open Source revolution" to all those who want to learn about the mythical free software coding feats, all the way from Richard Stallman's and Linus Torvald's first works.


Rebel Code

I just finished reading "Rebel Code" by Glyn Moody and I really enjoyed it so here's a short review.
"Rebel Code" is an older book which relates the story of high profile Open Source projects from the beginning of the free software movement up until 2001.
I picked it up at a local Atlanta library and I had little idea about what I was going to find inside. I saw that it talked about the story of GNU/Linux and that was enough to arouse my curiosity.
The book is a great read for those wanting a short history of Open Source development. The author did a good job transitioning between various stories (The Free Software Foundation, Linux, Perl, etc.) and the result is a fun and instructive read. I finished it in 4 days.
It makes me want to read an hypothetical volume two that would span from 2001 up to the present day.

I highly recommend "Rebel Code - Inside Linux and the Open Source revolution" to all those who want to learn about the mythical free software coding feats, all the way from Richard Stallman's and Linus Torvald's first works.


Saturday, October 29, 2011

Google AI Challenge, Fall 2011 edition

It is this time of the year again. Google has set up its Fall 2011 Artificial Intelligence (AI) challenge.
The goal is to protect your ant hill and gather food while destroying the enemy's hills.

There are starter packages for 25 languages, including Perl, for Windows and Linux environments.

The contest closes on December 18th. Submit your best efforts to move Perl up the rankings!
Here is the ranking table for Perl, there were 35 entries at the moment this post was written with a best position of 198th by grand_sbor.

Google AI Challenge, Fall 2011 edition

It is this time of the year again. Google has set up its Fall 2011 Artificial Intelligence (AI) challenge.
The goal is to protect your ant hill and gather food while destroying the enemy's hills.

There are starter packages for 25 languages, including Perl, for Windows and Linux environments.

The contest closes on December 18th. Submit your best efforts to move Perl up the rankings!
Here is the ranking table for Perl, there were 35 entries at the moment this post was written with a best position of 198th by grand_sbor.

Tuesday, October 25, 2011

The Perl Weekly newsletter


When it comes to getting fresh Perl news, the community provides many resources.
It can be time consuming to sift through all of them and there is always the chance that you will miss interesting tidbits about your favorite scripting language.

Lo and behold, Gabor Szabo (szabgab) comes to your rescue. His weekly Perl Weekly newsletter is an aggregate of all the recent happenings in the world of Perl programming. He is doing a great job of extracting the big news for us in order to save time to everyone.
You can read the newsletter on the web or subscribe to receive it in your mailbox. 13 issues are out already!


The Perl Channel

Gabor has been keeping himself busy recently, having the courage to stand in front of the spotlights. A few months ago, he started a series of Perl videos on YouTube. There are, as of the end of October 2011, 25 segments of about 15 minutes.
If you are starting development of Perl programs on Windows, this is another great resource for learning. There are beginner lessons, Padre tutorials, Perl 6 videos and more.
If you prefer your information presented by a live person (as opposed to read from a document), I recommend that you check it out now. Even better, watch it on your phone during your commute (WARNING: do not drive and Perl at the same time!).



The Perl Weekly newsletter


When it comes to getting fresh Perl news, the community provides many resources.
It can be time consuming to sift through all of them and there is always the chance that you will miss interesting tidbits about your favorite scripting language.

Lo and behold, Gabor Szabo (szabgab) comes to your rescue. His weekly Perl Weekly newsletter is an aggregate of all the recent happenings in the world of Perl programming. He is doing a great job of extracting the big news for us in order to save time to everyone.
You can read the newsletter on the web or subscribe to receive it in your mailbox. 13 issues are out already!


The Perl Channel

Gabor has been keeping himself busy recently, having the courage to stand in front of the spotlights. A few months ago, he started a series of Perl videos on YouTube. There are, as of the end of October 2011, 25 segments of about 15 minutes.
If you are starting development of Perl programs on Windows, this is another great resource for learning. There are beginner lessons, Padre tutorials, Perl 6 videos and more.
If you prefer your information presented by a live person (as opposed to read from a document), I recommend that you check it out now. Even better, watch it on your phone during your commute (WARNING: do not drive and Perl at the same time!).



Sunday, June 19, 2011

CPAN tester

Tweets from Gabor Szabo got me curious about CPAN tests.

I work in software testing and I recognize the value of automatic tests and reports.

After checking out the CPAN Tester wiki, I installed and configured CPAN::Reporter. If I understand well, this module will automatically send the test results run on your computer to the CPAN test database when you install a new Perl module. The authors and users can see reports on all tested configurations.

This is a simple way to give back to the Perl community for those nice CPAN modules.



The process involved starts with installing the CPAN::Reporter module on your machine and saving the configuration file.

Note: When looking for the metabase-profile tool on Windows, check in the C:\Perl\cpan\build\Metabase-Fact-0.019\script directory.



You can also set up a machine to continuously test CPAN Perl modules.



Each tester registered to the CPAN gets listed according to the number of test reports sent back to the server. Gabor is making a game of climbing the ranks. Who will join the testers' army and try to beat him?



Edit June 22nd: As of now, I am registered on CPAN testers and have sent my first test report. You can check that your setup is correct by searching through the log tail. Here's my entry:

22T07:21:29Z] [dlp] [pass] [DAGOLDEN/CPAN-Reporter-1.1902.tar.gz] [MSWin32-x86-multi-thread] [perl-v5.10.1] [ddfed1a4-6e1e-1014-9872-6ddfc9b97d9d] [2011-06-22T07:21:29Z]



Let's work our way up!

CPAN tester

Tweets from Gabor Szabo got me curious about CPAN tests.
I work in software testing and I recognize the value of automatic tests and reports.
After checking out the CPAN Tester wiki, I installed and configured CPAN::Reporter. If I understand well, this module will automatically send the test results run on your computer to the CPAN test database when you install a new Perl module. The authors and users can see reports on all tested configurations.
This is a simple way to give back to the Perl community for those nice CPAN modules.

The process involved starts with installing the CPAN::Reporter module on your machine and saving the configuration file.
Note: When looking for the metabase-profile tool on Windows, check in the C:\Perl\cpan\build\Metabase-Fact-0.019\script directory.

You can also set up a machine to continuously test CPAN Perl modules.

Each tester registered to the CPAN gets listed according to the number of test reports sent back to the server. Gabor is making a game of climbing the ranks. Who will join the testers' army and try to beat him?

Edit June 22nd: As of now, I am registered on CPAN testers and have sent my first test report. You can check that your setup is correct by searching through the log tail. Here's my entry:
22T07:21:29Z] [dlp] [pass] [DAGOLDEN/CPAN-Reporter-1.1902.tar.gz] [MSWin32-x86-multi-thread] [perl-v5.10.1] [ddfed1a4-6e1e-1014-9872-6ddfc9b97d9d] [2011-06-22T07:21:29Z]

Let's work our way up!

Sunday, January 16, 2011

Modern Perl

Last November, chromatic announced the availability of his book on Modern Perl. His creation is geared towards beginner Perl programmers who want to learn how to use the language in the most effective way, based on experienced programmers' know-how.
The book is also recommended to anyone who wrote Perl scripts in the distant past and wants to take advantage of Perl 5's latest features and good coding practices.
You can download the pdf file from the companion site.

Modern Perl

Last November, chromatic announced the availability of his book on Modern Perl. His creation is geared towards beginner Perl programmers who want to learn how to use the language in the most effective way, based on experienced programmers' know-how.
The book is also recommended to anyone who wrote Perl scripts in the distant past and wants to take advantage of Perl 5's latest features and good coding practices.
You can download the pdf file from the companion site.

Friday, February 12, 2010

Google AI Challenge: move Perl up the charts!

If you get snowed in this week-end, here's a fun way to pass the time: enter the Google AI challenge!
Organized by the University of Waterloo (Canada) and sponsored by Google, this challenge lets you compete in a game of Tron against other people's algorithms from a wide selection of programming languages.
Sadly, Perl is not widely represented and the highest Perl score is at the 176th rank as of this Friday night.
The contest ends on the 26th of this month. Who will care to test their Perl skills?

Google AI Challenge: move Perl up the charts!

If you get snowed in this week-end, here's a fun way to pass the time: enter the Google AI challenge!
Organized by the University of Waterloo (Canada) and sponsored by Google, this challenge lets you compete in a game of Tron against other people's algorithms from a wide selection of programming languages.
Sadly, Perl is not widely represented and the highest Perl score is at the 176th rank as of this Friday night.
The contest ends on the 26th of this month. Who will care to test their Perl skills?

Tuesday, December 22, 2009

Reentrant perldoc help

Just want to elaborate on a tip that I gave back in March.
I have a somehow standard way of displaying help for my scripts:
I display the POD data using the $0 perl internal variable.
$0 (or $PROGRAM_NAME if you are using the use English; pragma) "contains the name of the file containing the Perl script being executed", to quote the Camel book.

The help section is printed on screen if a script requires a parameter but none has been provided. I used to write it this way for a command line accepting only one parameter:
sub get_help {
system("perldoc $0");
exit;
}
my $arg = $ARGV[0] || &get_help;

However, I recently ran into a slight difficulty.
On Windows, $0 contains the disk letter followed by the complete path and finally the file name. For example:
C:\Users\Damien\Desktop\test coverage\Perl Scripts

Notice the white spaces in the directory names.
When trying to perldoc this particular file name, I would get an error stating that there is no documentation for "C:\Users\Damien\Desktop\test", "coverage\Perl" nor "Scripts".

My first reaction was simply to surround the $0 file name with "" as shown below:
sub get_help {
system("perldoc \"$0\"");
exit;
}
my $arg = $ARGV[0] || &get_help;

[Added Dec. 29th]
However, an enlightened reader pointed me to the best solution:
sub get_help {
system("perldoc", $0);
exit;
}
my $arg = $ARGV[0] || &get_help;

Using the list argument form of the system function avoids relying on the shell to run your command.

Reentrant perldoc help

Just want to elaborate on a tip that I gave back in March.
I have a somehow standard way of displaying help for my scripts:
I display the POD data using the $0 perl internal variable.
$0 (or $PROGRAM_NAME if you are using the use English; pragma) "contains the name of the file containing the Perl script being executed", to quote the Camel book.

The help section is printed on screen if a script requires a parameter but none has been provided. I used to write it this way for a command line accepting only one parameter:
sub get_help {
system("perldoc $0");
exit;
}
my $arg = $ARGV[0] || &get_help;

However, I recently ran into a slight difficulty.
On Windows, $0 contains the disk letter followed by the complete path and finally the file name. For example:
C:\Users\Damien\Desktop\test coverage\Perl Scripts

Notice the white spaces in the directory names.
When trying to perldoc this particular file name, I would get an error stating that there is no documentation for "C:\Users\Damien\Desktop\test", "coverage\Perl" nor "Scripts".

My first reaction was simply to surround the $0 file name with "" as shown below:
sub get_help {
system("perldoc \"$0\"");
exit;
}
my $arg = $ARGV[0] || &get_help;

[Added Dec. 29th]
However, an enlightened reader pointed me to the best solution:
sub get_help {
system("perldoc", $0);
exit;
}
my $arg = $ARGV[0] || &get_help;

Using the list argument form of the system function avoids relying on the shell to run your command.

Monday, September 14, 2009

Installing Padre on Windows Vista

Last week, I (re-)installed Padre on my Windows Vista machine. Padre is a development environment geared towards Perl programmers, with a focus on Perl beginners.
I use the Strawberry Perl distribution (5.10.0.6) and read that it was a breeze to install Padre.
Well, it almost is.
I opened the cpan shell (typing cpan in a DOS shell is all it takes if C:\strawberry\perl\bin is in your PATH).
The regular install (install Padre) failed because of a dependency on Wx::Perl::ProcessStream not building. Just force install that module (force install Wx::Perl::ProcessStream) then install Padre (a ticket has already been written for the issue).
It works like a charm, no need to force install Padre anymore!
So if you are a Perl beginner and want to use an editor written in Perl + suited for Perl tasks, you have no excuse anymore.

If you already have Padre and want to update it to version 0.46 released today (September 14th 2009), type upgrade Padre in your cpan shell. Better yet, you can update directly from the Padre Plugins menu: Plugins>Module Tools>Install CPAN Module and type Padre.

Installing Padre on Windows Vista

Last week, I (re-)installed Padre on my Windows Vista machine. Padre is a development environment geared towards Perl programmers, with a focus on Perl beginners.
I use the Strawberry Perl distribution (5.10.0.6) and read that it was a breeze to install Padre.
Well, it almost is.
I opened the cpan shell (typing cpan in a DOS shell is all it takes if C:\strawberry\perl\bin is in your PATH).
The regular install (install Padre) failed because of a dependency on Wx::Perl::ProcessStream not building. Just force install that module (force install Wx::Perl::ProcessStream) then install Padre (a ticket has already been written for the issue).
It works like a charm, no need to force install Padre anymore!
So if you are a Perl beginner and want to use an editor written in Perl + suited for Perl tasks, you have no excuse anymore.

If you already have Padre and want to update it to version 0.46 released today (September 14th 2009), type upgrade Padre in your cpan shell. Better yet, you can update directly from the Padre Plugins menu: Plugins>Module Tools>Install CPAN Module and type Padre.

Saturday, September 5, 2009

Learning Perl for cheap!

Brian d foy announced on his blog a price drop on O'Reilly Perl books, including his own "Learning Perl" book that he co-wrote with Randal Schwartz and Tom Phoenix.
With the discount code found in the comment section, the grand total is... $6.49 for a must have in every programmer's library!
Credits to RAT for announcing the news first.