[Phoenix-pm] wxPerl: Perl questions

Michael Friedman friedman at highwire.stanford.edu
Wed Apr 13 10:50:22 PDT 2011


leegold,

Newbie questions are our specialty!

First and foremost, a very good book that gives a good overview of what all this stuff means is _Modern Perl_, by chromatic. It's a free download, completely searchable, and easy to read, even for someone new to Perl.
	http://www.onyxneon.com/books/modern_perl/index.html

> 
>> From a wxPerl program:
> 
> use Wx;
> package MyApp;
> use base 'Wx::App';
> 
> 
> What is difference between a module and a package?

A module is a file on the filesystem containing Perl code. They usually end with .pm.

A package is the equivalent of a class in many other languages. You can have more than one package in a file, if you're being tricky. In general, there's one package per module and the module is named the same as the package. So "package MyApp;" is in the file "MyApp.pm". That way Perl can auto-locate the file containing the package when you use it from some other file. 

(A package isn't really a class -- it's just a way of segmenting code by giving it a namespace -- but modern perl uses packages in the same way other languages use classes, most of the time.)

> 
> What exactly is MyApp? It's a package but I can't find any
> documentation on it. There's no "MyApp" in my C:\Perl folder...

Your own modules go wherever you want them. So you don't find a MyApp.pm because you haven't saved this code in a file on your own computer yet. :-) 

Since the same syntax ('use <module>;') is used for loading both modules installed from CPAN and your own modules, it can sometimes be hard to tell which are which. I recommend naming your own modules with some top-level name that relates to the project/company/section/whatever so it's easier to tell the difference. I work for a company named "HighWire", so our custom modules are called like so:

	package Highwire::Resource::Citation;

which is in a file in my home directory called ~/projects/Highwire/Resource/Citation.pm . That way it's easy to see which are modules I wrote and which are from CPAN.

> 
> Tried to find info in "use base" keywords but no simple
> explainations. What does "use base" do?
> 
> 

"use base" means that the named package is the parent of this package. When you're calling methods on an object, it'll search in the local package first and then start walking through everything in "use base". If you're using a version of perl > 5.10 (I think) you can also use "use parent" instead. 

http://perldoc.perl.org/base.html
http://perldoc.perl.org/parent.html
all the 'use' pragmas: http://perldoc.perl.org/index-pragmas.html

In this case, by having "use base Wx::App;", you're saying that "if you can't find a method in this file, go look in Wx::App, which probably has it." That's subclassing!

Good luck!
-- Mike Friedman

> 
> Here's the script:
> 
> # load wxPerl main module
> use Wx;
> # every application must create an application object
> package MyApp;
> use base 'Wx::App';
> sub OnInit {
> 
>    # parent window,  Window ID -1 means any,  # title,  #
> default position, # size
>    my $frame = Wx::Frame->new(  undef,  -1, 'wxPerl rules', [-1,
> -1], [250, 150]);
> 
>    $frame->Show( 1 );
> }
> package main;
> # create the application object, this will call OnInit
> my $app = MyApp->new;
> # process GUI events from the application this function will not
> # return until the last frame is closed
> $app->MainLoop;
> 
> Thanks
> _______________________________________________
> Phoenix-pm mailing list
> Phoenix-pm at pm.org
> http://mail.pm.org/mailman/listinfo/phoenix-pm



More information about the Phoenix-pm mailing list