Re: best practice ... requires
- From: Ben Morrow <ben@xxxxxxxxxxxx>
- Date: Mon, 27 Aug 2007 15:18:49 +0100
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
.
- References:
- best practice ... requires
- From: mike
- Re: best practice ... requires
- From: comp.llang.perl.moderated
- Re: best practice ... requires
- From: mike
- best practice ... requires
- Prev by Date: Re: Using DBI, Set Select to Variable
- Next by Date: Re: best practice ... requires
- Previous by thread: Re: best practice ... requires
- Next by thread: Threaded Perl Processes Going to Sleep Simultaneously??? Why?
- Index(es):
Relevant Pages
|