You are viewing a plain text version of this content. The canonical link for it is here.
Posted to modperl@perl.apache.org by Perrin Harkins <pe...@elem.com> on 2005/05/25 19:16:19 UTC

Re: Should I use Package or Modual or what?

On Wednesday 25 May 2005 1:19 pm, Luinrandir Insight wrote:
> As I understand Pack/Mod are loaded into memory when the main program is
> run.
> So saving memory is not the issue here... using Packs or Mods is mainly for
> programming ease.
> Yes or No?

Yes, modules are primarilly about making your code easier to work on.  
However, you do want to preload your code during the server startup, which 
saves memory.
http://modperlbook.com/html/ch10_01.html

> So the way I was going to design the program is
>
> Main program processes the <INPUT> from something like:
> main.cgi?location=castle&coins=12&horses=2.. etc.
>
> once the input is split....it goes to a big if-elsif-elsif-elsif-elsif-....

Check out the CGI::Application module on CPAN.  There are also some articles 
that have been written about it that explain the basics of separating the 
parts of your program into a model-view-controller design.

> So all I am really doing is moving the subroutines to packages....

Packages are just collections of subroutines and data under a specific 
namespace.  If you can't imagine why this is beneficial, you might want to 
read a book about programming best practices, like Code Complete.

- Perrin

[OT] Re: a mystery.. need help

Posted by Frank Maas <fr...@cheiron-it.nl>.
Hi,

(a) this is a modperl list. your question seems a general perl issue,
please use the appropriate channels for this - not this list.

(b) whatever channel you choose for your question, be sure to give 
information about what you are trying to achieve, what goes wrong, what 
you tried to solve the problem, etc.

(c) just for this once:
>             $Data[$v0] = $form{Data($v0)}; <<<<<<<< this line is bad! ! ! !
              ^^^^^^^^^^         ^^^^^^^^^
this is the $v0'th                 here you call sub/function &Data
element of @Data                   with parameter $v0

since you test if $Data[$v0] not equals "" it seems you are doing 
something you don't want to do.

I hope (c) helps you in the right direction, if not adhere to (a) and (b) 
and please do not follow-up to the list.

Thanks,

Frank


Re: [MP2] How to turn off caching?

Posted by Torsten Foertsch <to...@gmx.net>.
On Tuesday 31 May 2005 06:23, Foo Ji-Haw wrote:
> If I am not mistaken, modperl tends to cache all output until the script
> is completed, then it sends out the page. If I want to (for example)
> print a period (.) back to the browser every second, what do I need to
> do? I tried $| but it does not work.

My handler looks this:

package My::Content;
use Apache2::RequestRec ();
use Apache2::RequestIO ();
use Apache2::Const -compile=>qw(OK);

sub handler {
  my $r=shift;
  $r->content_type( 'text/html' );

  for( my $i=0; $i<10; $i++ ) {
    $r->print( time."\n" );
    $r->rflush;
    sleep 1;
  }
  $r->print( "\n" );
  return Apache2::Const::OK;
}

and here is what it sends every second one line as expected:

r2@opi:~> telnet localhost 8081
Trying 127.0.0.1...
Connected to localhost.
Escape character is '^]'.
GET /mp2-handler HTTP/1.0

HTTP/1.1 200 OK
Date: Tue, 31 May 2005 09:24:41 GMT
Server: Apache/2.0.54 (Unix) mod_perl/2.0.0 Perl/v5.8.5
Connection: close
Content-Type: text/html

1117531481
1117531482
1117531483
1117531484
1117531485
1117531486
1117531487
1117531488
1117531489
1117531490

Connection closed by foreign host.

It work with simple dots as well.

Torsten

Re: [MP2] How to turn off caching?

Posted by Issac Goldstand <ma...@beamartyr.net>.
As more of a hack than a necessarily good practice, I've found that
sending a newline (in addition to $|=1) sometimes helps.  I think the
problem here is more that the browser doesn't necessarily render content
every single time some data comes in over the socket, but maybe waits
for logical looking breakpoints to render.   Someone who knows browsers
better will probably correct me, but that's what I've found.

