best practice ... requires
best practice ... requires
am 24.08.2007 03:39:20 von Mike
i'm having trouble with variables
I have a main cgi script ... lets call it main.cgi
In main.cgi i have some other perl scripts that have subroutines
#!/usr/bin/perl -w
#
# name of this program is: main.cgi
#
use CGI;
use Cwd; #used to get the current working directory
$filebase = cwd;
#requires here
require "$filebase/some.lib";
require "$filebase/a.pl";
require "$filebase/b.pl";
require "$filebase/c.pl";
my $x = &do this;
So the question is:
1) if i dont use a subroutine that is in some.lib here in main.cgi but
in a.pl is it better to remove the require from main.cgi and put it in
a.pl?
2) can my require in main.cgi be global since the vars in some.lib are
used in a.pl, b.pl, and c.pl?
thanks 4 any help.
Mike
Re: best practice ... requires
am 24.08.2007 04:04:35 von Gunnar Hjalmarsson
mike wrote:
> i'm having trouble with variables
What kind of trouble??
> I have a main cgi script ... lets call it main.cgi
>
> In main.cgi i have some other perl scripts that have subroutines
>
> #!/usr/bin/perl -w
> #
> # name of this program is: main.cgi
> #
>
> use CGI;
> use Cwd; #used to get the current working directory
> $filebase = cwd;
>
> #requires here
> require "$filebase/some.lib";
> require "$filebase/a.pl";
> require "$filebase/b.pl";
> require "$filebase/c.pl";
>
> my $x = &do this;
>
> So the question is:
> 1) if i dont use a subroutine that is in some.lib here in main.cgi but
> in a.pl is it better to remove the require from main.cgi and put it in
> a.pl?
If you also import symbols from some.lib and the subroutines are in a
separate package, it's better to require the library from the script
that is using the subs. Otherwise it doesn't matter.
> 2) can my require in main.cgi be global since the vars in some.lib are
> used in a.pl, b.pl, and c.pl?
What does "global" mean here?
--
Gunnar Hjalmarsson
Email: http://www.gunnar.cc/cgi-bin/contact.pl
Re: best practice ... requires
am 24.08.2007 16:36:20 von xhoster
mike wrote:
> i'm having trouble with variables
>
> I have a main cgi script ... lets call it main.cgi
>
> In main.cgi i have some other perl scripts that have subroutines
>
> #!/usr/bin/perl -w
> #
> # name of this program is: main.cgi
> #
You should use strict;
>
> use CGI;
> use Cwd; #used to get the current working directory
> $filebase = cwd;
>
> #requires here
> require "$filebase/some.lib";
> require "$filebase/a.pl";
> require "$filebase/b.pl";
> require "$filebase/c.pl";
I think use is usually better than require for this type of stuff.
And if "." is in your @INC, then it will automatically search cwd for them.
>
> my $x = &do this;
Why are using this syntax?
> So the question is:
> 1) if i dont use a subroutine that is in some.lib here in main.cgi but
> in a.pl is it better to remove the require from main.cgi and put it in
> a.pl?
Probably, yes.
> 2) can my require in main.cgi be global since the vars in some.lib are
> used in a.pl, b.pl, and c.pl?
Does your library or any of the scripts change the package?
"package" variables in some.lib can be used from anywhere, they just need
to be fully qualified with a package name if you are using them from
another package (or even from the same package under strict.)
Xho
--
-------------------- http://NewsReader.Com/ --------------------
Usenet Newsgroup Service $9.95/Month 30GB
Re: best practice ... requires
am 26.08.2007 21:53:58 von Charles DeRykus
On Aug 23, 6:39 pm, mike wrote:
> i'm having trouble with variables
>
> I have a main cgi script ... lets call it main.cgi
>
> In main.cgi i have some other perl scripts that have subroutines
>
> #!/usr/bin/perl -w
> #
> # name of this program is: main.cgi
> #
>
> use CGI;
> use Cwd; #used to get the current working directory
> $filebase = cwd;
>
> #requires here
> require "$filebase/some.lib";
> require "$filebase/a.pl";
> require "$filebase/b.pl";
> require "$filebase/c.pl";
>
> my $x = &do this;
>
> So the question is:
> 1) if i dont use a subroutine that is in some.lib here in main.cgi but
> in a.pl is it better to remove the require from main.cgi and put it in
> a.pl?
Yes, clearer IMO to put the require in a.pl since
the subroutine's used there and not in main.cgi.
> 2) can my require in main.cgi be global since the vars in some.lib are
> used in a.pl, b.pl, and c.pl?
>
Yes, as already noted, package variables defined
earlier in some.lib can be used later downstream if fully qualified.
This applies to subroutines as well.
The package qualifier can be omitted only if
the definitions are in package 'main'. But,
polluting main from files pulled in via require
isn't a good idea.
--
Charles DeRykus
Re: best practice ... requires
am 27.08.2007 01:52:32 von Mike
Ok, so i decided to use packages.
my scripts would look something like:
#!/usr/bin/perl -w
#
# name of this program is: main.cgi
#
use CGI;
use pkg_a;
use pkg_b;
my $x = &pkg_a::do_this();
#!/usr/bin/perl -w
#
# name of this program is: pkg_a.pm
#
package pkg_a;
sub do_this()
{
&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.
I am getting this error message: Illegal character in prototype
I dont know if it because the packages are nested or if i have some
other problem.
Thanks
Re: best practice ... requires
am 27.08.2007 02:59:38 von Gunnar Hjalmarsson
mike wrote:
> Ok, so i decided to use packages.
>
> my scripts would look something like:
>
> #!/usr/bin/perl -w
> #
> # name of this program is: main.cgi
> #
> use CGI;
> use pkg_a;
> use pkg_b;
> my $x = &pkg_a::do_this();
>
> #!/usr/bin/perl -w
> #
> # name of this program is: pkg_a.pm
> #
> package pkg_a;
>
> sub do_this()
-------------^^
Skip those parentheses (i.e. don't bother with prototypes)
> {
> &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.
You have already been told that it works.
> I am getting this error message: Illegal character in prototype
See above.
--
Gunnar Hjalmarsson
Email: http://www.gunnar.cc/cgi-bin/contact.pl
Re: best practice ... requires
am 27.08.2007 16:18:49 von Ben Morrow
Quoth mike :
> 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@morrow.me.uk
Re: best practice ... requires
am 27.08.2007 16:43:10 von Mike
So if I have some parameters to pass to the subroutine how do I do
that without a prototype?
Mike
Re: best practice ... requires
am 27.08.2007 17:14:53 von Gunnar Hjalmarsson
mike wrote:
> So if I have some parameters to pass to the subroutine how do I do
> that without a prototype?
You just pass them...
perldoc perlsub
--
Gunnar Hjalmarsson
Email: http://www.gunnar.cc/cgi-bin/contact.pl