Be careful of using array reference under mod_perl

beckheng on 2007-02-27T08:13:14

I store some value with an array reference like :
$self->{stash}->{css_files} = ['a.css', 'b.css'];
I append another css file after with push function, so the array expand more and more.

So I must be avoid to using array reference in config variables.


what's the trouble?

perrin on 2007-02-27T22:27:52

I don't know of any problem with using array references in mod_perl. Can you explain what's giving you trouble? Maybe we can help.

Re:what's the trouble?

beckheng on 2007-02-28T15:55:49

Any help is luck for me.
These three script show what happens.
-------configuration file(MyConfigTest.pm)--

package PBB::Packages::MyConfigTest;

use strict;

@PBB::Packages::MyConfigTest::ISA = qw(Exporter);
%PBB::Packages::MyConfigTest::stash = (

    "p_type" => "text/html",
    "p_charset" => "UTF-8",
    "p_theme" => "gnome-default",
    "css_files" => ["aa.js", "bb.js"],
);

1;
-------startup file(mystartup.pl)-------

#!/usr/bin/perl

use lib qw(/path/myprj);
use strict;

1;
-------my module file(ControllerTest.pm)--

package PBB::ControllerTest;

use vars qw($q $t);
use vars qw(%stash);
use Apache2::RequestIO ();
use Apache2::Const -compile => ':common';
use CGI qw();
use CGI::Carp qw(fatalsToBrowser);
use PBB::Packages::MyConfigTest;
use Data::Dumper;
use strict;

*stash = \%PBB::Packages::MyConfigTest::stash;

sub handler{
    my $r = shift;
    $q = new CGI;
    print $q->header();
    $t = {};
    @{$t->{stash}}{keys %stash} = values %stash;
    print Dumper($t->{stash});
    push @{$t->{stash}->{css_files}}, "a.js", "b.js";
    print "<br /><br />";
    print Dumper($t->{stash});
    return Apache2::Const::OK;
}

1;
and the follow is the apache config section

<IfModule mod_perl.c>
    PerlRequire "/path/myprj/mystartup.pl"
    <Location /ptest>
        SetHandler perl-script
        PerlResponseHandler PBB::ControllerTest
        PerlOptions +ParseHeaders
        Order allow,deny
        Allow from all
    </Location>
</IfModule>
while browse with http://127.0.0.1/ptest ,I see the result:

$VAR1 = { 'p_type' => 'text/html', 'css_files' => [ 'aa.js', 'bb.js', 'a.js', 'b.js', 'c.js', 'a.js', 'b.js', 'c.js', 'a.js', 'b.js', 'c.js' ], 'p_theme' => 'gnome-default', 'p_charset' => 'UTF-8' };

$VAR1 = { 'p_type' => 'text/html', 'css_files' => [ 'aa.js', 'bb.js', 'a.js', 'b.js', 'c.js', 'a.js', 'b.js', 'c.js', 'a.js', 'b.js', 'c.js', 'a.js', 'b.js', 'c.js' ], 'p_theme' => 'gnome-default', 'p_charset' => 'UTF-8' };

What's wrong with me?

Regards.

Re:what's the trouble?

perrin on 2007-02-28T18:40:34

I don't understand. Didn't you want the array to expand? If you were trying to replace it, just set it directly instead of using push:

$t->{stash}->{css_files} = ['a.js', 'b.js'];

There are several other things in this code that I would suggest changing, but this forum isn't the best place to do a code review. If you'd like more advice on your code, please post it on the mod_perl mailing list, or on http://perlmonks.org/.

Re:what's the trouble?

beckheng on 2007-03-01T02:55:16

I make mistake.
Direct assign value works good.
I never think it before.I always use push.
For more advice, I post it on
http://perlmonks.org/?node_id=602639


thx.
Regards.