Newlines are good because in general (unless you're in a NOBR block,
CODE block, etc ) the browser won't display it, but it seems to trigger
the browser to do something.

  Issac

Foo Ji-Haw wrote:

> Hello Rodger,
>
> Thanks for the advice. I'm concerned that this sounds like a lot of
> search-and-replace for my application. I wonder if there is a cleaner
> method that simply toggles off buffering?
>
> Anyone has any ideas on this?
>
> Rodger Castle wrote:
>
>>> If I am not mistaken, modperl tends to cache all output until the
>>> script is completed, then it sends out the page. If I want to (for
>>> example) print a period (.) back to the browser every second, what
>>> do I need to do? I tried $| but it does not work.
>>>   
>>
>>
>> I fought with this for a while, too. You can use the 'bucket brigade'
>> technique. I'm a bit green, so someone with more experience could
>> expound more.  Here's a snippet:
>>
>> sub send_response_body
>> {
>>    my ( $r, $data ) = @_;
>>       my $bb = APR::Brigade->new ( $r->pool,
>>                  $r->connection->bucket_alloc );
>>    my $b = APR::Bucket->new ( $data );
>>    $bb->insert_tail ($b);
>>    $r->output_filters->fflush ( $bb );
>>    $bb->destroy;
>> }
>>
>> Hope this helps.
>>
>> Rodger
>>
>>  
>>

Re: [MP2] How to turn off caching?

Posted by Foo Ji-Haw <jh...@nexlabs.com>.
Hello Rodger,

Thanks for the advice. I'm concerned that this sounds like a lot of 
search-and-replace for my application. I wonder if there is a cleaner 
method that simply toggles off buffering?

Anyone has any ideas on this?

Rodger Castle wrote:

>>If I am not mistaken, modperl tends to cache all output until the script 
>>is completed, then it sends out the page. If I want to (for example) 
>>print a period (.) back to the browser every second, what do I need to 
>>do? I tried $| but it does not work.
>>    
>>
>
>I fought with this for a while, too. You can use the 'bucket brigade' technique. I'm a bit green, so someone with more experience could expound more.  Here's a snippet:
>
>sub send_response_body
>{
>    my ( $r, $data ) = @_;
>    
>    my $bb = APR::Brigade->new ( $r->pool,
>				 $r->connection->bucket_alloc );
>    my $b = APR::Bucket->new ( $data );
>    $bb->insert_tail ($b);
>    $r->output_filters->fflush ( $bb );
>    $bb->destroy;
>}
>
>Hope this helps.
>
>Rodger
>
>  
>

Re: [MP2] How to turn off caching?

Posted by Rodger Castle <ro...@theartofbooks.com>.
> If I am not mistaken, modperl tends to cache all output until the script 
> is completed, then it sends out the page. If I want to (for example) 
> print a period (.) back to the browser every second, what do I need to 
> do? I tried $| but it does not work.

I fought with this for a while, too. You can use the 'bucket brigade' technique. I'm a bit green, so someone with more experience could expound more.  Here's a snippet:

sub send_response_body
{
    my ( $r, $data ) = @_;
    
    my $bb = APR::Brigade->new ( $r->pool,
				 $r->connection->bucket_alloc );
    my $b = APR::Bucket->new ( $data );
    $bb->insert_tail ($b);
    $r->output_filters->fflush ( $bb );
    $bb->destroy;
}

Hope this helps.

Rodger

[MP2] How to turn off caching?

Posted by Foo Ji-Haw <jh...@nexlabs.com>.
Hi guys,

If I am not mistaken, modperl tends to cache all output until the script 
is completed, then it sends out the page. If I want to (for example) 
print a period (.) back to the browser every second, what do I need to 
do? I tried $| but it does not work.

Thanks in advance for your advice.

Re: a mystery.. need help

Posted by Luinrandir Insight <Lu...@insight.rr.com>.
ok.. thanks
and since thats not me.. i'm gone.
Lou

----- Original Message ----- 
From: "jonathan vanasco" <jv...@mastersofbranding.com>
To: "Luinrandir Insight" <Lu...@insight.rr.com>
Cc: <mo...@perl.apache.org>
Sent: Monday, May 30, 2005 9:23 AM
Subject: Re: a mystery.. need help


