Re: best practice ... requires




Quoth mike <hillmw@xxxxxxxxxxx>:
Ok, so i decided to use packages.

my scripts would look something like:

#!/usr/bin/perl -w

Don't use -w, use

use warnings;

You need

use strict;

here as well. Have you seen the posting guidelines?

#
# name of this program is: main.cgi
#
use CGI;
use pkg_a;

Packages with all lowercase names are reserved for pragmas (modules that
affect the way Perl parses your program, like strict and warnings).
Also, you should give your modules descriptive names (although I presume
this is just an example).

use PkgA;

use pkg_b;
my $x = &pkg_a::do_this();

Do you know what calling a sub with & does? I thought not. So don't do
it. You can avoid needing to call subs by their fully-qualified name by
using Exporter: see perldoc Exporter.

#!/usr/bin/perl -w
#
# name of this program is: pkg_a.pm
#
package pkg_a;

You need

use strict;
use warnings;

here as well. They only apply to the file in which they appear. (Yes,
this is annoying :).)


sub do_this()

Don't use prototypes unless you know what they do: Perl is not
Javascript.

sub do_this {

(Also, as a style issue, it is usual in Perl code to put the { on the
end of the line... it's not terribly important, but it can confuse
people.)

{
&pkg_b::do_that();
}

so you can see that my package is nested and I am now wondering how
correctly to do this. You can see that I am calling a package from
inside a package. I dont know if that is legal or not.

Your packages are not 'nested'. You simply have three packages:

main, defined in main.cgi;
pkg_a, defined in pkg_a.pm;
pkg_b, defined in pkg_b.pm.

Calling a function in pkg_b from pkg_a is no different from calling a
function in pkg_a from main. In order to keep pkg_a self-contained, you
should have a

use pkg_b;

at the top of pkg_a.pm as well: Perl will not load it more than once.
Then, when you've learned to use Exporter, you can import some subs into
pkg_a and some into main, and everything will be much neater :).

I am getting this error message: Illegal character in prototype

No, you're not; well, not from that code, anyway. The 'prototype' is
this bit:

sub do_this()
^^

which isn't anything like the formal argument list other languages might
have here. I suspect your real code has something like

sub do_this($param)

which is not valid Perl. Simply omit the parens until you understand how
prototypes work in Perl.

Ben

--
For far more marvellous is the truth than any artists of the past imagined!
Why do the poets of the present not speak of it? What men are poets who can
speak of Jupiter if he were like a man, but if he is an immense spinning
sphere of methane and ammonia must be silent? [Feynmann] ben@xxxxxxxxxxxx
.



Relevant Pages

  • Re: multiple packages/classes in one file
    ... > so you can use globals under strict and not just lexicals. ... > this means global to the package that our is used in. ... > sub set_member ... > return $member; ...
    (comp.lang.perl.misc)
  • AW: Undefined subroutine [...] called at [...] though sub and rou tine exist
    ... None except the one saying "package FooModule;". ... > I have a CGI script that is calling a perl module. ... > there is a forward declaration available for the function with sub. ... Is this a pure CGI environment, or are you running under mod_perl or similar? ...
    (perl.beginners)
  • Re: "Variable ... is not imported..." using an imported variable from a module
    ... When perl gets to the last line, it sees a variable with no package ... Since strict vars is in effect, it looks to see if it's ... At this point, perl is going to complain about the unqualified variable, ... the same name (especially if it's using the Exporter). ...
    (comp.lang.perl.misc)
  • Re: Module Testing Question
    ... I dont know the inner workings of Perl as ... >> I wanted to test all the subroutines in my module from a script, ... >> I get an error because the package name is passed into the sub and the ...
    (comp.lang.perl.misc)
  • HASH vs Regexp
    ... use strict; ... use warnings; ... sub Regexp::matches { ... OTOH I wonder why in current perl references are not ...
    (comp.lang.perl.misc)