A ghost in the machine?

A ghost in the machine?

am 14.01.2010 19:44:48 von Tosh Cooey

Hi to everyone!

I'm trying to find out if I'm passing objects properly under mod_perl
because something is not working as I expect.

index.pl
########
use MyConfig;
my $vars = { config => &cfg };


MyConfig.pm
###########
package MyConfig;

use strict;
use Exporter;
use vars qw(@ISA @EXPORT %CFG );
use ClientConf;
@ISA = qw(Exporter);
@EXPORT = qw(%CFG &cfg);

%CFG = { global vars... };

sub cfg {
my $CFG{$clientID} = new ClientConf;
return $CFG{$clientID};
}
1;


Under normal PERL $vars->{config} is a MyConfig object. Under mod_perl
nothing is returned. Debugging with "print" statements in &cfg shows me
that "ref $CFG{$clientID}" is ClientConf, the object is there, but upon
return it just disappears.

I have other functions which "return new MyUser()" and these work
perfectly, so I'm thinking that the problem lies with the global
variable "$CFG" and that something which I think should be happening is
NOT happening.

I'm flummoxed...

Thank-you for any insights!

Tosh

--
McIntosh Cooey - Twelve Hundred Group LLC - http://www.1200group.com/

RE: A ghost in the machine?

am 14.01.2010 20:00:18 von dihnen

Global? There's no need to use a global here. You only ever reference %C=
FG *in* the package... so just make it package scoped - it'll act like a st=
atic variable and persist. (scope it with 'my' and remove it from the expo=
rt)

You can always get the value you want with a call to MyConfig::cfg

David

-----Original Message-----
From: Tosh Cooey [mailto:tosh@1200group.com]=20
Sent: Thursday, January 14, 2010 10:45 AM
To: modperl@perl.apache.org
Subject: A ghost in the machine?

Hi to everyone!

I'm trying to find out if I'm passing objects properly under mod_perl=20
because something is not working as I expect.

index.pl
########
use MyConfig;
my $vars =3D { config =3D> &cfg };


MyConfig.pm
###########
package MyConfig;

use strict;
use Exporter;
use vars qw(@ISA @EXPORT %CFG );
use ClientConf;
@ISA =3D qw(Exporter);
@EXPORT =3D qw(%CFG &cfg);

%CFG =3D { global vars... };

sub cfg {
my $CFG{$clientID} =3D new ClientConf;
return $CFG{$clientID};
}
1;


Under normal PERL $vars->{config} is a MyConfig object. Under mod_perl=20
nothing is returned. Debugging with "print" statements in &cfg shows me=20
that "ref $CFG{$clientID}" is ClientConf, the object is there, but upon=20
return it just disappears.

I have other functions which "return new MyUser()" and these work=20
perfectly, so I'm thinking that the problem lies with the global=20
variable "$CFG" and that something which I think should be happening is=20
NOT happening.

I'm flummoxed...

Thank-you for any insights!

Tosh

--=20
McIntosh Cooey - Twelve Hundred Group LLC - http://www.1200group.com/

Re: A ghost in the machine?

am 14.01.2010 21:51:26 von Tosh Cooey

True, good point. I cleaned up my code and changed some things around
and I still have the same problem:

index.pl
########
use MyConfig;
use ClientConf;
use MyUser;
my $vars = { config => &cfg };


MyConfig.pm
###########
package MyConfig;

use strict;
use Exporter;
use vars qw(@ISA @EXPORT);

@ISA = qw(Exporter);
@EXPORT = qw(&cfg &user);

my %CFG = { global vars... };

sub cfg {
return ClientConf->new();
}
sub user {
return MyUser->new();
}
1;


The function "user" works just fine, it returns the object as expected.

#### BREAK ####

So, while I was testing to make sure everything works exactly as I
described above I discovered unexpected behaviour. &cfg is actually:

sub cfg {
my ($cfg_var) = @_;
if ($cfg_var) {
...do something...
} else {
return ClientConf->new();
}
}

&user is exactly as noted above. When I changed &user and added the
same $cfg_var and conditional it also did not return what I expected.

So basically the problem I'm having is that even though I'm calling &cfg
without arguments that $cfg_var is evaluating as "TRUE", but only under
ModPerl::Registry and not under regular unadulterated PERL.

As a PERL user for 15 years am I just the biggest newbie ever, or is
there something obscure going on that I should know about?

Thanks for anything!

