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

Now, since I am a relative new-bee when it comes to some of this stuff, careful before you --.

I have a couple questions about mod_perl's way of declaring functions. I've noticed a couple differences between CGI and mod_perl. Noted below:

sub cgi_sub(){ print $_[0]; } sub mod_perl_sub { print $_[0]; }
The real question here is with the ()'s. Do I need them for a cgi script? I know I could try it without, and it'll work. So maybe the real-real question here is, can somebody explain what happens to $_ and @_ when you have a empty ()'s in the declration of a function?

This stemmed when I had a script that would run perfectly as a cgi script. When ported to mod_perl, I had left the ()'s and the fuctions wheren't recieving paramaters properly.

Maybe I'm crazy and completly missing something ELSE, but it couldnt hurt to ask. :)

I guess I'm just curious, and well, curious.

_14k4 - webmaster@poorheart.com (www.poorheart.com)

Replies are listed 'Best First'.
Re: mod_perl and function declrations
by arturo (Vicar) on Apr 17, 2001 at 23:15 UTC

    The key is understanding how mod_perl -- specifically, Apache::Registry -- handles a script. Basically, what it does is take each script and turn it into a subroutine. That one subroutine is compiled and can be executed again and again, without recompiling, which is one reason scripts run faster under Apache::Registry. But it makes certain scoping issues come into play, so you are exhorted more loudly than ever to use strict and declare variables with my, to ensure you don't have values lingering from previous invocations of your script, which is now a subroutine.

    I can't tell you exactly why you're seeing differences (especially since you post no code! hint, hint =), but a lot might depend on how you're making calls to your subroutines. Make sure you always call them with

    sub_name(ARG LIST)

    and you shouldn't see any weirdnesses. If you make those changes and are still having problems, come back and ask again, with more specifics about the problem and a minimal code that produces the problem.

    HTH.

    Creates a sub bar that can be called from anywhere.

    Philosophy can be made out of anything. Or less -- Jerry A. Fodor

      As always, I find some insite here. The subroutine samples I had given were the way I was coding them when porting from cgi to mod_perl. I was calling the subroutines like so:
      my $content = do_something_in_mod_perl(name => $name, age => $age);
      And so forth. Well, the explanation you gave made sense to me, which is a good thing. Hopefully. Thank you for the help and ideas.

      _14k4 - webmaster@poorheart.com (www.poorheart.com)
Re: mod_perl and function declrations
by jwest (Friar) on Apr 18, 2001 at 00:34 UTC
    The difference is purely superficial, AFAIK. The sub() operator within Perl is subject to most of the same rules as the rest. That being said, parentheses are largely optional and are there for readability and grouping purposes only.

    Consider these two declarations:

    sub foo () { ... } sub bar { ... }

    To Perl, they are broken up into the following 'chunks':

    • Function name: 'sub'
    • NAME: 'foo' and 'bar', respectively
    • PROTOTYPE: '()' or '', respectively
    • BLOCK: '{\n...\n}'

    What Perl is then doing is substituting defaults for missing chunks. In the PROTOTYPE chunk, where you have a null value in the bar subroutine, essentially, you can think of it as substituting the empty list as the foo subroutine has. (Really, though, it may be doing something quite different, and likely is- I have no idea what Perl does under the covers- yet. ;)

    It's just as the calls to print that you make in your code could just as easily have been written:
    print ($_[0]);
    instead of without the parenthesis. The interpreter is usually very good about guessing at what you really mean.

    So, if you use a prototype as part of the subroutine, you'll need the parentheses to group the prototype together. Declaring a subroutine with no prototype is, essentially, the exact same thing as declaring it with an empty list, "( )", for the prototype.

    Hope this helps!

    --jwest

    -><- -><- -><- -><- -><-
    All things are Perfect
        To every last Flaw
        And bound in accord
             With Eris's Law
     - HBT; The Book of Advice, 1:7
    
Re: mod_perl and function declrations
by petdance (Parson) on Apr 18, 2001 at 01:17 UTC
    Short answer:
    • Empty parens mean "this function takes no arguments"
    • No parens means that the arguments are not specified at all.
    Unless you get into the whole morass that is parm specifications, just leave the parens off.
    # Andy Lester  http://www.petdance.com  AIM:petdance
    %_=split';','.; Perl ;@;st a;m;ker;p;not;o;hac;t;her;y;ju';
    print map $_{$_}, split //,
    'andy@petdance.com'
    
Re: mod_perl and function declrations
by repson (Chaplain) on Apr 18, 2001 at 18:38 UTC
    Usually the parameter context specifier is unnecessary, except in some few situations where it can be useful. It is nothing like what appears in subroutine declarations in most other programming languages and exists to provide access to syntax like core functions.
    For full descriptions read perlsub, part of the standard documention supplied with perl.
    • sub foo () { ... } - This syntax is used for functions which will never take any parameters (think of functions like time which will return a value but will never recieve any input) and also for constants which will be inlined at compile time since they never change.
    • sub blah ($) { ... } - This syntax is useful for cases like this: @foo (sin $num, time, ord $chr, @array); If sin was , time and ord didn't have specific declarations then they would eat the rest of the list availible, even if they only useds 0 or 1 items of it.
    • sub blah (&$) { ... } - This type of syntax is used by map and grep to allow simple use.