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
Support Our Work
Linux Magazine content is made possible with support from readers like you. Please consider contributing when you’ve found an article to be beneficial.
News
-
Gnome Fans Everywhere Rejoice for the Latest Release
Gnome 47.2 is now available for general use but don't expect much in the way of newness, as this is all about improvements and bug fixes.
-
Latest Cinnamon Desktop Releases with a Bold New Look
Just in time for the holidays, the developer of the Cinnamon desktop has shipped a new release to help spice up your eggnog with new features and a new look.
-
Armbian 24.11 Released with Expanded Hardware Support
If you've been waiting for Armbian to support OrangePi 5 Max and Radxa ROCK 5B+, the wait is over.
-
SUSE Renames Several Products for Better Name Recognition
SUSE has been a very powerful player in the European market, but it knows it must branch out to gain serious traction. Will a name change do the trick?
-
ESET Discovers New Linux Malware
WolfsBane is an all-in-one malware that has hit the Linux operating system and includes a dropper, a launcher, and a backdoor.
-
New Linux Kernel Patch Allows Forcing a CPU Mitigation
Even when CPU mitigations can consume precious CPU cycles, it might not be a bad idea to allow users to enable them, even if your machine isn't vulnerable.
-
Red Hat Enterprise Linux 9.5 Released
Notify your friends, loved ones, and colleagues that the latest version of RHEL is available with plenty of enhancements.
-
Linux Sees Massive Performance Increase from a Single Line of Code
With one line of code, Intel was able to increase the performance of the Linux kernel by 4,000 percent.
-
Fedora KDE Approved as an Official Spin
If you prefer the Plasma desktop environment and the Fedora distribution, you're in luck because there's now an official spin that is listed on the same level as the Fedora Workstation edition.
-
New Steam Client Ups the Ante for Linux
The latest release from Steam has some pretty cool tricks up its sleeve.