> This list is just about mod_perl -- the apache module that integrates a 
> perl interpreter into apache
> 
> To learn about apache modules, look on a website like perlmonks.com
> 
> 
> On May 29, 2005, at 10:10 PM, Luinrandir Insight wrote:
> 
> > OK.. first .. if I have the wrong list.. if this list is just about 
> > moduals
> > and apache please forgive.
> > I thought this list was about moduals, which I am just learning about. 
> > This
> > is a subroutine that is going to go into a package/modual once I get it
> > working.
> 
> 


Re: a mystery.. need help

Posted by jonathan vanasco <jv...@mastersofbranding.com>.
This list is just about mod_perl -- the apache module that integrates a 
perl interpreter into apache

To learn about apache modules, look on a website like perlmonks.com


On May 29, 2005, at 10:10 PM, Luinrandir Insight wrote:

> OK.. first .. if I have the wrong list.. if this list is just about 
> moduals
> and apache please forgive.
> I thought this list was about moduals, which I am just learning about. 
> This
> is a subroutine that is going to go into a package/modual once I get it
> working.


Re: a mystery.. need help

Posted by Tom Schindl <to...@gmx.at>.
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

1. To parse Parameters you should use:
~   a) CGI.pm
~      -----------------------8<-----------------------
~      my $q = new CGI();
~      my %form;
~      foreach( $q->param() ) {
~          $form{$_} = $q->param($_);
~      }
~      -----------------------8<-----------------------
~   b) Apache::Request or Apache2::Request => faster but only available
~      in mod-perl
2. The parameters in your second URL does not make any sense I think
they should be from a multi-selection box at least the braces '(' are
not supported in uris are escaped normally. But CGI can handle those
params fairly easily.
~      ----------------------8<----------------------
~      my @data = $q->param("Data");
~      ----------------------8<----------------------

3. I'd suggest you get:
~   a) a Perl Beginners book
~   b) a mod-perl book

Tom

Luinrandir Insight schrieb:
| OK.. first .. if I have the wrong list.. if this list is just about
moduals
| and apache please forgive.
| I thought this list was about moduals, which I am just learning about.
This
| is a subroutine that is going to go into a package/modual once I get it
| working.
| ALSO... perl is my HOBBY.... my background is GWbasic and pascal from 20
| years ago.
| I do not have a degree... so be nice to me.
|
| This piece of code is suppossed to take the Data from the Query string...
| $Data = $ENV{QUERY_STRING};
| and assign the value to the the vars.
| I am trying to move away from the commented out stuff below to something
| more strealine.
| I want to change my QUERY_STRING from:
| ca.cgi?PlayerName=Luinrandir&TownName=Avalon&Location=Castle&Action=Move
| to
| ca.cgi?Data(2)=Luinrandir&Data(3)=Avalon&Data(5)=Castle
|
| and this is the sub (to be modual) that is going to handle the data.
|
| sub ParseInput  # from previous page
| {
|     @pairs = split ( /&/, $Data );
|     foreach $pair(@pairs)
|     {
|         ( $name, $value ) = split ( /=/, $pair );
|   #changes plus sign to space
|         $name =~ tr/+/ /;
|         $name =~ s/%([a-fA-F0-9][a-fA-F0-9])/pack("C", hex($1))/eg;
|         #$name =~ tr/\0//d;
|         $value =~ tr/+/ /;
|         $value =~ s/%([a-fA-F0-9][a-fA-F0-9])/pack("C", hex($1))/eg;
|         #$value =~ tr/\0//d;
|          $form{$name} = $value;
|     }
|
| ### new code???#####
|  foreach $v0(0 .. 150)
|  {
|         if ($Data[$v0] eq "")
|         {
|             $Data[$v0] = $form{Data($v0)}; <<<<<<< this line does not work
|         }
|  }
|
| #### old code #####
| #        $Data[2]    = $form{PlayerName};
| #        $Data[3]    = $form{TownName};
| #        $Data[5]    = $form{Location};
|
| }
|
|
|

