You are viewing a plain text version of this content. The canonical link for it is here.
Posted to modperl@perl.apache.org by Christoph Haberberger <Ch...@yline.at> on 2000/05/09 12:08:22 UTC

To-Experts: BIG problem: my variables in nested subroutines, whic h call each other

Hi!

I should migrate Perl-CGIs to mod_perl for performance reasons.
We use many subroutines in our CGIs, which read and write
global (my-defined) variables. Our plain subroutines are not nested,
but they are when the mod_perl wrapper is put around them at execution time.

In this special case, some values are not updated. You get this error:
Variable "$<var-name>" will not stay shared at <script-file> line <line-no>
(#1)
    
This problem is also discribed under:
http://perl.apache.org/guide/perl.html#my_Scoped_Variable_in_Nested_S

This page says to convert all subroutines to "anonymous" subroutines.
This works fine, as long as anonymous subroutines call other anonymous
subs defined before. Ok, now you can order all subroutines, so that they are
always
defined before they are called.

QUESTION: But how should I transform the script, if the anonymous subs call
each other?
I get always the following error:
Undefined soubroutine &main:: called at <script-file> line <line-no>.

I don't know how to solve this. Is there any solution for this? Can I make
any prototype defs for such subroutines?

BIG THANKS IN ADVANCE,
Christoph.

yLine Internet Services AG

Re: To-Experts: BIG problem: my variables in nested subroutines, whic h call each other

Posted by sh...@isupportlive.com.
I'm seconding Ime's suggestion, and adding one piece, you don't have
to use anonymous subroutines for everything..., that would be.., well,
a big pain.  Named subroutines where EVERYTHING it uses is passed in
via references.  Also it's return values are sent back via reference
as well.  References "under the covers" are pointers.  So if you were
to pass something to a perl function via a reference, like a hash, the
hash when it got back to it's calling context would be modified...,
this is pretty usefull.

(perldoc perlsub && perldoc perlref BTW)

The last thing is maybe you should look into isolating a lot of this
stuff into modules.  I'm sure your sub's are shared quite frequently
between many scripts..., well, there is no need to suck up all that
extra memory, just pull that into your own module set.

Thanks,
Shane.

The rule is... NO GLOBALS! :-)  Of course, every rule is made to
broken in certain situations.



Re: To-Experts: BIG problem: my variables in nested subroutines, whic h call each other

Posted by Ime Smits <im...@iaehv.iae.nl>.
| QUESTION: But how should I transform the script, if the anonymous subs call
| each other?
| I get always the following error:
| Undefined soubroutine &main:: called at <script-file> line <line-no>.
| I don't know how to solve this. Is there any solution for this? Can I make
| any prototype defs for such subroutines?

A good programming practice is to never let
a sub {} depend nor use any variables which are not explicitly
passed to it. If severel subs should have some kind of globally
used structures to work with, explicitly pass it by reference
to each sub. If you follow that basic rule, you will write code
which is both maintainable and is perfectly suitable for a mod_perl
environment.

Ime


RE: To-Experts: BIG problem: my variables in nested subroutin

Posted by Kees Vonk 7249 24549 <KE...@BGTRANSCO.CO.UK>.
> QUESTION: But how should I transform the script, if the
> anonymous subs call each other?


Very simple little example:


#!/usr/bin/perl -w

use strict;

my $i = 0;

my $a;

my $b = sub { print "$i\n"; exit if $i++ >= 10; &$a };

$a = sub { print "$i\n"; exit if $i++ >= 10; &$b };

&$a;



Does that answer your question, or is there a reason you 
cannot do this?


Kees