Beefy Boxes and Bandwidth Generously Provided by pair Networks
go ahead... be a heretic
 
PerlMonks  

Re: Understanding module structure and inheritance

by jaldhar (Vicar)
on Jan 30, 2008 at 14:50 UTC ( #665134=note: print w/ replies, xml ) Need Help??


in reply to Understanding module structure and inheritance

ok, let's see if I can answer this without confusing myself in the process :-)

  1. First the easy question: yes this is the kind of layout I and I would say most CGI::Application use too. It's a bit safer if your code packages or templates are not in an area accessible to the web server. However you've got some errors.

    Contact.pm should be in a directory called Main. (:: is replaced by the directory separator for your particular OS.)

    Main.pm doesn't need the shebang line but does need one which says package Main;

    In calling.cgi use Main::Contact not Contact::Contact.

  2. Internally a perl script has a table of symbols including subroutine names. These are partitioned into namespaces or "packages." Contact inherits Main. That means the Contact namespace will also include a copy of all the symbols of the Main namespace plus whatever it defines for itself. However Main.pm only imports (or 'uses') a symbol from Common.pm. That means within the symbol table for that file, there is an entry called Common::show_here. That symbol is not part of the Main:: namespace so doesn't get inherited along with other Main:: symbols in Main::Contact. However Contact.pm can still access it in its original namespace as Common::show_here. If you use Common in Contact.pm you can leave out the Common:: part and get the same result.

Update: friedo is right and I have oversimplified far too much. What I am trying to get is that when you inherit a class that classes symbols are "yours" whereas with "use" the symbol remains "foreign" in a way though it is available to you. Hope this is clearer.

--
જલધર


Comment on Re: Understanding module structure and inheritance
Re^2: Understanding module structure and inheritance
by friedo (Prior) on Jan 30, 2008 at 14:54 UTC
    Contact inherits Main. That means the Contact namespace will also include a copy of all the symbols of the Main namespace plus whatever it defines for itself.

    That's actually not how inheritance works in Perl. The parent class's symbols do not get exported to the child class. When a method is called on the child class with the arrow (->) syntax, if that subroutine is not defined in the child class, then Perl searches the parent class(es) for it, and then finally tries AUTOLOAD if that doesn't work.

    Update: Fixed typo

Re^2: Understanding module structure and inheritance
by bradcathey (Prior) on Jan 30, 2008 at 15:30 UTC

    Great, that helps. Is there any other way to include Common.pm in Main.pm that would include the symbol for Common::show_here?

    —Brad
    "The important work of moving the world forward does not wait to be done by perfect men." George Eliot

      Well, seeing as you are using CGI::Application, see the docs about making a plugin. Most of the existing plugins are just simple wrappers around CPAN modules so you can use them as examples.

      --
      જલધર

      Hi, bradcathey,
      $ cat Common.pm package Common; use strict; use warnings; use Exporter; our @ISA = qw(Exporter); # borrows import() our @EXPORT = qw(show_here); sub show_here { } 1; $ cat Main.pm package Common; use strict; use warnings; use Common; sub show_me {} 1;
      The caller of Main.pm has direct access to show_me() and show_here() using qualified name,
      Main::show_here; # since exported by default by Common.pm Main::show_me; # originally in Main.pm
      If the caller has its own use Common then it doesn't have to qualify it, show_here() is enough.

      However, if you meant that "a way to include Common.pm in Main.pm so Contact.pm can use show_here directly, then make Main.pm to reexport the function.

      package Main; .... use Exporter; use Common; our @ISA = qw(Exporter); # borrows import() our @EXPORT = qw(show_here); # just @Common::Export to export # what ever Common exports. .... sub show_me {} 1;
      The caller,
      use Main; Main::show_me(); show_here()

      Open source softwares? Share and enjoy. Make profit from them if you can. Yet, share and enjoy!

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://665134]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others chanting in the Monastery: (6)
As of 2014-08-01 03:50 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    My favorite superfluous repetitious redundant duplicative phrase is:









    Results (256 votes), past polls