Hello again jamroll,
Let's take the question one at a time. Why you should use Package name as first letter capital? Read the perlman:perlstyle:
From the link above regarding the packages:
Package names are sometimes an exception to this rule. Perl informally + reserves lowercase module names for ``pragma'' modules like integer +and strict. Other modules should begin with a capital letter and use +mixed case, but probably without underscores due to limitations in pr +imitive file systems' representations of module names as files that m +ust fit into a few sparse bytes.
Having said that I would recommend renaming the directory to Pm/Security the same applies to the rest of the modules that you are using. Let's move on to the next problem, calling the module.
You are saying that you have a main.pl script that gives you the error:
Undefined subroutine &pm::security::banned called at pm/user.pm line 1 +36
Let's try to replicate the problem. I am creating the directories as you say d:/apache24/htdocs/pm/user.pm
I would modify a bit your module so I would recommend to do the same on the rest of your modules. First of all I use also use warnings; for many reasons make your code more safe. Second from the Perl documentation Exporter/Selecting What to Export:
Do not export anything else by default without a good reason! Exports pollute the namespace of the module user. If you must export t +ry to use @EXPORT_OK in preference to @EXPORT and avoid short or comm +on symbol names to reduce the risk of name clashes.
Having said that I modify your Export to Export_OK. On your modules you need to close them with 1;. Why? Read the perlmod/Making your module threadsafe:
If it returns a true value, then no objects of that class will be clon +ed; or rather, they will be copied as unblessed, undef values.
If this is not enough also read Perl Module ending without 1;. Having said that I add also 1; at the end of your module.
Sample of your module based on the modifications that I propose Security.pm in the directory of my local PC /home/tinyos/apache24/htdocs.
package Pm::Security; #/ # a module to encapsulate security-related functions #/ use CGI; use strict; use warnings; use Exporter; use vars qw($VERSION @ISA @EXPORT_OK); $VERSION = 1.00; @ISA = qw(Exporter); @EXPORT_OK = qw( _tests banned bounced get_salt password_correct password_set get_client_IP login logout ); ###################################################################### sub banned { #* # gets the banned status of a uid # !this function requires updating # the code in this function needs to # conform to a more basic format # there should only be one return! #* # my ($db, $uid) = @_; # a DBH && a uid # my $query = "select banned from users where ID = " . $db->quote( +$uid); # my $result = pm::bc_sql::sql_execute($db, $query); # should resu +lt in a 0 or a hash with one key: a UID # $result is a hash reference # if (ref $result eq "HASH") { # if ($result->{banned} eq 2) { # return 1; # 1 when the user is banned # } # } return reversed @_; # 0 when the user is not banned #usage: if (banned($db, $uid)) { print "yer banned, bitch"; } } 1;
Now that we have defined and applied minor modification to your module let's try to call it for execution from our main.pl:
Sample of main.pl script, remember the path is relevant to my local PC but it should work for your PC with minor modifications:
#!usr/bin/perl use say; use strict; use warnings; use lib '/home/tinyos/apache24/htdocs'; use Pm::Security qw( banned ); my @list = qw (First ~ Second); say banned(@list); __END__ $ perl main.pl Second~First
A minor detail to add here I call the script from a different directory where the dir Pm is located. I manage to do that by using the lib module. If I comment out this line # use lib '/home/tinyos/apache24/htdocs'; I get the following expected error:
$ perl main.pl Can't locate Pm/Security.pm in @INC (you may need to install the Pm::S +ecurity module) (@INC contains: /home/tinyos/perl5/lib/perl5/5.24.1/x +86_64-linux-gnu-thread-multi /home/tinyos/perl5/lib/perl5/5.24.1 /hom +e/tinyos/perl5/lib/perl5/x86_64-linux-gnu-thread-multi /home/tinyos/p +erl5/lib/perl5 /etc/perl /usr/local/lib/x86_64-linux-gnu/perl/5.24.1 +/usr/local/share/perl/5.24.1 /usr/lib/x86_64-linux-gnu/perl5/5.24 /us +r/share/perl5 /usr/lib/x86_64-linux-gnu/perl/5.24 /usr/share/perl/5.2 +4 /usr/local/lib/site_perl /usr/lib/x86_64-linux-gnu/perl-base) at ma +in.pl line 7. BEGIN failed--compilation aborted at main.pl line 7.
Of course it is because Perl is not aware of my Pm::Security module that I have defined in my directory.
One last question, why you are calling your function as sub banned($$) I was looking online regarding this because I never used it before and I found perlvar/General Variables:
LinuxThreads is now obsolete on Linux, and caching getpid() like this +made embedding perl unnecessarily complex (since you'd have to manual +ly update the value of $$), so now $$ and getppid() will always retur +n the same values as the underlying C library.
If this is the case why don't you call getppid instead of $$. Maybe I am wrong regarding this point so another Monk could share some knowledge.
Hope this provides you enough information to resolve all of your module(s) problems.
In reply to Re^3: Perl Modules
by thanos1983
in thread Perl Modules
by jamroll
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |