Beefy Boxes and Bandwidth Generously Provided by pair Networks
Problems? Is your data what you think it is?
 
PerlMonks  

Re^3: Perl Modules

by thanos1983 (Parson)
on Jun 20, 2017 at 20:49 UTC ( #1193180=note: print w/replies, xml ) Need Help??


in reply to Re^2: Perl Modules
in thread Perl Modules

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.

Seeking for Perl wisdom...on the process of learning...not there...yet!

Replies are listed 'Best First'.
Re^4: Perl Modules
by jamroll (Beadle) on Jun 21, 2017 at 05:45 UTC
    yes. seems everything is working most excellently. i have no clue how to thank you enough! so, i extend glorious amounts of thanks to you, and everyone else who replied - it's been a giant help. i even book marked that ebook that was linked to by another monk.

    the module Pm::Bc_chef doesn't like having use Pm::User; in it. Well, it actually causes Pm::User to throw the "subroutine not defined" error message (because use Pm::Bc_chef; is in the module Pm::User. so, if chef needs to refer to a user module subroutine, i can't put use Pm::User; in Bc_chef.pm. baffling, but it's working.

    so grateful

    JamRoll

    Btw - is my coding awful? i've always wondered, and hoped you folks might be willing to grade it without killing my itty bitty ego too much... ;) is there more of my code you would like to see? i can let you peek under the hood, so to speak, to get a better feel for how i go about doing things. i'd really like some constructive input on it, too....i know it's way off topic here, so perhaps PM me...I'll be on and off fairly often, doing my (re)search(es).
      ... everything is working most excellently. i have no clue how to thank you enough!

      Hint, hint: the Offering Plate.

      ... is my coding awful? i've always wondered ... i can let you peek under the hood ... i'd really like some constructive input on it ...

      As a registered user, you have a public scratchpad (see your personal node). You could post code there and solicit comments in the Chatterbox. The scratchpad is limited to 64K, but nobody is going to read anywhere near that much code (not for free, anyway), so don't go crazy. I would post code examples that are complete (or as complete as reasonably possible).

      Update:

      ... hoped you folks might be willing to grade it without killing my itty bitty ego too much ...
      I've seen the code you've posted in this thread. Constructive criticism, offered bluntly, can be pretty bruising. Please don't ask for something you aren't willing to receive.


      Give a man a fish:  <%-{-{-{-<

      nope! things don't seem to be meshing nicely....

      and oh no! @INC will no longer include .! That's inconvenient. But, not impossible to move my modules to a folder in @INC

      Pm::Redir is claiming pre_html_header() isn't defined - this routine is found in Pm::Html

      the subs Pm::Redir::redir and Pm::Redir::redir3 both call Pm::Html::pre_html_header()

      now granted, Pm::Html does use a bunch of other modules i've created. i haven't checked those modules to see if they include Pm::Redir, but would it matter if Pm::Html uses a module which then includes Pm::Redir??

      here's the code:

      just the header to ./Pm::Html
      package Pm::Html; #/ # functions for displaying HTML elements #/ my $DEBUGGER = 0; my $TABLE_BORDER = "0"; ######################## use strict; use warnings; use Exporter; use vars qw($VERSION @ISA @EXPORT_OK %EXPORT_TAGS); use URI::Escape; ######################## use Pm::Bc_chef qw(cookie_get); use Pm::Bc_sql qw(get_race_asWord get_zodiacs get_themes get_constant get_theme_data get_config get_cities get_countries sql_execute get_users get_site_name get_home_page user_exists get_location get_phrase ); use Pm::Bc_misc qw(get_param add_numeric_suffix shorten_str ); use Pm::Date qw(expand_date determine_zodiac get_today get_month ); use Pm::Search qw(search_terms); use Pm::User qw(get_user_stats get_user_friend_requests get_user_blocked_users get_user_unread_messages get_user_stat get_user_fuck_alert_count get_user_pic get_user_dp get_user_stats_asIcons get_user_friends ); ######################## our $VERSION = 1.00; our @ISA = qw(Exporter); our @EXPORT_OK = qw( _tests pre_html_header header footer table fieldset fieldset_constricted div tdata trow radio checkbox textarea unordered_list ordered_list li href img dropdown input display_404_page display_about_page display_chat display_city_names display_city_names_asDropdown display_country_names display_country_names_asDropdown display_forgot_page display_blocked display_friends display_fuck_me_alerts display_homepage display_mail display_navbar display_pay_page display_photos_page display_searchbar display_signup_page display_stats_page display_theme_names display_theme_names_asDropdown display_titlebar display_todays_birthdays display_user_card display_user_card_mini display_user_list display_admin_page display_admin_ustats_editor display_admin_uphotos_editor display_admin_uflags_editor display_admin_umsgs_editor display_years_forDropdowns display_users_forDropdowns display_config_forDropdowns display_zodiac_icon display_debug_one display_debug_many display_debug_code display_debug_large get_config_forDropdowns :debug ); %EXPORT_TAGS = ( debug => [qw(display_debug_one display_debug_many disp +lay_debug_code display_debug_large _tests)], ); ########################
      and the Pm::Redir code:
      package Pm::Redir; #/ # Client Redirections #/ use strict; use warnings; use CGI::Carp qw(fatalsToBrowser); use URI::Escape; use Exporter; use vars qw($VERSION @ISA @EXPORT_OK); use Pm::Bc_chef qw(cookie_set); use Pm::Html qw(display_debug_one display_debug_many display_debug_code display_debug_large pre_html_header ); # i'm not using :debug cuz that don't work either! $VERSION = 1.00; @ISA = qw(Exporter); @EXPORT_OK = qw( _tests redir redir3 error_redir notice_redir ); ############################## sub redir($$) { #* # redirects a client browser to a specified URL (may include a msg) #* my ($url, $msg) = @_; # a url to redirect to && a msg my $html = pre_html_header(); $html =~ s/content-type\: text\/html\n\n//i; if ($msg) { $html .= "status: 302 $msg\n"; } else { $html .= "status: 302 redir ok\n"; } $html .= "location: $url\n\n"; return $html; # a scalar #usage print redir("/", "invalid page"); } ############################## sub redir3($$$) { #* # redirects a client browser to a specified URL (may include a msg) # can add a cookie to the redirect (for errors or msgs or other thin +gs you deem necessary) # this is NOT version 3 of the redir command. it's a 3 param comman +d! #* my ($url, $msg, $type) = @_; # a url to redirect to && a msg && a ms +g type ('e' or 'n', or whatever else you like) my $html = ""; if ($type) { $html = cookie_set($type, $msg, 0); } my $html .= pre_html_header(); $html =~ s/content-type\: text\/html\n\n//i; $msg = uri_escape($msg); $html .= "status: 302 $msg\n"; $html .= "location: $url\n\n"; return $html; # a scalar #usage: print redir3("/", "Access Denied by redir3", 'e') } ############################## sub error_redir($$) { #* # redirects a client browser to a specified URL (may include a msg) # adds an 'error' cookie to the redirect #* my ($url, $msg) = @_; # a url to redirect to && a msg my $html = redir3($url, $msg . " by?", 'e'); return $html; # a scalar #usage: print error_redir("/subscribe.pl", "you must subscribe to ac +cess this area"); } ############################## sub notice_redir($$) { #* # redirects a client browser to a specified URL (may include a msg) # adds a 'notice' cookie to the redirect #* my ($url, $msg) = @_; # a url to redirect to && a msg my $html = redir3($url, $msg, 'n'); return $html; # a scalar #usage: print notice_redir("/', "file updated!"); } ############################## sub _tests(;$) { #* # to test all <i>Pm::Redir</i> functions #* my ($extended) = @_; # show extended data (optional) my $rv = ""; my $loggedin = cookie_get("loggedin"); my $test = ""; my $test2 = ""; my $test3 = ""; my $db = sql_connect("ns.db"); if ($db) { $test = "/index.pl"; $test2 = "Test Redirection"; $test3 = "n"; $rv .= display_debug_code("error_redir(\"$test\", \"$test2\")", er +ror_redir($test, $test2)); $rv .= display_debug_code("notice_redir(\"$test\", \"$test2\")", n +otice_redir($test, $test2)); $rv .= display_debug_code("redir(\"$test\", \"$test2\")", redir($t +est, $test2)); $rv .= display_debug_code("redir3(\"$test\", \"$test2\", \"$test3\ +")", redir3($test, $test2, $test3)); } else { $rv .= "DB connection error!<br>\n"; } return $rv; # a scalar of the results of all tests #usage: print _tests(); } ############################## ############################## ############################## ############################## ############################## ############################## ############################## ############################## ############################## ############################## ############################## ############################## ############################## ############################## ############################## ############################## ############################## ############################## 1;

        Cycles in use statements can cause issues. Often hard-to-diagnose problems.

        Breaking the cycle by making one of them a just-in-time require instead of a use is often a good solution.

        # AAA.pm package AAA; use base 'Exporter'; our @EXPORT = qw(aaa); use BBB qw(bbb); sub aaa { my $arg = shift; return 0 if $arg <= 0; return 1 + bbb($arg - 1); } 1; # BBB.pm package BBB; use base 'Exporter'; our @EXPORT = qw(bbb); # use AAA qw(aaa); sub bbb { my $arg = shift; return 0 if $arg <= 0; require AAA; return 1 + AAA::aaa($arg - 1); } 1; # main.pl use AAA; use BBB; print aaa(3) + bbb(4), "\n";
Re^4: Perl Modules
by jamroll (Beadle) on Jun 20, 2017 at 22:10 UTC
    need to talk about that lib thing, too. do i need to include that? @INC already includes the current working folder (the folder where the script resides, by default anyway, and i usually just go with that). so that when i write use Pm::Security;, the system knows to look in "./pm/" (which the ./ in my case is my web server's document root D:/Apache24/htdocs/) without me having to include use lib "...." in my code. um, what else was there about that....drawing a blank. anyway - i'll continue the modifications to my code. when i have completed the updates, and ran a test, i'll come back.
      > @INC already includes the current working folder..

      Please note that, for security reasons, . ie current directory has been removed from @INC in perl 5.26 and future realeases.

      L*

      There are no rules, there are no thumbs..
      Reinvent the wheel, then learn The Wheel; may be one day you reinvent one of THE WHEELS.
      ok. your modification suggestions seem to be working gloriously! i'm continuing to update the modules and scripts to the changes. so far so good. so, i give you my most sincere gratitude in your help, and believe me you will get a kudos on the site's about page when I'm done. My promise on that. I'll report back when I have completed the update. Again, thank you so much for your help.

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://1193180]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others musing on the Monastery: (6)
As of 2021-06-15 11:05 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    What does the "s" stand for in "perls"? (Whence perls)












    Results (70 votes). Check out past polls.

    Notices?