Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl: the Markov chain saw
 
PerlMonks  

vladb's scratchpad

by vladb (Vicar)
on Jun 05, 2004 at 12:29 UTC ( #361545=scratchpad: print w/ replies, xml ) Need Help??

# minimum sentence size, to elimnate fluff $min_size=10; # grab and print first 4 sentences only (ending with '.') $article="'Sentence 1. This is sent. 2. This is sentence 3. What? The +cost is $1.23, on sale. What do you think? This is the last s\ entence."; ($intro)=($article=~/((?:[^\.]{$min_size,}\.){4})/si); print "$intro\n";
Will print first 4 sentences:
im the kink. but you are not the king. we are the kings. yes we are th +e kings.

Database dillema

basically, i'm having to deal with a case where a user is presented with a set of options to select from for example, a field named 'Property ammenities' will have values such as
  • school
  • park
  • other


If client selects option 'other', he has to enter his own custom 'amenity' info. Client is allowed to enter as many custom 'other' amenities as he/she wishes.

Now, the question is, what would be the best way of storing this data in the database? Previously, I'd have an amenity_types table listing all pre-defined amenities. The table would have two fields, Id and Info. Then, I'll have another table named 'user_amenity'. Which will have two fields, user_id (referencing id in the user table) and amenity_id (referencing an amenity record in the amenity_types table). However, how do I extend this design to include an option for custom user defined amenities? Adding an extra VARCHAR column to the user_amenity table doesn't sound wise to me. So, should I just add another table... Something like 'user_amenity_other' with only thse fields: user_id and Info. Notice how amenity_id has been replaced with Info. However, having two tables seems like an overkill. What if I have 5 or 6 such similar fields that a user can set from a predefined list as well as provide his custom answer? What would be the best approach?




The end of times:
$x = 0; $s = "HOLY BIBLE"; $s =~ s/(\w)/{$x+=($o=ord($1));print"$1 - $o\n";''}/eg; print "Total: $x\n";
Output:
H - 72 O - 79 L - 76 Y - 89 B - 66 I - 73 B - 66 L - 76 E - 69 Total: 666


My japh:

open(I,$0);<I>;$~=$/;$/='~';$_=<I>;($/)=/(.)$~/;s/[$~~]//g;/(v)/;$-=$-[0];s;\Q$/\E;$~;g;($/,$^)=/^(.)(.)/;
#%  xxxxxx   xx-+  xx    xxx xx  xx       xx  xx   xxx   xxxxx+ xx  xx xxxx xxxxx  ......+
#x xxxxvxxx xx  xx xv   xxxx x+ %+  ===== xx  xx  xx xx  x+  =x xx xx  xx   xx xx ...+
#x xx xx xx xx  xx xx  xx xx xxx+         xxxxxx xx   +x xx     xx+x-  xxxx xxxx ........+
#% xx xx xx xx  xx xx xx  x+ xx xx  =+=== xx  xx xxxx-xx xx  =x +x xx  xx   xx xx ...+

#% xx xx xx  -+x+  xxx+   xx xx  xx       xx  xx x+   xx xxx+xx xx  xx xxxx xx  xx ....+~
for(split/$~/){s,[ $/],,g;/(.)$/;$l=$-[0];/(.)/||next;$_=chr$-+$l;$".=($1=~/$^/)?"\u$_":$_;}print$";
---------------------------------

This script that I wrote here is to test the possibility of forking a child process inside CGI to handle file uploads. I was exploring a possibility of splitting the actual work to be done to write the file to the server and render html page (displaying status etc). I have also done some rough benchmarking. You can take your own measurements by studying the contents of the report.out file where I dump start time, run time for the parent process, and run time for the child process. In all cases, child process took longer to complete. However, i'm still left to test large file uploads... i'm afraid fork won't help here as the client still has to sit and wait till the browser has finished sending all of the file (it works like a pipe.. )

Actually, I have now figured that as long as I use the CGI module as is, my script will _always_ waaaait whenever the client sends me a file. There's a subroutine inside CGI which automatically reads from the pipe and into a temporary file. This is exactly the place where I think the fork should have happened. Certainly, CGI was in no business to do that. But what _I_ could do instead is override that method with my own that will do the forking and reading from the pipe. And _then_ things _should_ work fine. ;-)
#!/usr/local/bin/perl use strict; use Time::HiRes qw( time ); use CGI; ################### MAIN ###################### { my $time_start = time; my $cgi = new CGI; print $cgi->header(); print $cgi->start_html(); open(REPORT, ">./report.out"); my $file = $cgi->upload("upload_file"); if (!$file && $cgi->cgi_error) { print $cgi->header(-status=>$cgi->cgi_error); exit 0; } elsif ($file) { use Data::Dumper; my $upload_info = $cgi->uploadInfo($file); print Dumper($upload_info); if (!fork()) { # do this stuff in child process... # get rid of STDOUT so that CGI won't # have to hold on to this (child) process until # it's done. open(STDOUT, ">/dev/null"); my ($local_file) = ($file =~ m|[\\/]+([^\\/]+)$|); open (OUTFILE,">./$local_file"); my ($buffer, $bytesread); while (<$file>) { print OUTFILE $_; } close (OUTFILE); my $time_finish = time; my $child_time = $time_finish - $time_start; print REPORT "Child time: $child_time\n"; # note: this will also flush the buffer, # which may any lines printed to this handle # before the fork() to duplicate! close (REPORT); exit; } print "File '$file' uploaded successfully!<br><br>Saved in '$f +ile'. "; } print $cgi->start_multipart_form(); print $cgi->filefield(-name=>'upload_file', -default=>'starting value', -size=>50, -maxlength=>80); print $cgi->submit(-value => 'Submit!'); print $cgi->end_form; my $time_finish = time; my $time_parent = $time_finish - $time_start; print REPORT "Time Start: $time_start\n"; print REPORT "Parent time: $time_parent\n"; # I may end up closing this before even the child process # has finished writing to it? close REPORT; exit; }

Improving access to "Writeup Formatting Tips"
Log In?
Username:
Password:

What's my password?
Create A New User
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others wandering the Monastery: (6)
As of 2014-08-28 03:19 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    The best computer themed movie is:











    Results (255 votes), past polls