[Phoenix-pm] wxPerl: Perl questions

Douglas E. Miles doug at veritablesoftware.com
Thu Apr 14 08:50:10 PDT 2011


I'll just add one thing to Michael's excellent explanation. Like he
said, in general you're being tricky if you have more than one package
per module, but I wanted to warn you that almost all examples of wx code
you find will do this. I don't know why that is. If it confuses you, you
can always break the packages out into separate files and then use them
in your main program.

On 04/13/2011 12:13 PM, Michael Friedman wrote:
> Er, left off the list... Sorry.
>
> ______________________________________________________________________________
> Mike Friedman | HighWire Press, Stanford Univ | friedman at highwire.stanford.edu
>
> On Apr 13, 2011, at 12:12 PM, Michael Friedman wrote:
>
>> Yep, that's exactly right. 
>>
>> 'package' is file-scoped, until you hit another package statement or EOF, so it "contains" everything from there down. 
>>
>> "package main;" is a special statement. Just like in C or Java (or probably other languages that I'm not as familiar with) the "main" package is the one that contains global definitions and handles command-line arguments and such. Effectively it's acting as an "end package" statement here, since Perl doesn't use braces around packages.
>>
>> -- Mike F
>> ______________________________________________________________________________
>> Mike Friedman | HighWire Press, Stanford Univ | friedman at highwire.stanford.edu
>>
>> On Apr 13, 2011, at 12:01 PM, leegold wrote:
>>
>>> Hi,
>>>
>>> So if "package" creates classes and thus namespaces then take the snip:
>>>
>>> ...
>>> 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 );
>>> }
>>> ...
>>>
>>> Rewriting it a little. Could it be thought of like this ?:
>>>
>>> package MyApp; # class 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 );
>>>   }
>>> #} end class MyApp
>>>
>>> Because that's what seems like is going on to me. And then further down
>>> in the script the main namespace is re-established and a MyApp object is
>>> instantiated...?
>>>
>>> Thanks,
>>>
>>> Lee G.
>>>
>>>
>>>
>>>
>>> On Wed, 13 Apr 2011 10:50 -0700, "Michael Friedman"
>>> <friedman at highwire.stanford.edu> wrote:
>>>> 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
> _______________________________________________
> 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