Beefy Boxes and Bandwidth Generously Provided by pair Networks
Your skill will accomplish
what the force of many cannot
 
PerlMonks  

Perl Modules Scope ?

by Anonymous Monk
on Oct 02, 2015 at 06:01 UTC ( [id://1143638]=perlquestion: print w/replies, xml ) Need Help??

Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

Hi, I am writing a login screen and and have following files:
lib.cgi config.pm and login.cgi
in config.pm i have following code:
package config; our $abcd = 1; 1;
in lib.cgi i have following code:
use config; 1;
in login.cgi i have following code:
use strict; required 'lib.cgi'; print $config::abcd;
on compiling this I get an error. The error goes away if i "use config" in login.cgi. I thought since I am including lib.cgi inside login.cgi I would not have to use config inside login.cgi Please help me understand this better

Replies are listed 'Best First'.
Re: Perl Modules Scope ?
by Anonymous Monk on Oct 02, 2015 at 06:07 UTC
    While perl modules are case sensitive , some filesystems are not; there already exists a Config module, so "config" or "CoNFiG" or any variation is a bad name to choose, its already taken

    Also, all lowercase names are reserved for pragmas

    Also, you shouldn't be "including" lib.cgi or login.cgi or any such thing, you're already making modules, so make them all modules, something like this

    MyApp::Config MyApp::Login MyApp::Lib MyApp::Screen::Login MyApp::Screen::Lib

    Something along those lines,

    You can find more discussions on naming modules in Re: RFC: Automatic logger module

Re: Perl Modules Scope ?
by Laurent_R (Canon) on Oct 02, 2015 at 07:34 UTC
    In your login.cgi module, you have (code tags and line returns added):
    use strict; required 'lib.cgi'; print $config::abcd; # ...
    Is the second line above ("required ...") really what you have, or is this an error when copying your code?
Re: Perl Modules Scope ?
by u65 (Chaplain) on Oct 02, 2015 at 10:31 UTC

    It would help us (certainly it would help me) if you would format your separate code chunks in code blocks. See Writeup Formatting Tips.

Re: Perl Modules Scope ?
by sundialsvc4 (Abbot) on Oct 03, 2015 at 12:33 UTC

    Like the others, I suggest that you take a double-step back from your present design, and look for something better.   In particular, for something that already exists in CPAN that is better.

    The CPAN website contains everything from full-fledged application frameworks (e.g. Dancer) to lower-level tools (e.g. CGI::Application) that have both been used to implement a helluva lot of production code that, every day, earns a helluva lot of money.   :-)   Take a good, long look at the SYNOPSIS section of both of these ... especially the latter.   You will observe a number of things.

    First, that there are rarely “many separate .cgi files.”   This is not PHP.   Even if the URIs are formed that way to facilitate the needs of browsers and their Back button, there is often just one single program that uses the URI-string as part of its input.

    Second, that the target-script in question is almost a stub ... it instantiates an object and then tells that object to “run.”   The operative routines are either used, or they are loaded on-demand via require (or [mod://UNIVERSAL::require).   This strategy provides consistency and avoids duplication of code.

    Finally, Perl web applications do not “mix source-code with HTML,” as PHP does or did.   They are almost always driven by templates (e.g. Template::Toolkit).   They consist of highly modular programs which take a URI-string as input, select some module to do processing, and use templates to prepare HTML (or JSON or what-have-you) output.   And the foundations have already been laid.   If you describe your project more, you’ll get lots of good suggestions as to what foundation might work best for your project.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://1143638]
Front-paged by Corion
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others chilling in the Monastery: (2)
As of 2024-04-26 07:49 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found