Tosh


Ihnen, David wrote:
> Global? There's no need to use a global here. You only ever reference %CFG *in* the package... so just make it package scoped - it'll act like a static variable and persist. (scope it with 'my' and remove it from the export)
>
> You can always get the value you want with a call to MyConfig::cfg
>
> David
>
> -----Original Message-----
> From: Tosh Cooey [mailto:tosh@1200group.com]
> Sent: Thursday, January 14, 2010 10:45 AM
> To: modperl@perl.apache.org
> Subject: A ghost in the machine?
>
> Hi to everyone!
>
> I'm trying to find out if I'm passing objects properly under mod_perl
> because something is not working as I expect.
>
> index.pl
> ########
> use MyConfig;
> my $vars = { config => &cfg };
>
>
> MyConfig.pm
> ###########
> package MyConfig;
>
> use strict;
> use Exporter;
> use vars qw(@ISA @EXPORT %CFG );
> use ClientConf;
> @ISA = qw(Exporter);
> @EXPORT = qw(%CFG &cfg);
>
> %CFG = { global vars... };
>
> sub cfg {
> my $CFG{$clientID} = new ClientConf;
> return $CFG{$clientID};
> }
> 1;
>
>
> Under normal PERL $vars->{config} is a MyConfig object. Under mod_perl
> nothing is returned. Debugging with "print" statements in &cfg shows me
> that "ref $CFG{$clientID}" is ClientConf, the object is there, but upon
> return it just disappears.
>
> I have other functions which "return new MyUser()" and these work
> perfectly, so I'm thinking that the problem lies with the global
> variable "$CFG" and that something which I think should be happening is
> NOT happening.
>
> I'm flummoxed...
>
> Thank-you for any insights!
>
> Tosh
>

--
McIntosh Cooey - Twelve Hundred Group LLC - http://www.1200group.com/

Re: A ghost in the machine?

am 14.01.2010 22:28:16 von Tosh Cooey

Ok now I'm really boggled...

If I use:

my $vars = { config => &cfg() };
instead of:
my $vars = { config => &cfg };

Then it works! So what's the difference between &cfg and &cfg() when it
comes to mod_perl, or at least ModPerl::Registry?

Thank-you all...

Tosh


Tosh Cooey wrote:
> True, good point. I cleaned up my code and changed some things around
> and I still have the same problem:
>
> index.pl
> ########
> use MyConfig;
> use ClientConf;
> use MyUser;
> my $vars = { config => &cfg };
>
>
> MyConfig.pm
> ###########
> package MyConfig;
>
> use strict;
> use Exporter;
> use vars qw(@ISA @EXPORT);
>
> @ISA = qw(Exporter);
> @EXPORT = qw(&cfg &user);
>
> my %CFG = { global vars... };
>
> sub cfg {
> return ClientConf->new();
> }
> sub user {
> return MyUser->new();
> }
> 1;
>
>
> The function "user" works just fine, it returns the object as expected.
>
> #### BREAK ####
>
> So, while I was testing to make sure everything works exactly as I
> described above I discovered unexpected behaviour. &cfg is actually:
>
> sub cfg {
> my ($cfg_var) = @_;
> if ($cfg_var) {
> ...do something...
> } else {
> return ClientConf->new();
> }
> }
>
> &user is exactly as noted above. When I changed &user and added the
> same $cfg_var and conditional it also did not return what I expected.
>
> So basically the problem I'm having is that even though I'm calling &cfg
> without arguments that $cfg_var is evaluating as "TRUE", but only under
> ModPerl::Registry and not under regular unadulterated PERL.
>
> As a PERL user for 15 years am I just the biggest newbie ever, or is
> there something obscure going on that I should know about?
>
> Thanks for anything!
>
> Tosh
>
>
> Ihnen, David wrote:
>> Global? There's no need to use a global here. You only ever
>> reference %CFG *in* the package... so just make it package scoped -
>> it'll act like a static variable and persist. (scope it with 'my' and
>> remove it from the export)
>>
>> You can always get the value you want with a call to MyConfig::cfg
>>
>> David
>>
>> -----Original Message-----
>> From: Tosh Cooey [mailto:tosh@1200group.com] Sent: Thursday, January
>> 14, 2010 10:45 AM
>> To: modperl@perl.apache.org
>> Subject: A ghost in the machine?
>>
>> Hi to everyone!
>>
>> I'm trying to find out if I'm passing objects properly under mod_perl
>> because something is not working as I expect.
>>
>> index.pl
>> ########
>> use MyConfig;
>> my $vars = { config => &cfg };
>>
>>
>> MyConfig.pm
>> ###########
>> package MyConfig;
>>
>> use strict;
>> use Exporter;
>> use vars qw(@ISA @EXPORT %CFG );
>> use ClientConf;
>> @ISA = qw(Exporter);
>> @EXPORT = qw(%CFG &cfg);
>>
>> %CFG = { global vars... };
>>
>> sub cfg {
>> my $CFG{$clientID} = new ClientConf;
>> return $CFG{$clientID};
>> }
>> 1;
>>
>>
>> Under normal PERL $vars->{config} is a MyConfig object. Under
>> mod_perl nothing is returned. Debugging with "print" statements in
>> &cfg shows me that "ref $CFG{$clientID}" is ClientConf, the object is
>> there, but upon return it just disappears.
>>
>> I have other functions which "return new MyUser()" and these work
>> perfectly, so I'm thinking that the problem lies with the global
>> variable "$CFG" and that something which I think should be happening
>> is NOT happening.
>>
>> I'm flummoxed...
>>
>> Thank-you for any insights!
>>
>> Tosh
>>
>

