Perl applets extend a platform-independent desktop panel
Strictly by the Rules
Listing 2 Ticker.pm contains the applet code and has to follow the PerlPanel's rules. This includes a module in the PerlPanel::Applet::Name name space, a new() constructor, and an initial configure() function, which the panel runs first. The expand() and fill() functions stipulate how the widget changes if more space becomes available in the panel. widget() must return a pointer to the topmost Gtk widget in the applet, and get_default_config() normally returns a structure that configures the applet. However, as the configuration is stored in an external file in our case, to avoid any changes by the user requiring a restart of the applet, the function only returns undef here.
Listing 2
Ticker.pm
Enough said about the PerlPanel API – what we need now is the application code. The applet uses configure() to build its GUI, which comprises a label with share price data and a button that handles user clicks. Line 42 defines this using the widget's signal_connect() method, which assigns an anonymous subroutine to the clicked event; the subroutine calls the applet's stocks_update() method. After all the widgets have been defined, show_all() draws them in the panel. Line 56 calls stocks_update() for the first time before the program uses add_timeout() to define an event that reoccurs every five minutes (5 * 60 * 1000 milliseconds), which also calls stocks_update(). The function in turn calls symbols() to parse the ~/.ticker-rc file, thus immediately catching short-term symbol updates by the user.
Comment lines, blanks and anything that does not look like a ticker symbol is simply ditched by the function; later on, these symbols will be sent to the getquotes script, and it's essential to avoid unpleasant surprises with unchecked command-line parameters.
Background
When Perl's open() function is called by stocks_update() in line 110, it creates a pipe to allow getquote to run in the background. Be careful here: If the code were to grab the output of the externally called process right after, it might have to wait a couple of seconds for the results to trickle in from the Internet, which is not a good idea for a GUI that must react swiftly to user input that can happen at any time. Instead, stocks_update() uses the Gtk2::Helper add_watch() function, which accepts a file descriptor (fileno() generates a descriptor from a Perl file handle) and jumps to a callback function when data arrives on it. This means that the script keeps running in the meantime, terminating stocks_update() and jumping to the applet's main event loop, where it can process user input and other external events without any delays.
If get_quote has finally sent some data, but still has more to come, the call to eof(COMMAND) is false and the Else loop in Line 132 appends the data to the existing results. When get_quote finishes, it's the IF branch's turn; the file handle is cleaned up by close(), and remove_watch() stops watching the corresponding descriptor. Line 129 then transforms the column format to a single Symbol Price Symbol Price … line and then calls the set_markup() method to send it to the label widget, which displays the text in the panel.
To make sure that the developer knows what the applet is up to, Ticker.pm first initializes Log4perl to redirect the log statements embedded in the code to the /tmp/ticker.log file. Figure 2 shows a couple of lines from the log. If you do not need this additional output, simply comment out the call to easy_init() in the code.
Installation
On Ubuntu, you can type sudo apt-get install perlpanel at the command line to install PerlPanel along with the Perl modules on which it depends. The files are available from the Ubuntu Package repository, so there is no need for a CPAN Shell. The getquote script then must be installed in /usr/bin and requires the Finance::-YahooQuote module, which is also available as a Ubuntu package (libfinance-yahooquote-perl).
To tell PerlPanel about the new ticker applet, you must copy the Ticker.pm file to the /usr/share/perlpanel/PerlPanel directory, where you will find some other applets that are installed as part of the PerlPanel distribution. Then you need to add the line
Ticker:Stock Ticker:Utilities
to the applet registry file, /usr/share/perlpanel/applet.registry. This specifies that the panel will find the "Ticker" widget in the applet directory under the name of Ticker.pm, that it is a stock ticker, and that it should be located in the Utiltities section.
After it restarts, PerlPanel knows that the applet exists, but still does not display it. To allow this to happen, the user first has to add the applet to the panel. To do so, you need to press the Actions button in the panel, and select the Configure menu item. In the dialog box that follows, click the +Add button. This gives you the selection box (Figure 3), which shows a selection of ready-to-run applets. One of them is your ticker, which you can then select and install by pressing the +Install Applet button. The applet then appears in the container, where you can move it up or down to change its corresponding horizontal display position in the panel.
To launch the panel automatically when you log in to a session with your window manager, you must add the /usr/bin/perlpanel program to your session start dialog; on Gnome, this looks something like the dialog box shown in Figure 4, which appears when you click System | Preferences | Sessions in the main menu.
For more information on building your own Perl applets, you can read the perlpanel-applet-howto.pod file, which is not part of the Ubuntu package, but is available with the source code from the PerlPanel CVS repository [2].
All of the panel's functions, including the desktop pager, the taskbar, and the dialog boxes for adding new applets and their configurations, were written in Perl and give you a taste of what's possible with PerlPanel.
Infos
- PerlPanel project: http://savannah.nongnu.org/projects/perlpanel
- Listings for this article: http://www.linux-magazine.com/resources/article_code
« Previous 1 2
Buy this article as PDF
(incl. VAT)
Buy Linux Magazine
Subscribe to our Linux Newsletters
Find Linux and Open Source Jobs
Subscribe to our ADMIN Newsletters
Find SysAdmin Jobs
News
-
Kubuntu Focus Announces XE Gen 2 Linux Laptop
Another Kubuntu-based laptop has arrived to be your next ultra-portable powerhouse with a Linux heart.
-
MNT Seeks Financial Backing for New Seven-Inch Linux Laptop
MNT Pocket Reform is a tiny laptop that is modular, upgradable, recyclable, reusable, and ships with Debian Linux.
-
Ubuntu Flatpak Remix Adds Flatpak Support Preinstalled
If you're looking for a version of Ubuntu that includes Flatpak support out of the box, there's one clear option.
-
Gnome 44 Release Candidate Now Available
The Gnome 44 release candidate has officially arrived and adds a few changes into the mix.
-
Flathub Vying to Become the Standard Linux App Store
If the Flathub team has any say in the matter, their product will become the default tool for installing Linux apps in 2023.
-
Debian 12 to Ship with KDE Plasma 5.27
The Debian development team has shifted to the latest version of KDE for their testing branch.
-
Planet Computers Launches ARM-based Linux Desktop PCs
The firm that originally released a line of mobile keyboards has taken a different direction and has developed a new line of out-of-the-box mini Linux desktop computers.
-
Ubuntu No Longer Shipping with Flatpak
In a move that probably won’t come as a shock to many, Ubuntu and all of its official spins will no longer ship with Flatpak installed.
-
openSUSE Leap 15.5 Beta Now Available
The final version of the Leap 15 series of openSUSE is available for beta testing and offers only new software versions.
-
Linux Kernel 6.2 Released with New Hardware Support
Find out what's new in the most recent release from Linus Torvalds and the Linux kernel team.