You are viewing a plain text version of this content. The canonical link for it is here.
Posted to modperl@perl.apache.org by Steve Hay <St...@uk.radan.com> on 2001/03/16 13:15:23 UTC

Shared variables, inner subs and "our"

Hi,

I was just tidying up an old mod_perl script which had some ugly "use
vars qw(...);" lines in it which I thought I'd replace with "our ...;".
I realise this isn't always a good idea since "our" is not intended as a
replacement for "use vars", but its often OK and I thought it would be
in my case.

I was only half right:  The script still works fine, but emits warnings
which it previously didn't about "variable will not stay shared".

The mod_perl Guide (1.28) refers to such problems in section 3.5:

It gives as an example the following program:

    use strict;
    use warnings;

    sub print_power_of_2 {
        my $x = shift;
        sub power_of_2 {
             return $x ** 2;
        }
        my $result = power_of_2();
        print "$x^2 = $result\n";
    }

    print_power_of_2(5);
    print_power_of_2(6);

This prints:

    Variable "$x" will not stay shared at ./nested.pl line 7.
    5^2 = 25
    6^2 = 25

The solution is to use a package-global $x which won't get deep-bound
into power_of_2():

    use strict;
    use warnings;

    sub print_power_of_2 {
        use vars qw($x);
        $x = shift;
        sub power_of_2 {
             return $x ** 2;
        }
        my $result = power_of_2();
        print "$x^2 = $result\n";
    }

    print_power_of_2(5);
    print_power_of_2(6);

This prints:

    5^2 = 25
    6^2 = 36

However, if you change the ugly "use vars" to the
sexier-although-not-quite-the-same "our":

    use strict;
    use warnings;

    sub print_power_of_2 {
        our $x = shift;
        sub power_of_2 {
             return $x ** 2;
        }
        my $result = power_of_2();
        print "$x^2 = $result\n";
    }

    print_power_of_2(5);
    print_power_of_2(6);

then it prints:

    Variable "$x" will not stay shared at ./nested.pl line 7.
    5^2 = 25
    6^2 = 36

!!!

In other words, we get a bizarre cross between the two: the warning
about $x not staying shared is emitted, but of course its nonsense (?)
because package-globals don't get deep-bound into subroutines anyway,
and the program actually works fine!

The eagle-eyed will have noticed that the above "use vars" solution is
not *exactly* as presented in the mod_perl Guide:  the solution there
puts the "use vars" *outside" of the declaration of print_power_of_2(),
not *inside* as above.  This, of course, makes no difference to "use
vars" which affects the package, not a lexical scope.

But it *does* make a big difference to "our", which applies to a lexical
scope, not a package:  If we move the "our" *outside* of the declaration
of print_power_of_2():

    use strict;
    use warnings;

    our $x;

    sub print_power_of_2 {
        $x = shift;
        sub power_of_2 {
             return $x ** 2;
        }
        my $result = power_of_2();
        print "$x^2 = $result\n";
    }

    print_power_of_2(5);
    print_power_of_2(6);

then the confusing warning goes away:

    5^2 = 25
    6^2 = 36

Why am I bringing this up?

(a) because I think the mod_perl Guide needs to mention the use of "our"
as well as "use vars" (they're only very briefly mentioned, regarding
something else, in section 10.6.5);
(b) because I can't actually do what I just did above in my mod_perl
script!

I run my mod_perl script under Apache::Registry, which (as we all know)
makes the script into a subroutine, and therefore any subroutines into
inner subroutines.

In the example above, print_power_of_2() is like my script, power_of_2()
is like a subroutine in my script, and the two calls to
print_power_of_2() are like my script being run twice.

Obviously I can't move the "our" declaration *outside* my script like I
did above (unless Apache::Registry did this for me when it does its
stuff with my script), so I'm stuck with the warning (or else "use
vars").

Is there some reason why the warning gets emitted with "our" inside
print_power_of_2()?  Was I just lucky that this particular example
worked and I should really heed the warning, or is the warning actually
bogus?

Is there any way I can use "our" rather than "use vars" and not get
these warnings?

- Steve Hay



Re: perl5.6 (was: Shared variables, inner subs and "our")

Posted by Dave Rolsky <au...@urth.org>.
On Fri, 16 Mar 2001, Bogomolnyi Constantin wrote:

> You should probably try 5.7.0 witch is much more stable than 5.6.0 (you
> should not try unicode stuff , whitch is quite buggy)
>
> I use 5.7.0  on all my production servers without any problems .

5.7.0 may have fixed some of the bugs of 5.6.0 but it is a dev release
(anything where the subversion is odd is a dev release).  I don't think
anyone working on the Perl core would recommend the use of a dev release
in production unless you absolutely need a piece of functionality present
in that particular dev release.

I'd suggest using 5.00503 until 5.6.1 comes out, which should fix lots of
bugs.


-dave

/*==================
www.urth.org
We await the New Sun
==================*/


Re: perl5.6 (was: Shared variables, inner subs and "our")

Posted by Bogomolnyi Constantin <Co...@ifrance.com>.
Hi ,

You should probably try 5.7.0 witch is much more stable than 5.6.0 (you
should not try unicode stuff , whitch is quite buggy)

I use 5.7.0  on all my production servers without any problems .

Best
----- Original Message -----
From: "Wim Kerkhoff" <wi...@merilus.com>
To: "modperl" <mo...@apache.org>
Sent: Saturday, March 17, 2001 6:20 AM
Subject: Re: perl5.6 (was: Shared variables, inner subs and "our")


