Beefy Boxes and Bandwidth Generously Provided by pair Networks
good chemistry is complicated,
and a little bit messy -LW
 
PerlMonks  

Re^6: Perl Modules -- abstraction and interfaces: exporter and @ISA

by jamroll (Beadle)
on Jul 11, 2017 at 13:54 UTC ( [id://1194820]=note: print w/replies, xml ) Need Help??


in reply to Re^5: Perl Modules -- abstraction and interfaces: exporter and @ISA
in thread Perl Modules

thank you, sir. again, a good read. can't say i fully understood all of that, but i get the gist. See, i have much programming experience - coding in so many languages, including c/c++. I liked OO and inheritance and the "Is A" and "Has A" relationships. when i began coding my new site, i had not known perl could do OO...as I code the site, i learned so much and realize i should have gone OO with this...but, i'm several modules in (11 modules), and I have so many scripts that use the modules (some 32 scripts). like the song i'm hearing says right now - "it's too late". lol - i can't change it. so i'm stuck with the procedural method for now.

the next question on my mind....i'll use code to describe the question, cuz it gets my point across far easier than trying to use words to describe the question

package Blah::Blah::BlackSheep; use strict; use warnings; # etc, etc...etc... my $loggedin = cookie_get("loggedin"); # does exactly what you think i +t does... my $page = get_param(get_constant($db, "QUERY_PAGE")); # the page the +user wants to visit # cookie_get, get_param, get_constant are all subs I created to ease m +y coding a little, despite it being a tad...convoluted. it works. t +hat's what matters if (not allowed($page, $loggedin)) { print cookie_set("error", "Access Denied!"); print "location: /\n\n"; exit 1; } sub something { return "blah"; } sub something2 { return "bleet"; } # etc, etc, etc... 1;
is this "good practice" - i mean the bit before the first sub?

why do i ask? I don't want to have to code the above into EVERY script i write....that's prone to errors, and can be a real B when a change is needed....

if this isn't good practice, how can i accomplish this task? I know PERL allows the above code - compiles without complaint, and it does work....just wanted to know if this is the right way to go about it...?
  • Comment on Re^6: Perl Modules -- abstraction and interfaces: exporter and @ISA
  • Download Code

Replies are listed 'Best First'.
Re^7: Perl Modules -- abstraction and interfaces: exporter and @ISA
by haukex (Archbishop) on Jul 11, 2017 at 19:25 UTC
    is this "good practice" - i mean the bit before the first sub?

    I would answer this with "no". As you said, it might work, but there are two main reasons I would recommend against it.

    First a practical point: usually, when you write a module like that, you'd be loading it with use. However, as documented in use, that code gets run in a BEGIN block, that is, while the calling code is still being complied. For some code, it will still work, but on the other hand you may get some unexpected effects (e.g. this thread). Also, modules might not yet be loaded and/or initialized depending on the order of the use statements in the calling code! (Even if you use require to load modules at runtime instead of compile time, note that this will lead to other possibly unexpected effects, like the code in that module still being executed only once, or you having to use parentheses on all your subroutine calls imported from that module.) This is why modules that are intended to be used usually limit themselves to declarations of subs and package variables (Update: and often the exporting of those subs into the caller's namespace, e.g. Exporter), and if they do have initialization code, it is limited to things that are internal to that module without externally visible side effects. (Update 2: Note I'm ignoring more complicated things like custom pragmas here, which are also just special kinds of modules.)

    Second, something to do more with convention: When I load a module with use, I don't expect that call to have any side effects other than loading that module, but the code you showed not only prints something, it may even kill my whole program with exit!

    So what can you do instead? If you want to write a module that is intended to be loaded with use, then it's easiest when it only contains sub definitions and perhaps declarations of package variables (our). If it does execute code on loading, then it should only be for its internal initialization, i.e. it shouldn't have any side effects visible to outside code, and it needs to consider that it is being executed in a BEGIN block, as I explained above (Update: i.e., I wouldn't mess with cookies or CGI params that that point yet). If you want to include initialization code with effects visible to the outside code, then for example, put it in a sub init that the user has to explicitly call.

    Another approach, which I would consider to be less elegant, would be to use do to explicitly execute another Perl file. Unlike require, that Perl file will be executed every time you call do instead of only once, so it's more like calling a sub than loading a module. However, in that case it's not necessarily the best place to put subs etc., so you'd have to consider splitting the code you showed into two parts. (So as you can tell, it's probably easier to just put the init code in a sub init instead.)

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others sharing their wisdom with the Monastery: (2)
As of 2024-04-26 00:57 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found