--
McIntosh Cooey - Twelve Hundred Group LLC - http://www.1200group.com/

Re: A ghost in the machine?

am 14.01.2010 22:42:13 von Eric Howe

Hi Tosh,

The function sigil ("&") is an archaic left over from before perl5. =
Calling a function as "&cfg" is the same as saying "&cfg(@_)" and that =
implicit "@_" was probably the source of your problem. A bit more =
information can be found here:

=
http://www.perlfoundation.org/perl5/index.cgi?subroutines_ca lled_with_the_=
ampersand

Executive summary: don't use "&" on a sub unless you need a sub-ref:

my $x =3D \&cfg;
$x->();


On 2010-01-14, at 13:28 , Tosh Cooey wrote:

> Ok now I'm really boggled...
>=20
> If I use:
>=20
> my $vars =3D { config =3D> &cfg() };
> instead of:
> my $vars =3D { config =3D> &cfg };
>=20
> Then it works! So what's the difference between &cfg and &cfg() when =
it comes to mod_perl, or at least ModPerl::Registry?
>=20
> Thank-you all...
>=20
> Tosh
>=20
>=20
> Tosh Cooey wrote:
>> True, good point. I cleaned up my code and changed some things =
around and I still have the same problem:
>> index.pl
>> ########
>> use MyConfig;
>> use ClientConf;
>> use MyUser;
>> my $vars =3D { config =3D> &cfg };
>> MyConfig.pm
>> ###########
>> package MyConfig;
>> use strict;
>> use Exporter;
>> use vars qw(@ISA @EXPORT);
>> @ISA =3D qw(Exporter);
>> @EXPORT =3D qw(&cfg &user);
>> my %CFG =3D { global vars... };
>> sub cfg {
>> return ClientConf->new();
>> }
>> sub user {
>> return MyUser->new();
>> }
>> 1;
>> The function "user" works just fine, it returns the object as =
expected.
>> #### BREAK ####
>> So, while I was testing to make sure everything works exactly as I =
described above I discovered unexpected behaviour. &cfg is actually:
>> sub cfg {
>> my ($cfg_var) =3D @_;
>> if ($cfg_var) {
>> ...do something...
>> } else {
>> return ClientConf->new();
>> }
>> }
>> &user is exactly as noted above. When I changed &user and added the =
same $cfg_var and conditional it also did not return what I expected.
>> So basically the problem I'm having is that even though I'm calling =
&cfg without arguments that $cfg_var is evaluating as "TRUE", but only =
under ModPerl::Registry and not under regular unadulterated PERL.
>> As a PERL user for 15 years am I just the biggest newbie ever, or is =
there something obscure going on that I should know about?
>> Thanks for anything!
>> Tosh
>> Ihnen, David wrote:
>>> Global? There's no need to use a global here. You only ever =
reference %CFG *in* the package... so just make it package scoped - =
it'll act like a static variable and persist. (scope it with 'my' and =
remove it from the export)
>>>=20
>>> You can always get the value you want with a call to MyConfig::cfg
>>>=20
>>> David
>>>=20
>>> -----Original Message-----
>>> From: Tosh Cooey [mailto:tosh@1200group.com] Sent: Thursday, January =
14, 2010 10:45 AM
>>> To: modperl@perl.apache.org
>>> Subject: A ghost in the machine?
>>>=20
>>> Hi to everyone!
>>>=20
>>> I'm trying to find out if I'm passing objects properly under =
mod_perl because something is not working as I expect.
>>>=20
>>> index.pl
>>> ########
>>> use MyConfig;
>>> my $vars =3D { config =3D> &cfg };
>>>=20
>>>=20
>>> MyConfig.pm
>>> ###########
>>> package MyConfig;
>>>=20
>>> use strict;
>>> use Exporter;
>>> use vars qw(@ISA @EXPORT %CFG );
>>> use ClientConf;
>>> @ISA =3D qw(Exporter);
>>> @EXPORT =3D qw(%CFG &cfg);
>>>=20
>>> %CFG =3D { global vars... };
>>>=20
>>> sub cfg {
>>> my $CFG{$clientID} =3D new ClientConf;
>>> return $CFG{$clientID};
>>> }
>>> 1;
>>>=20
>>>=20
>>> Under normal PERL $vars->{config} is a MyConfig object. Under =
mod_perl nothing is returned. Debugging with "print" statements in &cfg =
shows me that "ref $CFG{$clientID}" is ClientConf, the object is there, =
but upon return it just disappears.
>>>=20
>>> I have other functions which "return new MyUser()" and these work =
perfectly, so I'm thinking that the problem lies with the global =
variable "$CFG" and that something which I think should be happening is =
NOT happening.
>>>=20
>>> I'm flummoxed...
>>>=20
>>> Thank-you for any insights!
>>>=20
>>> Tosh
>>>=20
>=20
> --=20
> McIntosh Cooey - Twelve Hundred Group LLC - http://www.1200group.com/


