Perl applets extend a platform-independent desktop panel

Games for Speculators

© cornelius, Fotolia

© cornelius, Fotolia

Author(s):

One panel has a neat collection of applets and another has spectacular looks – but a combination of the two is rare. Now help draws nigh for the desktop: PerlPanel is extensible with do-it-yourself widgets.

Regardless of whether you use Gnome or KDE, every desktop offers panels. They dock at the bottom or top of your screen, giving a home to menus and icons for launching programs, showing active applications in taskbars, or helping you switch between virtual desktops. The aim of the PerlPanel project is to use Perl to provide a platform-independent panel. At the same time, PerlPanel aims to let users add their own applets, simply by hashing up a couple of scripts.

Trial Run

On Ubuntu, you can install PerlPanel and the Perl modules on which it depends by typing sudo apt-get install perlpanel at the command line. To take it for a trial run, type /usr/bin/perlpanel. Figure 1 shows the panel GUI at the bottom of the desktop.

If the space at the bottom of the screen is already occupied by another panel, you can move it to the right or left border, or just ditch it if you feel brave enough to burn some bridges behind you.

Figure 1: PerlPanel lets you add do-it-yourself applets.

"Ticker" Widget

If you would like to keep track of share prices, a standard application might not be the best option for you as share price ticker windows tend to hide other applications. Instead, a panel applet might be the ideal choice for the budding investor as it lets you keep a constant eye on the latest share price developments, no matter which application you are currently working with. To allow this to happen, the applet checks the share prices you are interested in by querying Yahoo Finance every 5 minutes and displays the results on screen (the ticker symbols to watch are stored in a local configuration file, ~/.ticker-rc, in your home directory). The applet ignores comment and empty lines in the configuration file and expects a ticker symbol in every line. Figure 1 shows the applet display for selected prices in the panel on the desktop.

The getquote script in Listing 1 queries the Yahoo server to reel in share prices; it expects a list of share symbols on the command line, and outputs the latest share prices line by line. The applet code proper is shown in Listing 2, Ticker.pm. The applet simply parses the configuration file, calls the getquote script at five minute intervals, and refreshes the display in the PerlPanel with the returned values. If an impatient user clicks the applet instead, it assumes they can't wait until the next update; in this case, the applet rushes off to ask the Yahoo server and updates the panel display straight away.

Listing 1

getquote

01 #!/usr/local/bin/perl -w
02 use strict;
03 use Finance::YahooQuote;
04
05 $Finance::YahooQuote::TIMEOUT
06   = 60;
07
08 exit 0 unless @ARGV;
09
10 my @quotes = getcustomquote(
11  [@ARGV],
12  [
13   "Symbol",
14   "Last Trade (Price Only)"
15  ]
16 );
17
18 if (!exists $quotes[0][1]) {
19  die
20    "Fetching quote failed\n";
21 }
22
23 for my $quote (@quotes) {
24  print "@$quote\n";
25 }

Get Those Quotes!

The share symbols passed in to getquote at the command line are stored in the @ARGV array in typical Perl style. The getcustomquote function in the Finance::YahooQuote module retrieves the share prices from the Yahoo server, specifying that it is only interested in the fields Symbol (the share name symbol specified at the command line), and Last Trade (Price Only). Without this restriction, Yahoo would return a whole bunch of data the applet doesn't need at every getquote call, so it's better to say up front what you need and what you don't.

If a transmission error occurs, a single value with an error message is returned; if the query is successful, Yahoo returns an array whose entries are pointers to arrays with the symbol and last trade values. Line 18 checks if the returned results really are two-column entries and bails out immediately if not. Line 5 sets a timeout of 60 seconds; if a network error occurs, this will prevent the script from waiting for ever. Share prices that reach the script are sent to its standard output by the for loop in Line 23. and formatted as Symbol Last-Trade on every line of output.

Read full article as PDF:

072-076_perl.pdf  (802.66 kB)

Related content

  • Perl: Portfolio Watch

    We'll show you a Perl script that helps you draw area graphs to keep track of your portfolio's performance.

  • Perl: Personal Finances

    A helpful Perl script gives you an immediate overview of your financial status, adding the balances of multiple accounts and share depots. It even allows users to add their own plugins.

  • deskTOPia

    Even the smallest desktop has room

    for a panel. If your choice of a window

    manager does not support a suitable

    panel alternative,you can always add

    a panel like SuxPanel. Suxpanel provides

    many useful features for the

    desktop,and it even it even supports

    plug-ins.

  • Perl Snapshot: Debugging

    Some developers condemn the use of debuggers, but in many cases, a debugger is a programmer’s last hope. In this Month’s Perl Snapshot, we’ll look at Perl’s integrated debugger.

  • strace

    Get started with strace by examining a pair of "Hello World" programs. Next month, in the second part of this two-part series, I'll take a deeper look at strace output.

comments powered by Disqus

Direct Download

Read full article as PDF:

072-076_perl.pdf  (802.66 kB)

News

njobs Europe
What:
Where:
Country:
Njobs Netherlands Njobs Deutschland Njobs United Kingdom Njobs Italia Njobs France Njobs Espana Njobs Poland
Njobs Austria Njobs Denmark Njobs Belgium Njobs Czech Republic Njobs Mexico Njobs India Njobs Colombia