-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.0 (GNU/Linux)
Comment: Using GnuPG with Thunderbird - http://enigmail.mozdev.org

iD8DBQFCmsbakVPeOFLgZFIRAkJHAJwLv147CABrL5Kuuv2BEgkua0dGhwCguW6P
oE1TuXL/TQvw3S+vzdb9COQ=
=2IPy
-----END PGP SIGNATURE-----

Re: a mystery.. need help

Posted by Luinrandir Insight <Lu...@insight.rr.com>.
OK.. first .. if I have the wrong list.. if this list is just about moduals
and apache please forgive.
I thought this list was about moduals, which I am just learning about. This
is a subroutine that is going to go into a package/modual once I get it
working.
ALSO... perl is my HOBBY.... my background is GWbasic and pascal from 20
years ago.
I do not have a degree... so be nice to me.

This piece of code is suppossed to take the Data from the Query string...
$Data = $ENV{QUERY_STRING};
and assign the value to the the vars.
I am trying to move away from the commented out stuff below to something
more strealine.
I want to change my QUERY_STRING from:
ca.cgi?PlayerName=Luinrandir&TownName=Avalon&Location=Castle&Action=Move
to
ca.cgi?Data(2)=Luinrandir&Data(3)=Avalon&Data(5)=Castle

and this is the sub (to be modual) that is going to handle the data.

sub ParseInput  # from previous page
{
    @pairs = split ( /&/, $Data );
    foreach $pair(@pairs)
    {
        ( $name, $value ) = split ( /=/, $pair );
  #changes plus sign to space
        $name =~ tr/+/ /;
        $name =~ s/%([a-fA-F0-9][a-fA-F0-9])/pack("C", hex($1))/eg;
        #$name =~ tr/\0//d;
        $value =~ tr/+/ /;
        $value =~ s/%([a-fA-F0-9][a-fA-F0-9])/pack("C", hex($1))/eg;
        #$value =~ tr/\0//d;
         $form{$name} = $value;
    }

### new code???#####
 foreach $v0(0 .. 150)
 {
        if ($Data[$v0] eq "")
        {
            $Data[$v0] = $form{Data($v0)}; <<<<<<< this line does not work
        }
 }

#### old code #####
#        $Data[2]    = $form{PlayerName};
#        $Data[3]    = $form{TownName};
#        $Data[5]    = $form{Location};

}



Re: a mystery.. need help

Posted by David Dick <da...@iprimus.com.au>.
what is it supposed to do?

Luinrandir Insight wrote:
> Hallo group.. any idea why this line is bad?
> Lou
> 
> 
>  foreach $v0(0 .. 150)
>  {
>         if ($Data[$v0] ne "")
>         {
>             $Data[$v0] = $form{Data($v0)}; <<<<<<<< this line is bad! ! ! !
> !
>         }
>         print qq|Data($v0) = $Data[$v0]<BR>|;
>  }
> }
> 
> 
> 

a mystery.. need help

Posted by Luinrandir Insight <Lu...@insight.rr.com>.
Hallo group.. any idea why this line is bad?
Lou


 foreach $v0(0 .. 150)
 {
        if ($Data[$v0] ne "")
        {
            $Data[$v0] = $form{Data($v0)}; <<<<<<<< this line is bad! ! ! !
!
        }
        print qq|Data($v0) = $Data[$v0]<BR>|;
 }
}



Re: Should I use Package or Modual or what?

Posted by Perrin Harkins <pe...@elem.com>.
[ Please keep your replies on the list... ]

On Wednesday 25 May 2005 4:51 pm, Luinrandir Insight wrote:
> what about executing a sepate .pl ?
> to save memory or processer time...

Are you asking if you should split things up into separate CGI files?  
Probably.  It will not affect memory or performance though.  It's just easier 
to maintain.

If you're asking about putting subs into a Perl4-style .pl file that you 
require from your CGI, don't do it.  Make modules instead.

> so far the program is over 10k lines of code

That's huge.  You probably should be taking more advantage of CPAN modules 
instead of writing so much code.

- Perrin