PurduePerlMongers RecentChanges Random Page Categories Meetings

QuestionsAndAnswers

Please ask your questions here.
  1. Who can ask questions here?
  2. Who can answer questions here?
  3. How do I put source code verbatim in this wiki?
  4. What's the proper way of handling passwords for websites?
  5. How to count the number of occurrences of something in a string?
  6. How do you do proper fork coding?
  7. What is a Schwartzian Transform? What does it do? How does it work?
  8. How can I make a link to another web document with the text of the link in italics. ---MarkSenn
  9. This is a DBI question. I'm working on weblog software with a MySQL backend, with an index number to specify specific entries. I figure that a user's number of individual entries could get abstractly large, so, is it better to do an SQL statement like this:

Question: Who can ask questions here?

Answer: Anyone. You don't even have to give a name, although we'd like it if you do. Just place it at the bottom of this list.

Question: Who can answer questions here?

Answer: Anyone. Again, you don't have to give your name.

Question: How do I put source code verbatim in this wiki?

Answer: Standard markup is to preceed the line with a space, like this: -- DaveJacoby

 $foo = $bar + $blee ; #example

Question: What's the proper way of handling passwords for websites?

OK, so you have Alice. Alice is a good subgenius and wants to log onto the Bob.com web page (using the convention, but the real Bob.com seems like a real Saint of Sales!). She puts her userid and her password, which pass to the server.

So, Mallory could sniff the network and grab the password that way, because the general machine on the network serves HTTP, not HTTPS, which sucks to the depth and the breadth and the height my soul can reach. And only marginally better, you can't really hash it in javascript and send it that way. Perl's Digest::MD5 module is compiled in C because speed-wise, it'd suck otherwise, and I can just imagine how badly a Javascript implementation of MD5 would suck.

OK, clearly, you store the password in hashed form only, and clearly you start thinking about switching to something less theoreticallyy pwned than SHA1 or MD5. So, the best solution I've seen is to store the userid and a long random cookie, and always forcing a password on significant uses, like posting or whatever. This makes sense to me. The threat from Mallory is lessened but still there, because the password is still being passed in the clear. And so, the depth and the breadth and the height....

And yes, this is more web standards questioning than Perl questioning, but I do intend to code this thing in Perl. --DaveJacoby

Question: How to count the number of occurrences of something in a string?

Answer: The program below uses the same basic idea to show how to solve the problem four different ways in Perl 5. This program

 # revised regex-count.pl  2006-09-14  18:21  Mark Senn  http://www.ecn.purdue.edu/~mark
 # created regex-count.pl  2006-09-13  12:00  Mark Senn  http://www.ecn.purdue.edu/~mark
 #
 # Perl 5: Count occurrences of regular expression in a string.
 #
 # Question: How to count the number of occurrences of something in a string?
 # Answer: The program below uses the same basic idea to show how to solve the problem
 # four different ways in Perl 5.
 #
 
 $string = 'x a x xax';
 
 # (1)
 # Count the number of x's.
 # Put them in () and evaluate the array in scalar context.
 # Evaluating the array in scalar context returns the length of the array.
 $_ = $string;
 $count = () = /(x)/g;
 print qq/(1)   $count   "$_"\n/;
 
 # (2)
 # Same as (1) but don't use a $count temporary variables.
 print qq/(2)   /, scalar(() = /(x)/g), qq/   "$_"\n/;
 
 # (3)
 # Same as (1) but do it on $a instead of $_.
 # "=~" binds tighter than "=".
 $a = $string;
 $count = () = $a =~ /(x)/g;
 print qq/(3)   $count   "$a"\n/;
 
 # (4)
 # Same as (2) but do it on $a instead of $_.
 # "=~" binds tighter than "=".
 print qq/(4)   /, scalar(() = $a =~ /(x)/g), qq/   "$a"\n/;
 
 # The method at
 #     http://www.perl.com/doc/FAQs/FAQ/oldfaq-html/Q4.24.html
 # may be faster.  I don't like it as well because of
 # the "while" loop though.  Don't forget to set the $count
 # variable to zero before starting the loop each time.

prints

 (1)   4   "x a x xax"
 (2)   4   "x a x xax"
 (3)   4   "x a x xax"
 (4)   4   "x a x xax"

-MarkSenn

Answer: I played around a little bit and found that this works.

 my $text = 'abracadabra' ;
 print length join "" , $text =~ m{a|b}gmx ;

My first instinct is to go for an s{a}{b} kind of thing:

 $text =~ s{a|b}{$i++}ge ;
 print $i ;

-- DaveJacoby

Question: How do you do proper fork coding?

I'm working on my spy code, and one of the things I'm doing is calling an outside program to do certain things, like send email and harvest links off mp3 blogs, and I decided that having the program fork that stuff would be good, so that long-running processes can run while the later spies spy. But because I haven't really done forking for years and then in a client-server context, My fork code, from lines 146-153 of my Spy example code:

  if ( $action ) {
    my $kid = fork ;
    if ( !defined $kid ){ print_log qq(fork fail: $!\n) ; }
    if ( $kid ) {
        print_log qq(THIS IS THE FORK) ;
        print_log join " " , qx(pwd) ,  $action , $target , $url ;
        print_log qx( .spy_tools/$action  -t "$target" -u "$url" ) ;
        print_log qq(THIS IS THE FORK) ;
        kill(TERM => $kid ) ;
        }
      }

I don't want to be a necromancer. Any pointers toward better fork coding? -- DaveJacoby

Answer: I finally grepped standard libraries for 'fork', assuming that if a library uses fork, it would use it correctly. This is from CPAN:CPAN and yes, I get the irony.

  local $SIG{CHLD}; # = sub { wait };
  if (defined($pid = fork)) {
    if ($pid) { #parent
      # wait;
      waitpid $pid, 0;
    } else {    #child
      # note, this exec isn't necessary if
      # inactivity_timeout is 0. On the Mac I'd
      # suggest, we set it always to 0.
      exec $system;
    }

The errors I see include putting the activity I wanted forked in the parent part, not the child part, I wouldn't doubt there's more. -- DaveJacoby

Question: What is a Schwartzian Transform? What does it do? How does it work?

Answer: See [http://en.wikipedia.org/wiki/Schwartzian_transform Schwartzian Transform] ---MarkSenn

Answer: You know, you can just do WikiPedia:Schwartzian_Transform to point to Wikipedia. That's what an InterMap is for. DaveJacoby

Question: How can I make a link to another web document with the text of the link in italics. ---MarkSenn

Answer: Making a link to another web document works like this:

     [http://slashdot.org/ SlashDot]

Adding a pair of single quotes around something makes it italic, like this:

     ''italics''

So putting them around the square brackets of a link looks like this:

     ''[http://slashdot.org/ SlashDot]''

And works like this:

SlashDot

Question: This is a DBI question. I'm working on weblog software with a MySQL backend, with an index number to specify specific entries. I figure that a user's number of individual entries could get abstractly large, so, is it better to do an SQL statement like this:

      SELECT rant_id FROM rants ;

and play around with the big array you feed it into to get the specific rant_ids you want, or should you do this:

      SELECT rant_id FROM rants LIMIT 20,10 ;

or the like to specify and get a smaller array? --DaveJacoby