> Stas Bekman wrote:
>
> > our() and other perl5.6 new APIs are too early to be endorsed, as 5.6 is
> > not yet considered as a stable version for mod_perl production sites,
> > therefore the guide barely touches on it.
>
> Would you recommend the use of perl5.6 with mod_perl?  What you are
> saying is making me queasy.
>
> I'm asking because I've been having bad luck with Apache::Session and
> some other modules in some mod_perl aware applications. Things aren't
> comming out the data source properly, things are hanging, things aren't
> locking/unlocking properly, etc. It could well be that the applications
> I'm working with aren't using Sessions, Tie::Cache, etc properly. I may
> downgrade to perl5.005 and give that a whirl...
>
> --
>
> Regards,
>
> Wim Kerkhoff
>


Re: perl5.6 (was: Shared variables, inner subs and "our")

Posted by Stas Bekman <st...@stason.org>.
On Fri, 16 Mar 2001, Wim Kerkhoff wrote:

> Stas Bekman wrote:
>
> > our() and other perl5.6 new APIs are too early to be endorsed, as 5.6 is
> > not yet considered as a stable version for mod_perl production sites,
> > therefore the guide barely touches on it.
>
> Would you recommend the use of perl5.6 with mod_perl?  What you are
> saying is making me queasy.
>
> I'm asking because I've been having bad luck with Apache::Session and
> some other modules in some mod_perl aware applications. Things aren't
> comming out the data source properly, things are hanging, things aren't
> locking/unlocking properly, etc. It could well be that the applications
> I'm working with aren't using Sessions, Tie::Cache, etc properly. I may
> downgrade to perl5.005 and give that a whirl...

Please search the archives. There were reports of problems with perl5.6,
Doug has submitted quite a few fixes for 5.6 to make it working with
mod_perl. I'm not sure whether perl5.6-PATCH2 is bugless with regard to
mod_perl. Not to mention possible problems that has nothing to do with
mod_perl, but are general Perl issues.

Therefore I still use 5.00503 on my production sites. But some people are
using 5.6 without any problems on their production sites.

_____________________________________________________________________
Stas Bekman              JAm_pH     --   Just Another mod_perl Hacker
http://stason.org/       mod_perl Guide  http://perl.apache.org/guide
mailto:stas@stason.org   http://apachetoday.com http://logilune.com/
http://singlesheaven.com http://perl.apache.org http://perlmonth.com/



Re: perl5.6 (was: Shared variables, inner subs and "our")

Posted by Wim Kerkhoff <wi...@merilus.com>.
Stas Bekman wrote:

> our() and other perl5.6 new APIs are too early to be endorsed, as 5.6 is
> not yet considered as a stable version for mod_perl production sites,
> therefore the guide barely touches on it.

Would you recommend the use of perl5.6 with mod_perl?  What you are
saying is making me queasy.

I'm asking because I've been having bad luck with Apache::Session and
some other modules in some mod_perl aware applications. Things aren't
comming out the data source properly, things are hanging, things aren't
locking/unlocking properly, etc. It could well be that the applications
I'm working with aren't using Sessions, Tie::Cache, etc properly. I may
downgrade to perl5.005 and give that a whirl...

-- 

Regards,

Wim Kerkhoff

Re: Shared variables, inner subs and "our"

Posted by Stas Bekman <st...@stason.org>.
On Fri, 16 Mar 2001, Paul wrote:

>
> not really answering your questions, but....
>
> Ragarding moving the "our()" statement out of the function --
>
> --- Steve Hay <St...@uk.radan.com> wrote:
> > (b) because I can't actually do what I just did above in my mod_perl
> > script!
>
> Couldn't you, if you put the func's into a module and our()'d it there,
> then use()'d it from your script?
>
> I realize that sort of redesign might not be convenient, but wouldn't
> it work?

That's exactly what I was going to anwer.

The example in the guide *demonstrates* the problem and possible
workarounds for it. But it states that the best approach is to have your
registry script with a single call to some method in another module, which
solves all the problems.

Of course you can go for workarounds, but those will haunt you until you
will give up and redesign (move the code out of the script). If you need a
quick solution with no redesign, you have it in the guide.

our() and other perl5.6 new APIs are too early to be endorsed, as 5.6 is
not yet considered as a stable version for mod_perl production sites,
therefore the guide barely touches on it.

_____________________________________________________________________
Stas Bekman              JAm_pH     --   Just Another mod_perl Hacker
http://stason.org/       mod_perl Guide  http://perl.apache.org/guide
mailto:stas@stason.org   http://apachetoday.com http://logilune.com/
http://singlesheaven.com http://perl.apache.org http://perlmonth.com/



Re: Shared variables, inner subs and "our"

Posted by Paul <yd...@yahoo.com>.
not really answering your questions, but....

Ragarding moving the "our()" statement out of the function --

--- Steve Hay <St...@uk.radan.com> wrote:
> (b) because I can't actually do what I just did above in my mod_perl
> script!

Couldn't you, if you put the func's into a module and our()'d it there,
then use()'d it from your script?

I realize that sort of redesign might not be convenient, but wouldn't
it work?

__________________________________________________
Do You Yahoo!?
Get email at your own domain with Yahoo! Mail. 
http://personal.mail.yahoo.com/