Eric Howe
eric@pieinsky.ca

Re: A ghost in the machine?

am 15.01.2010 00:41:25 von Tosh Cooey

Ha ha! The ghost is me, I'm an archaic leftover! I did say I been in
this for 15 years ;)

Well Gang, we solved the smaller @_ mystery but not the larger
"different behaviour under mod_perl" mystery. I have my bets on it
being Old Man Jenkins trying to scare everybody away by wearing a costume.

So?

Thanks Eric...

Tosh


Eric Howe wrote:
> Hi Tosh,
>
> The function sigil ("&") is an archaic left over from before perl5. Calling a function as "&cfg" is the same as saying "&cfg(@_)" and that implicit "@_" was probably the source of your problem. A bit more information can be found here:
>
> http://www.perlfoundation.org/perl5/index.cgi?subroutines_ca lled_with_the_ampersand
>
> Executive summary: don't use "&" on a sub unless you need a sub-ref:
>
> my $x = \&cfg;
> $x->();
>
>
> On 2010-01-14, at 13:28 , Tosh Cooey wrote:
>
>> Ok now I'm really boggled...
>>
>> If I use:
>>
>> my $vars = { config => &cfg() };
>> instead of:
>> my $vars = { config => &cfg };
>>
>> Then it works! So what's the difference between &cfg and &cfg() when it comes to mod_perl, or at least ModPerl::Registry?
>>
>> Thank-you all...
>>
>> Tosh
>>
>>
>> Tosh Cooey wrote:
>>> True, good point. I cleaned up my code and changed some things around and I still have the same problem:
>>> index.pl
>>> ########
>>> use MyConfig;
>>> use ClientConf;
>>> use MyUser;
>>> my $vars = { config => &cfg };
>>> MyConfig.pm
>>> ###########
>>> package MyConfig;
>>> use strict;
>>> use Exporter;
>>> use vars qw(@ISA @EXPORT);
>>> @ISA = qw(Exporter);
>>> @EXPORT = qw(&cfg &user);
>>> my %CFG = { global vars... };
>>> sub cfg {
>>> return ClientConf->new();
>>> }
>>> sub user {
>>> return MyUser->new();
>>> }
>>> 1;
>>> The function "user" works just fine, it returns the object as expected.
>>> #### BREAK ####
>>> So, while I was testing to make sure everything works exactly as I described above I discovered unexpected behaviour. &cfg is actually:
>>> sub cfg {
>>> my ($cfg_var) = @_;
>>> if ($cfg_var) {
>>> ...do something...
>>> } else {
>>> return ClientConf->new();
>>> }
>>> }
>>> &user is exactly as noted above. When I changed &user and added the same $cfg_var and conditional it also did not return what I expected.
>>> So basically the problem I'm having is that even though I'm calling &cfg without arguments that $cfg_var is evaluating as "TRUE", but only under ModPerl::Registry and not under regular unadulterated PERL.
>>> As a PERL user for 15 years am I just the biggest newbie ever, or is there something obscure going on that I should know about?
>>> Thanks for anything!
>>> Tosh
>>> Ihnen, David wrote:
>>>> Global? There's no need to use a global here. You only ever reference %CFG *in* the package... so just make it package scoped - it'll act like a static variable and persist. (scope it with 'my' and remove it from the export)
>>>>
>>>> You can always get the value you want with a call to MyConfig::cfg
>>>>
>>>> David
>>>>
>>>> -----Original Message-----
>>>> From: Tosh Cooey [mailto:tosh@1200group.com] Sent: Thursday, January 14, 2010 10:45 AM
>>>> To: modperl@perl.apache.org
>>>> Subject: A ghost in the machine?
>>>>
>>>> Hi to everyone!
>>>>
>>>> I'm trying to find out if I'm passing objects properly under mod_perl because something is not working as I expect.
>>>>
>>>> index.pl
>>>> ########
>>>> use MyConfig;
>>>> my $vars = { config => &cfg };
>>>>
>>>>
>>>> MyConfig.pm
>>>> ###########
>>>> package MyConfig;
>>>>
>>>> use strict;
>>>> use Exporter;
>>>> use vars qw(@ISA @EXPORT %CFG );
>>>> use ClientConf;
>>>> @ISA = qw(Exporter);
>>>> @EXPORT = qw(%CFG &cfg);
>>>>
>>>> %CFG = { global vars... };
>>>>
>>>> sub cfg {
>>>> my $CFG{$clientID} = new ClientConf;
>>>> return $CFG{$clientID};
>>>> }
>>>> 1;
>>>>
>>>>
>>>> Under normal PERL $vars->{config} is a MyConfig object. Under mod_perl nothing is returned. Debugging with "print" statements in &cfg shows me that "ref $CFG{$clientID}" is ClientConf, the object is there, but upon return it just disappears.
>>>>
>>>> I have other functions which "return new MyUser()" and these work perfectly, so I'm thinking that the problem lies with the global variable "$CFG" and that something which I think should be happening is NOT happening.
>>>>
>>>> I'm flummoxed...
>>>>
>>>> Thank-you for any insights!
>>>>
>>>> Tosh
>>>>
>> --
>> McIntosh Cooey - Twelve Hundred Group LLC - http://www.1200group.com/
>
>
> Eric Howe
> eric@pieinsky.ca
>
>

