Have a look at , for ActivePerl documentation. As an alternative to ActivePerl, there is Strawberry Perl. This section describes what is ActivePerl – a free Perl engine for Windows, Perl Tutorials – Herong’s Tutorial Examples – Version , by Dr. Herong Yang. Easily learn how to install Perl and run your first script. If you are on Windows, you may see a choice of ActivePerl and Strawberry Perl.
|Published (Last):||25 November 2004|
|PDF File Size:||12.8 Mb|
|ePub File Size:||2.47 Mb|
|Price:||Free* [*Free Regsitration Required]|
The Tk toolkit is native to the Tcl programming language, but its ease of use and cross-platform availability has made it the GUI toolkit of choice for many other dynamic languages.
Tkx is a Perl module that makes the Tk toolkit available to Perl programs. By loading the Tkx tutorrial Perl programs can create windows tutorizl fill them with text, images, buttons and other controls that make up the user interface of the application. Let’s start with the mandatory exercise of creating an application that greats the world.
We’ll make the application window contain a single button which will shut down the application if clicked. The code to make this happen is:.
Perl Tutorial – Programming
Save this to a file called hello. A window with the text “Hello, world” should appear on your screen. Let’s look at what this code is doing. After the Tkx module has been loaded by the use Tkx statement, the application will show an empty window called “.
We create a button with the name “. After the layout of the window has been set up, we need to pass control back to Tk so that it can draw the window and invoke our callback if the button is clicked. This is done with the Tkx:: MainLoop call at the end. Clicking the button will invoke the subroutine registered with the button’s -command option. In this case the callback destroys the window, which in turn terminates the activepeerl.
As you can see the code is mostly identical, but with a slightly different syntax. The only difference is that the call to MainLoop is implicit in Tcl and does not have to be spelled out.
Tkx does not include documentation for all the Tk widgets available for use. This translation is relatively straightforward and basically involves adding the prefix “Tkx:: The Tk documentation can be found here:. This documents core Tuutorial and useful add-on packages that are part of ActiveTcl.
A major complication in the mapping to Perl is how to invoke subcommands on Tk widgets.
For example, if you want to change the text of the button created above you might in Tcl do:. Because of this we almost always use objects when working with Tkx widgets.
The windows and controls that make up a Tk interface are called widgets. The widgets are identified by path names of the form. These names are hierarchical in the same way as file system names are, but “. At the top of this hierarchy we have a widget called.
The Tkx module provides the Tkx:: This provide a more “perlish” way to create and manipulate Tk widgets. It also provide a convenient way to invoke subcommands methods on the widgets. By loading the Tkx module, we make the Tkx:: Next, we instantiate a new Tkx:: The rest of the method name is the name of the Tk widget to create; i.
Tkx::Tutorial – How to use Tkx
Arguments are passed as before. For trivial programs like the one above, using Tkx:: The following, slightly expanded version of the previous Hello World program, introduces a tutorlal more Tkx features. Line numbers have been added to the program for easier to reference back to its statements:. The first thing we add is the use strict statement, because that’s a good practice in general.
How can I run a Perl script with ActivePerl? – Stack Overflow
In line 5 and 6 we set up some window manager attributes of the main application window. The same rules apply to the function names in the Tkx:: We could alternatively have modified the window attributes with:.
In line 11 to 16 we have expanded the button callback to change the text of button and wait 1. The most commonly used subcommand is “configure” that is used to change the attributes of actievperl widget as we do in line In line 15 we destroy the window after a delay of ms, which should be enough time to read the new “Goodbye, cruel world” text.
In line 23 we invoke the messageBox command to pop up a useful reminder to our user. Remember the previous rule that an underscore in Tkx:: If you try to call Tkx:: What happens is that Tkx invoked the “tk messageBox” command, but the Tcl “tk” command only takes the subcommands listed in the error message above and refuse to do anything about “messageBox”. In order to invoke Tcl commands with underscore their name, you need to triple the underscore on the Perl side, which gives us Tkx:: Double underscores in names have yet another meaning that we will tell you about in the next section.
Most real GUI application will need a menu line at the top of the application window or screen. The following runnable program shows how a minimal menu can be set up with Tkx:. We start out as all proper Perl programs should by enabling warnings and stricture at line 1 and 3.
Then, we load Tkx which will create our main application window at line 4. Aqua is the native interface of Mac OS X. We need this constant because the menu layout on Aqua is not the same as in other windowing systems.
Tcl packages can be loaded with the Tkx:: More about Tcl packages and namespaces in the next section. In line 14, we obtain a Tkx:: In a real application there would be additional code between line 15 and 17 to set up the rest of the application window, but for this demonstration we’ll just leave the window empty. In line 17, we ask Tk to start processing events by invoking Tkx:: This function will return when the application window has been destroyed.
When that happens, we exit at line This code should be easy enough to follow. The application will still run, but when you try to invoke these menu entries you get an “Application Error Dialog” from Tk. The -underline options are provided to make it possible to select menu entries with the keyboard. The corresponding character of the -label will be underlined and you will be able to select this entry by pressing the key when the menu is active.
It is also possible to set up direct keyboard shortcuts as we’ve done for the File New function at line Note that the -accelerator option only adds the text to the menu item, so we need to use an explicit call to set up this binding in line For Aqua we don’t want to add the “File Exit” entry to the menu because the OS itself always provide a Quit action in the application menu.
Aqua applications will also need to add the “About” function on the application menu instead of the “Help” menu as is common on other platforms.
The menu names “apple” and “help” provided in line 46 and 65 has special significance to Tk.
Menu items added to the “apple” menu will show up in the application menu. In Mac OS X these entries show up at the top of the menu just right of the apple. A menu called “help” will be flushed right on Unix, even though this style seems to be out of fashion in modern Unix applications. The Tkx distribution contains a script called menu which is a runnable version of the program shown here. You might want to use this as a starting point for your own Tkx applications.
When the Perl application starts up and loads Tkx, the only functions available in the Tkx:: These commands are described in the “Tcl” and “Tk” sections at http: Additional commands can be loaded from Tcl packages.
Once loaded, new commands show up in the Tkx:: This example activepperl the “Tktable” package in order to make the table command available for createing table widgets:. Packages are loaded by calling the Tkx:: An optional version number can be provided as the second argument if you want to make sure a certain version or newer is loaded.
One source of confusion here is the proper spelling of the package name to provide to Tkx:: In some cases the “synopsis” section describing the package will spell out the package name, but in cases like this we have found no better way than to look into the pkgIndex. The package documented as “BWidgets” should be loaded as tutorjal without the “s” and the package documented as “IWidgets” should be loaded as “Iwidgets” with a lower case “w”.
Most modern Tcl packages do not create names at the top level like TkTable above. Instead, they create functions in a Tcl namespace with a name matching the package name. In the menu example of the previous section we loaded the “style” package which created a command called “use” in the “style” namespace.
This command can be referenced as “:: From Perl this maps to a function called Tkx:: In Tkx applications it is tutprial convenient to use your own subclass of Tkx:: The following is an example class, which could be saved to the file MyWidget.
The MyWidget class above provides shortcuts for the “messageBox” and “getOpenFile” in order to hide the triple underscore ugliness and propagate the -parent attribute. Similar reasoning exists for the “bell”. Having you own application-specific widget class provides a place to add methods discovered by refactoring repeated code in your application.