in reply to A lil module creation advice

snafu,

Are the modules in question .pm files being used (pulled in at compile time) or .pl files being required (at run time)?

I suspect the former. If a used file is pulled in, any mainline code (not part of a sub) actually gets run at compile time - as if it were in a BEGIN block.

This is handy for initialisation code, but if your colleague is stuffing application code in here, it will indeed happen at compile time.

On the subject of package main;, this just means that any subs will be placed in the main:: namespace, thus defensively negating any package declaration prior to calling the module. IMO this should not be done, as the module should not be dictating the namespace to its caller, (unless we are talking OO, which is a different ball game).

Also note that leaving the use or require restores the namespace to that of the calling code.

For more on this, see perldoc perlfunc sections on use and require. I also recommend buying a copy of the Camel book for your colleague.

I may be able to answer more specifically if you post some code.

hth

--rW

Replies are listed 'Best First'.
Re: Re: A lil module creation advice
by snafu (Chaplain) on Apr 21, 2002 at 03:26 UTC
    <indent>Are the modules in question .pm files being used (pulled in at compile time) or .pl files being required (at run time)?</indent>

    Yes. These files are called script_name.pm with their first line containing package main; and every module that is being written is being called from the main script as use script_name. Now, I realize that this i s just basicly placing the namespace of each script into main:: which is fine but I am still not clear why my subs are being run at compile time.

    Here is a portion of my code:

    38 my %sn; # temporary 39 $sn{chanserv} = "ChanServ"; # temporary 40 41 my $_name = $sn{chanserv}; 42 my($_source,$_data) = (shift,shift); 43 44 $_data = &_cs_parse_raw_cmd($_data) if $_data; 45 &_sendit($_name,$_source,$_data); 46 47 # ************************************************************ 48 # sub routines 49 # ************************************************************ 50 51 # *** 52 # _send_it(): simple wrapper for chanserv to send data 53 # through the main socket. 54 # *** 55 56 sub _sendit 57 { 58 my $_channel = shift; 59 my $_message = shift; 60 61 # send_data(':%s ! %s :%s', $_name, $_channel, "I saw your + message: $_message"); 62 printf(":%s ! %s :%s\n", $_name, $_channel, "I saw your m +essage: $_message"); 63 } 64 65 66 # *** 67 # _cs_parse_raw_cmd() : parses out raw data from main destined 68 # for chanserv. This will parse the raw packet. 69 # *** 70 71 sub _cs_parse_raw_cmd 72 { 73 my ($cmd) = @_ ; 74 75 # Chanserv :test 76 # ChanServ@Services.Netfrag.Com :test 77 78 my ($_to_whom, 79 $_message) = split(/:/,$cmd); 80 81 # First, make sure that we are the right one getting call +ed 82 unless ( /\#/ ) { 83 if ( lc($_to_whom) !~ m/$_name/i ) { 84 &sendit($_source,"Please let an admin know that s +omething is broke."); 85 } 86 } 87 88 return($_message); 89 } 90 91 1;

    Now. I have just spoken to the main coder about my issue and he made it clear that what I was doing was setting a scalar by calling a function which of course I was but that this was causing the problem. He also said that I needed to simply make a bunch of functions and not worry about creating any global scalars or arrays.

    Everything makes sense to me now but Im still curious what the other monks think.

    _ _ _ _ _ _ _ _ _ _
    - Jim
    Insert clever comment here...

      Now. I have just spoken to the main coder about my issue and he made it clear that what I was doing was setting a scalar by calling a function which of course I was but that this was causing the problem. He also said that I needed to simply make a bunch of functions and not worry about creating any global scalars or arrays.
      Nice to see that you got to the bottom of it. Global variables are generally agreed to be bad news, and show up quite clearly with
      use strict;
      In fact, if you try to run code with global variables in an Apache+ModPerl configuration, it will break in strange, wonderful and inconsistent ways.

      As greywolf says, use strict; is your friend.

      See also this thread for a discussion on use of global variables.