http://www.perlmonks.org?node_id=665134


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.

--
જલધર