--
McIntosh Cooey - Twelve Hundred Group LLC - http://www.1200group.com/

Re: A ghost in the machine?

am 15.01.2010 10:30:38 von torsten.foertsch

On Friday 15 January 2010 00:41:25 Tosh Cooey wrote:
> Well Gang, we solved the smaller @_ mystery but not the larger
> "different behaviour under mod_perl" mystery.
>
No, we have.

A registry script is wrapped into a subroutine. So your index.pl will look
like:

sub ... {
package ...;
use MyConfig;
use ClientConf;
use MyUser;
my $vars = { config => &cfg };
...
}

This function is called with $r as the one and only parameter. So @_=($r) at
the time of calling &cfg. That means you is fact call &cfg($r).

Torsten

Re: A ghost in the machine?

am 15.01.2010 14:20:53 von Tosh Cooey

Case closed.

Danke an alle!

Tosh


Torsten Förtsch wrote:
> On Friday 15 January 2010 00:41:25 Tosh Cooey wrote:
>> Well Gang, we solved the smaller @_ mystery but not the larger
>> "different behaviour under mod_perl" mystery.
>>
> No, we have.
>
> A registry script is wrapped into a subroutine. So your index.pl will look
> like:
>
> sub ... {
> package ...;
> use MyConfig;
> use ClientConf;
> use MyUser;
> my $vars = { config => &cfg };
> ...
> }
>
> This function is called with $r as the one and only parameter. So @_=($r) at
> the time of calling &cfg. That means you is fact call &cfg($r).
>
> Torsten
>

--
McIntosh Cooey - Twelve Hundred Group LLC - http://www.1200group.com/