Home > Archive > PERL CGI Beginners > October 2005 > how to write it properly?
You are viewing an archived Text-only version of the thread.
To view this thread in it's original format and/or if you want to reply to
this thread please [click here]
| Author |
how to write it properly?
|
|
| leezard 2005-10-10, 3:55 am |
| Hi,
I'm brand new in Perl, and I'd like to find out a few things about
Perl, but I haven't found answers yet.
So, I've script called let's say index.pl. It contains series of
conditions and under certain conditions it does "do('display.pl')".
And display.pl contains something like that:
display.pl:
-----------
print "<table border=0><tr><td>$category</td></tr>";
print ... # next few lines of html printing
And my first question is: Either this file should have standard perl
lines like #!/usr/bin/perl, use strict, etc, or can be used like it is
written. Next question, what about $category variable?Is it visible in
display.pl if I put "use strict" and "use vars qw($category)" in
index.pl?
Next thing
If i write library with some functions, and I use it in my script using
"require", should it also have #!/usr/bin/perl, use strict, use vars
etc, lines? And the same question about configuration files, for
example:
config.pl:
-----------
#!/usr/bin/perl
#is the line above necessary in such file???
$db="dbname";
$host="hostname";
$user="username";
$pass="password";
Thanks for all replies, I hope you'll help me to understand proper
style of writing perl scripts.
Greetings.
| |
| Paul Lalli 2005-10-10, 9:55 pm |
| leezard wrote:
> I'm brand new in Perl, and I'd like to find out a few things about
> Perl, but I haven't found answers yet.
You may wish to read some of the relevant documentation...
perldoc perlmod
perldoc perlnewmod
perldoc -f do
perldoc -f require
> So, I've script called let's say index.pl. It contains series of
> conditions and under certain conditions it does "do('display.pl')".
> And display.pl contains something like that:
>
> display.pl:
> -----------
> print "<table border=0><tr><td>$category</td></tr>";
> print ... # next few lines of html printing
That's generally a pretty sub-standard way of doing things. You're
asking Perl to include and parse the same lines of code multiple times.
Much better would be to put the code in display.pl into a subroutine,
require the file once, and then just call the subroutine whenever
you're currently calling do().
> And my first question is: Either this file should have standard perl
> lines like #!/usr/bin/perl, use strict, etc, or can be used like it is
> written.
You're asking two very different questions in one. No, it does not
need #!/usr/bin/perl - that line is only to tell the operating system
what program is executing the following code. In display.pl, the code
is simply being inserted into the "main" file; /usr/bin/perl is already
being run.
Yes, it *should* still have 'use strict;'. That pragma, like all
pragmas, is lexically scoped. Therefore, if display.pl does not have
'use strict;', display.pl will not be subject to strictures.
> Next question, what about $category variable?Is it visible in
> display.pl if I put "use strict" and "use vars qw($category)" in
> index.pl?
.... yes, but don't do that. use vars() is a hold-over from very old
versions of perl. These days, you probably want to use the 'our'
keyword instead (perldoc -f our). Optimally, of course, you're
replacing the display.pl code with a subroutine that will take any
variables it needs as a parameter, so you'd replace all
do ('display.pl');
lines with:
display($category);
That way, you don't need to mess with global variables.
> Next thing
>
> If i write library with some functions, and I use it in my script using
> "require", should it also have #!/usr/bin/perl,
no. As before, only the "main" script that's actually executed needs
the shebang.
> use strict
Yes. 'use strict;' is lexically scoped. Your modules should be made
subject to strictures.
> use vars
Probably not. Your modules should probably *not* be using global
variables. Use lexical variables, and pass them back and forth in your
subroutines.
> etc, lines? And the same question about configuration files, for
> example:
>
> config.pl:
> -----------
> #!/usr/bin/perl
> #is the line above necessary in such file???
No.
> $db="dbname";
> $host="hostname";
> $user="username";
> $pass="password";
I would write this entire file as such:
MyConfig.pm:
----------
package MyConfig;
use strict;
use warnings;
use base 'Exporter';
our @EXPORT_OK = ('get_config');
sub get_config {
my %config = (
db => 'dbname',
host => 'hostname',
user => 'username',
pass => 'password',
);
return %config;
}
1;
main.pl:
--------
#!/usr/bin/perl
use strict;
use warnings;
use MyConfig qw/get_config/;
my %cfg = get_config();
print "DBName: $cfg{dbname}\n";
# etc. . .
> Thanks for all replies, I hope you'll help me to understand proper
> style of writing perl scripts.
Do check out some of the perldocs I mentioned above...
Paul Lalli
|
|
|
|
|