[11371] in Perl-Users-Digest

home help back first fref pref prev next nref lref last post

Perl-Users Digest, Issue: 4971 Volume: 8

daemon@ATHENA.MIT.EDU (Perl-Users Digest)
Fri Feb 26 11:27:33 1999

Date: Fri, 26 Feb 99 08:25:15 -0800
From: Perl-Users Digest <Perl-Users-Request@ruby.OCE.ORST.EDU>
To: Perl-Users@ruby.OCE.ORST.EDU (Perl-Users Digest)

Perl-Users Digest           Fri, 26 Feb 1999     Volume: 8 Number: 4971

Today's topics:
        FAQ 6.20: Are Perl regexps DFAs or NFAs?  Are they POSI <perlfaq-suggestions@perl.com>
        FAQ 6.2: I'm having trouble matching over more than one <perlfaq-suggestions@perl.com>
        FAQ 6.3: How can I pull out lines between two patterns  <perlfaq-suggestions@perl.com>
        FAQ 6.4: I put a regular expression into $/ but it didn <perlfaq-suggestions@perl.com>
        FAQ 6.5: How do I substitute case insensitively on the  <perlfaq-suggestions@perl.com>
        FAQ 6.6: How can I make C<\w> match national character  <perlfaq-suggestions@perl.com>
        FAQ 6.7: How can I match a locale-smart version of C</[ <perlfaq-suggestions@perl.com>
        FAQ 6.8: How can I quote a variable to use in a regexp? <perlfaq-suggestions@perl.com>
        FAQ 6.9: What is C</o> really for?   <perlfaq-suggestions@perl.com>
    Re: FAQ 8.45: How do I install a CPAN module? droby@copyright.com
    Re: FAQ 8.45: How do I install a CPAN module? (Bart Lateur)
        FAQ 9.10: How do I redirect to another page?   <perlfaq-suggestions@perl.com>
        FAQ 9.11: How do I put a password on my web pages?   <perlfaq-suggestions@perl.com>
        FAQ 9.12: How do I edit my .htpasswd and .htgroup files <perlfaq-suggestions@perl.com>
        FAQ 9.13: How do I make sure users can't enter values i <perlfaq-suggestions@perl.com>
        FAQ 9.14: How do I parse a mail header?   <perlfaq-suggestions@perl.com>
        FAQ 9.15: How do I decode a CGI form?   <perlfaq-suggestions@perl.com>
        FAQ 9.16: How do I check a valid mail address?   <perlfaq-suggestions@perl.com>
        FAQ 9.17: How do I decode a MIME/BASE64 string?   <perlfaq-suggestions@perl.com>
        FAQ 9.18: How do I return the user's mail address?   <perlfaq-suggestions@perl.com>
        FAQ 9.19: How do I send mail?   <perlfaq-suggestions@perl.com>
    Re: FAQ 9.19: How do I send mail? <M.Piff@shef.ac.uk>
    Re: FAQ 9.19: How do I send mail? <terjekv@ifi.uio.no>
        FAQ 9.1: My CGI script runs from the command line but n <perlfaq-suggestions@perl.com>
        Special: Digest Administrivia (Last modified: 12 Dec 98 (Perl-Users-Digest Admin)

----------------------------------------------------------------------

Date: 26 Feb 1999 06:28:50 -0700
From: Tom Christiansen <perlfaq-suggestions@perl.com>
Subject: FAQ 6.20: Are Perl regexps DFAs or NFAs?  Are they POSIX compliant?  
Message-Id: <36d6a192@csnews>

(This excerpt from perlfaq6 - Regexps 
    ($Revision: 1.25 $, $Date: 1999/01/08 04:50:47 $)
part of the standard set of documentation included with every 
valid Perl distribution, like the one on your system.
See also http://language.perl.com/newdocs/pod/perlfaq6.html
if your negligent system adminstrator has been remiss in his duties.)

  Are Perl regexps DFAs or NFAs?  Are they POSIX compliant?

    While it's true that Perl's regular expressions resemble the DFAs
    (deterministic finite automata) of the egrep(1) program, they are in
    fact implemented as NFAs (non-deterministic finite automata) to allow
    backtracking and backreferencing. And they aren't POSIX-style either,
    because those guarantee worst-case behavior for all cases. (It seems
    that some people prefer guarantees of consistency, even when what's
    guaranteed is slowness.) See the book "Mastering Regular Expressions"
    (from O'Reilly) by Jeffrey Friedl for all the details you could ever
    hope to know on these matters (a full citation appears in the perlfaq2
    manpage).

-- 
    "A witty saying proves nothing."
		    --Voltaire


------------------------------

Date: 24 Feb 1999 09:27:09 -0700
From: Tom Christiansen <perlfaq-suggestions@perl.com>
Subject: FAQ 6.2: I'm having trouble matching over more than one line.  What's wrong?  
Message-Id: <36d4285d@csnews>

(This excerpt from perlfaq6 - Regexps 
    ($Revision: 1.25 $, $Date: 1999/01/08 04:50:47 $)
part of the standard set of documentation included with every 
valid Perl distribution, like the one on your system.
See also http://language.perl.com/newdocs/pod/perlfaq6.html
if your negligent system adminstrator has been remiss in his duties.)

  I'm having trouble matching over more than one line.  What's wrong?

    Either you don't have more than one line in the string you're looking at
    (probably), or else you aren't using the correct modifier(s) on your
    pattern (possibly).

    There are many ways to get multiline data into a string. If you want it
    to happen automatically while reading input, you'll want to set $/
    (probably to '' for paragraphs or `undef' for the whole file) to allow
    you to read more than one line at a time.

    Read the perlre manpage to help you decide which of `/s' and `/m' (or
    both) you might want to use: `/s' allows dot to include newline, and
    `/m' allows caret and dollar to match next to a newline, not just at the
    end of the string. You do need to make sure that you've actually got a
    multiline string in there.

    For example, this program detects duplicate words, even when they span
    line breaks (but not paragraph ones). For this example, we don't need
    `/s' because we aren't using dot in a regular expression that we want to
    cross line boundaries. Neither do we need `/m' because we aren't wanting
    caret or dollar to match at any point inside the record next to
    newlines. But it's imperative that $/ be set to something other than the
    default, or else we won't actually ever have a multiline record read in.

        $/ = '';            # read in more whole paragraph, not just one line
        while ( <> ) {
            while ( /\b([\w'-]+)(\s+\1)+\b/gi ) {   # word starts alpha
                print "Duplicate $1 at paragraph $.\n";
            }
        }

    Here's code that finds sentences that begin with "From " (which would be
    mangled by many mailers):

        $/ = '';            # read in more whole paragraph, not just one line
        while ( <> ) {
            while ( /^From /gm ) { # /m makes ^ match next to \n
                print "leading from in paragraph $.\n";
            }
        }

    Here's code that finds everything between START and END in a paragraph:

        undef $/;           # read in whole file, not just one line or paragraph
        while ( <> ) {
            while ( /START(.*?)END/sm ) { # /s makes . cross line boundaries
                print "$1\n";
            }
        }

-- 
    "Yes, you can think of almost everything as a function, but this may upset
    your wife." --Larry Wall


------------------------------

Date: 24 Feb 1999 11:57:11 -0700
From: Tom Christiansen <perlfaq-suggestions@perl.com>
Subject: FAQ 6.3: How can I pull out lines between two patterns that are themselves on different lines?  
Message-Id: <36d44b87@csnews>

(This excerpt from perlfaq6 - Regexps 
    ($Revision: 1.25 $, $Date: 1999/01/08 04:50:47 $)
part of the standard set of documentation included with every 
valid Perl distribution, like the one on your system.
See also http://language.perl.com/newdocs/pod/perlfaq6.html
if your negligent system adminstrator has been remiss in his duties.)

  How can I pull out lines between two patterns that are themselves on different lines?

    You can use Perl's somewhat exotic `..' operator (documented in the
    perlop manpage):

        perl -ne 'print if /START/ .. /END/' file1 file2 ...

    If you wanted text and not lines, you would use

        perl -0777 -ne 'print "$1\n" while /START(.*?)END/gs' file1 file2 ...

    But if you want nested occurrences of `START' through `END', you'll run
    up against the problem described in the question in this section on
    matching balanced text.

    Here's another example of using `..':

        while (<>) {
            $in_header =   1  .. /^$/;
            $in_body   = /^$/ .. eof();
            # now choose between them
        } continue {
            reset if eof();         # fix $.
        } 

-- 
Unix is defined by whatever is running on Dennis Ritchie's machine.


------------------------------

Date: 24 Feb 1999 14:27:14 -0700
From: Tom Christiansen <perlfaq-suggestions@perl.com>
Subject: FAQ 6.4: I put a regular expression into $/ but it didn't work. What's wrong?  
Message-Id: <36d46eb2@csnews>

(This excerpt from perlfaq6 - Regexps 
    ($Revision: 1.25 $, $Date: 1999/01/08 04:50:47 $)
part of the standard set of documentation included with every 
valid Perl distribution, like the one on your system.
See also http://language.perl.com/newdocs/pod/perlfaq6.html
if your negligent system adminstrator has been remiss in his duties.)

  I put a regular expression into $/ but it didn't work. What's wrong?

    $/ must be a string, not a regular expression. Awk has to be better for
    something. :-)

    Actually, you could do this if you don't mind reading the whole file
    into memory:

        undef $/;
        @records = split /your_pattern/, <FH>;

    The Net::Telnet module (available from CPAN) has the capability to wait
    for a pattern in the input stream, or timeout if it doesn't appear
    within a certain time.

        ## Create a file with three lines.
        open FH, ">file";
        print FH "The first line\nThe second line\nThe third line\n";
        close FH;

        ## Get a read/write filehandle to it.
        $fh = new FileHandle "+<file";

        ## Attach it to a "stream" object.
        use Net::Telnet;
        $file = new Net::Telnet (-fhopen => $fh);

        ## Search for the second line and print out the third.
        $file->waitfor('/second line\n/');
        print $file->getline;

-- 
    I dunno, I dream in Perl sometimes...
                    --Larry Wall in  <8538@jpl-devvax.JPL.NASA.GOV>


------------------------------

Date: 24 Feb 1999 16:57:16 -0700
From: Tom Christiansen <perlfaq-suggestions@perl.com>
Subject: FAQ 6.5: How do I substitute case insensitively on the LHS, but preserving case on the RHS?  
Message-Id: <36d491dc@csnews>

(This excerpt from perlfaq6 - Regexps 
    ($Revision: 1.25 $, $Date: 1999/01/08 04:50:47 $)
part of the standard set of documentation included with every 
valid Perl distribution, like the one on your system.
See also http://language.perl.com/newdocs/pod/perlfaq6.html
if your negligent system adminstrator has been remiss in his duties.)

  How do I substitute case insensitively on the LHS, but preserving case on the RHS?

    It depends on what you mean by "preserving case". The following script
    makes the substitution have the same case, letter by letter, as the
    original. If the substitution has more characters than the string being
    substituted, the case of the last character is used for the rest of the
    substitution.

        # Original by Nathan Torkington, massaged by Jeffrey Friedl
        #
        sub preserve_case($$)
        {
            my ($old, $new) = @_;
            my ($state) = 0; # 0 = no change; 1 = lc; 2 = uc
            my ($i, $oldlen, $newlen, $c) = (0, length($old), length($new));
            my ($len) = $oldlen < $newlen ? $oldlen : $newlen;

            for ($i = 0; $i < $len; $i++) {
                if ($c = substr($old, $i, 1), $c =~ /[\W\d_]/) {
                    $state = 0;
                } elsif (lc $c eq $c) {
                    substr($new, $i, 1) = lc(substr($new, $i, 1));
                    $state = 1;
                } else {
                    substr($new, $i, 1) = uc(substr($new, $i, 1));
                    $state = 2;
                }
            }
            # finish up with any remaining new (for when new is longer than old)
            if ($newlen > $oldlen) {
                if ($state == 1) {
                    substr($new, $oldlen) = lc(substr($new, $oldlen));
                } elsif ($state == 2) {
                    substr($new, $oldlen) = uc(substr($new, $oldlen));
                }
            }
            return $new;
        }

        $a = "this is a TEsT case";
        $a =~ s/(test)/preserve_case($1, "success")/gie;
        print "$a\n";

    This prints:

        this is a SUcCESS case

-- 
    "In wildness is the preservation of the world."  
	--Henry David Thoreau


------------------------------

Date: 24 Feb 1999 19:27:22 -0700
From: Tom Christiansen <perlfaq-suggestions@perl.com>
Subject: FAQ 6.6: How can I make C<\w> match national character sets?  
Message-Id: <36d4b50a@csnews>

(This excerpt from perlfaq6 - Regexps 
    ($Revision: 1.25 $, $Date: 1999/01/08 04:50:47 $)
part of the standard set of documentation included with every 
valid Perl distribution, like the one on your system.
See also http://language.perl.com/newdocs/pod/perlfaq6.html
if your negligent system adminstrator has been remiss in his duties.)

  How can I make `\w' match national character sets?

    See the perllocale manpage.

-- 
There ain't nothin' in this world that's worth being a snot over.
	    --Larry Wall in <1992Aug19.041614.6963@netlabs.com>


------------------------------

Date: 24 Feb 1999 21:57:25 -0700
From: Tom Christiansen <perlfaq-suggestions@perl.com>
Subject: FAQ 6.7: How can I match a locale-smart version of C</[a-zA-Z]/>?  
Message-Id: <36d4d835@csnews>

(This excerpt from perlfaq6 - Regexps 
    ($Revision: 1.25 $, $Date: 1999/01/08 04:50:47 $)
part of the standard set of documentation included with every 
valid Perl distribution, like the one on your system.
See also http://language.perl.com/newdocs/pod/perlfaq6.html
if your negligent system adminstrator has been remiss in his duties.)

  How can I match a locale-smart version of `/[a-zA-Z]/'?

    One alphabetic character would be `/[^\W\d_]/', no matter what locale
    you're in. Non-alphabetics would be `/[\W\d_]/' (assuming you don't
    consider an underscore a letter).

-- 


------------------------------

Date: 25 Feb 1999 00:27:27 -0700
From: Tom Christiansen <perlfaq-suggestions@perl.com>
Subject: FAQ 6.8: How can I quote a variable to use in a regexp?  
Message-Id: <36d4fb5f@csnews>

(This excerpt from perlfaq6 - Regexps 
    ($Revision: 1.25 $, $Date: 1999/01/08 04:50:47 $)
part of the standard set of documentation included with every 
valid Perl distribution, like the one on your system.
See also http://language.perl.com/newdocs/pod/perlfaq6.html
if your negligent system adminstrator has been remiss in his duties.)

  How can I quote a variable to use in a regexp?

    The Perl parser will expand $variable and @variable references in
    regular expressions unless the delimiter is a single quote. Remember,
    too, that the right-hand side of a `s///' substitution is considered a
    double-quoted string (see the perlop manpage for more details). Remember
    also that any regexp special characters will be acted on unless you
    precede the substitution with \Q. Here's an example:

        $string = "to die?";
        $lhs = "die?";
        $rhs = "sleep no more";

        $string =~ s/\Q$lhs/$rhs/;
        # $string is now "to sleep no more"

    Without the \Q, the regexp would also spuriously match "di".

-- 
MSDOS isn't dead.  It just smells like it.


------------------------------

Date: 25 Feb 1999 02:57:29 -0700
From: Tom Christiansen <perlfaq-suggestions@perl.com>
Subject: FAQ 6.9: What is C</o> really for?  
Message-Id: <36d51e89@csnews>

(This excerpt from perlfaq6 - Regexps 
    ($Revision: 1.25 $, $Date: 1999/01/08 04:50:47 $)
part of the standard set of documentation included with every 
valid Perl distribution, like the one on your system.
See also http://language.perl.com/newdocs/pod/perlfaq6.html
if your negligent system adminstrator has been remiss in his duties.)

  What is `/o' really for?

    Using a variable in a regular expression match forces a re-evaluation
    (and perhaps recompilation) each time through. The `/o' modifier locks
    in the regexp the first time it's used. This always happens in a
    constant regular expression, and in fact, the pattern was compiled into
    the internal format at the same time your entire program was.

    Use of `/o' is irrelevant unless variable interpolation is used in the
    pattern, and if so, the regexp engine will neither know nor care whether
    the variables change after the pattern is evaluated the *very first*
    time.

    `/o' is often used to gain an extra measure of efficiency by not
    performing subsequent evaluations when you know it won't matter (because
    you know the variables won't change), or more rarely, when you don't
    want the regexp to notice if they do.

    For example, here's a "paragrep" program:

        $/ = '';  # paragraph mode
        $pat = shift;
        while (<>) {
            print if /$pat/o;
        }

-- 
    I won't mention any names, because I don't want to get sun4's into
    trouble...  :-)     --Larry Wall in <11333@jpl-devvax.JPL.NASA.GOV>


------------------------------

Date: Mon, 22 Feb 1999 13:53:09 GMT
From: droby@copyright.com
To: perlfaq-suggestions@perl.com (Tom and Gnat)
Subject: Re: FAQ 8.45: How do I install a CPAN module?
Message-Id: <7arng2$ak$1@nnrp1.dejanews.com>

[posted and mailed]

In article <36ceea11@csnews>,
  perlfaq-suggestions@perl.com (Tom and Gnat) wrote:
> (This excerpt from perlfaq8 - System Interaction
>     ($Revision: 1.36 $, $Date: 1999/01/08 05:36:34 $)
> part of the standard set of documentation included with every
> valid Perl distribution, like the one on your system.
> See also http://language.perl.com/newdocs/pod/perlfaq8.html
> if your negligent system adminstrator has been remiss in his duties.)
>
>   How do I install a CPAN module?
>
>     The easiest way is to have the CPAN module do it for you. This module
>     comes with perl version 5.004 and later.

I think the fact that we used the name CPAN for a module is getting us into
trouble here.  I of course know what this means, but a naive reader might
think you're suggesting he use the CPAN module he's try to install to install
itself.

Perhaps a rephrasing to disambiguate:

   How do I install a module from CPAN?

     The easiest way is to have a module also named CPAN do it for you. This
module comes with perl version 5.004 and later.

--
Don Roby

-----------== Posted via Deja News, The Discussion Network ==----------
http://www.dejanews.com/       Search, Read, Discuss, or Start Your Own    


------------------------------

Date: Mon, 22 Feb 1999 22:45:50 GMT
From: bart.lateur@skynet.be (Bart Lateur)
Subject: Re: FAQ 8.45: How do I install a CPAN module?
Message-Id: <36d2cd04.2129552@news.skynet.be>

'droby' wrote:

[TC wrote:]
>>     The easiest way is to have the CPAN module do it for you. This module
>>     comes with perl version 5.004 and later.
>
>I think the fact that we used the name CPAN for a module is getting us into
>trouble here.  I of course know what this means, but a naive reader might
>think you're suggesting he use the CPAN module he's try to install to install
>itself.

PErhaps the full name of the module, "CPAN.pm", ought to be used in
order to avoid the ambiguity.

	Bart.


------------------------------

Date: 23 Feb 1999 20:45:20 -0700
From: Tom Christiansen <perlfaq-suggestions@perl.com>
Subject: FAQ 9.10: How do I redirect to another page?  
Message-Id: <36d375d0@csnews>

(This excerpt from perlfaq9 - Networking 
    ($Revision: 1.24 $, $Date: 1999/01/08 05:39:48 $)
part of the standard set of documentation included with every 
valid Perl distribution, like the one on your system.
See also http://language.perl.com/newdocs/pod/perlfaq9.html
if your negligent system adminstrator has been remiss in his duties.)

  How do I redirect to another page?

    Instead of sending back a `Content-Type' as the headers of your reply,
    send back a `Location:' header. Officially this should be a `URI:'
    header, so the CGI.pm module (available from CPAN) sends back both:

        Location: http://www.domain.com/newpage
        URI: http://www.domain.com/newpage

    Note that relative URLs in these headers can cause strange effects
    because of "optimizations" that servers do.

        $url = "http://www.perl.com/CPAN/";
        print "Location: $url\n\n";
        exit;

    To be correct to the spec, each of those `"\n"' should really each be
    `"\015\012"', but unless you're stuck on MacOS, you probably won't
    notice.

-- 
When the dinosaurs are mating, climb a tree.  --Steve Johnson


------------------------------

Date: 23 Feb 1999 21:45:22 -0700
From: Tom Christiansen <perlfaq-suggestions@perl.com>
Subject: FAQ 9.11: How do I put a password on my web pages?  
Message-Id: <36d383e2@csnews>

(This excerpt from perlfaq9 - Networking 
    ($Revision: 1.24 $, $Date: 1999/01/08 05:39:48 $)
part of the standard set of documentation included with every 
valid Perl distribution, like the one on your system.
See also http://language.perl.com/newdocs/pod/perlfaq9.html
if your negligent system adminstrator has been remiss in his duties.)

  How do I put a password on my web pages?

    That depends. You'll need to read the documentation for your web server,
    or perhaps check some of the other FAQs referenced above.

-- 
When the dinosaurs are mating, climb a tree.  --Steve Johnson


------------------------------

Date: 23 Feb 1999 22:45:25 -0700
From: Tom Christiansen <perlfaq-suggestions@perl.com>
Subject: FAQ 9.12: How do I edit my .htpasswd and .htgroup files with Perl?  
Message-Id: <36d391f5@csnews>

(This excerpt from perlfaq9 - Networking 
    ($Revision: 1.24 $, $Date: 1999/01/08 05:39:48 $)
part of the standard set of documentation included with every 
valid Perl distribution, like the one on your system.
See also http://language.perl.com/newdocs/pod/perlfaq9.html
if your negligent system adminstrator has been remiss in his duties.)

  How do I edit my .htpasswd and .htgroup files with Perl?

    The HTTPD::UserAdmin and HTTPD::GroupAdmin modules provide a consistent
    OO interface to these files, regardless of how they're stored. Databases
    may be text, dbm, Berkley DB or any database with a DBI compatible
    driver. HTTPD::UserAdmin supports files used by the `Basic' and `Digest'
    authentication schemes. Here's an example:

        use HTTPD::UserAdmin ();
        HTTPD::UserAdmin
              ->new(DB => "/foo/.htpasswd")
              ->add($username => $password);

-- 
Data structures, not algorithms, are central to programming. --Rob Pike


------------------------------

Date: 23 Feb 1999 23:45:27 -0700
From: Tom Christiansen <perlfaq-suggestions@perl.com>
Subject: FAQ 9.13: How do I make sure users can't enter values into a form that cause my CGI script to do bad things?  
Message-Id: <36d3a007@csnews>

(This excerpt from perlfaq9 - Networking 
    ($Revision: 1.24 $, $Date: 1999/01/08 05:39:48 $)
part of the standard set of documentation included with every 
valid Perl distribution, like the one on your system.
See also http://language.perl.com/newdocs/pod/perlfaq9.html
if your negligent system adminstrator has been remiss in his duties.)

  How do I make sure users can't enter values into a form that cause my CGI script to do bad things?

    Read the CGI security FAQ, at http://www-genome.wi.mit.edu/WWW/faqs/www-
    security-faq.html, and the Perl/CGI FAQ at
    http://www.perl.com/CPAN/doc/FAQs/cgi/perl-cgi-faq.html.

    In brief: use tainting (see the perlsec manpage), which makes sure that
    data from outside your script (eg, CGI parameters) are never used in
    `eval' or `system' calls. In addition to tainting, never use the single-
    argument form of system() or exec(). Instead, supply the command and
    arguments as a list, which prevents shell globbing.

-- 
"Twisted cleverness is my only skill as a programmer." --Elizabeth Zwicky


------------------------------

Date: 24 Feb 1999 00:45:29 -0700
From: Tom Christiansen <perlfaq-suggestions@perl.com>
Subject: FAQ 9.14: How do I parse a mail header?  
Message-Id: <36d3ae19@csnews>

(This excerpt from perlfaq9 - Networking 
    ($Revision: 1.24 $, $Date: 1999/01/08 05:39:48 $)
part of the standard set of documentation included with every 
valid Perl distribution, like the one on your system.
See also http://language.perl.com/newdocs/pod/perlfaq9.html
if your negligent system adminstrator has been remiss in his duties.)

  How do I parse a mail header?

    For a quick-and-dirty solution, try this solution derived from page 222
    of the 2nd edition of "Programming Perl":

        $/ = '';
        $header = <MSG>;
        $header =~ s/\n\s+/ /g;      # merge continuation lines
        %head = ( UNIX_FROM_LINE, split /^([-\w]+):\s*/m, $header );

    That solution doesn't do well if, for example, you're trying to maintain
    all the Received lines. A more complete approach is to use the
    Mail::Header module from CPAN (part of the MailTools package).

-- 
Anyone can be taught to sculpt.
Michaelangleo would have had to have been taught how not to.
The same is true of great programmers


------------------------------

Date: 24 Feb 1999 01:45:31 -0700
From: Tom Christiansen <perlfaq-suggestions@perl.com>
Subject: FAQ 9.15: How do I decode a CGI form?  
Message-Id: <36d3bc2b@csnews>

(This excerpt from perlfaq9 - Networking 
    ($Revision: 1.24 $, $Date: 1999/01/08 05:39:48 $)
part of the standard set of documentation included with every 
valid Perl distribution, like the one on your system.
See also http://language.perl.com/newdocs/pod/perlfaq9.html
if your negligent system adminstrator has been remiss in his duties.)

  How do I decode a CGI form?

    You use a standard module, probably CGI.pm. Under no circumstances
    should you attempt to do so by hand!

    You'll see a lot of CGI programs that blindly read from STDIN the number
    of bytes equal to CONTENT_LENGTH for POSTs, or grab QUERY_STRING for
    decoding GETs. These programs are very poorly written. They only work
    sometimes. They typically forget to check the return value of the read()
    system call, which is a cardinal sin. They don't handle HEAD requests.
    They don't handle multipart forms used for file uploads. They don't deal
    with GET/POST combinations where query fields are in more than one
    place. They don't deal with keywords in the query string.

    In short, they're bad hacks. Resist them at all costs. Please do not be
    tempted to reinvent the wheel. Instead, use the CGI.pm or CGI_Lite.pm
    (available from CPAN), or if you're trapped in the module-free land of
    perl1 .. perl4, you might look into cgi-lib.pl (available from
    http://cgi-lib.stanford.edu/cgi-lib/ ).

    Make sure you know whether to use a GET or a POST in your form. GETs
    should only be used for something that doesn't update the server.
    Otherwise you can get mangled databases and repeated feedback mail
    messages. The fancy word for this is ``idempotency''. This simply means
    that there should be no difference between making a GET request for a
    particular URL once or multiple times. This is because the HTTP protocol
    definition says that a GET request may be cached by the browser, or
    server, or an intervening proxy. POST requests cannot be cached, because
    each request is independent and matters. Typically, POST requests change
    or depend on state on the server (query or update a database, send mail,
    or purchase a computer).

-- 
    Because . doesn't match \n.  [\0-\377] is the most efficient way to match
    everything currently.  Maybe \e should match everything.  And \E would
    of course match nothing.   :-) --Larry Wall in <9847@jpl-devvax.JPL.NASA.GOV>


------------------------------

Date: 24 Feb 1999 02:45:33 -0700
From: Tom Christiansen <perlfaq-suggestions@perl.com>
Subject: FAQ 9.16: How do I check a valid mail address?  
Message-Id: <36d3ca3d@csnews>

(This excerpt from perlfaq9 - Networking 
    ($Revision: 1.24 $, $Date: 1999/01/08 05:39:48 $)
part of the standard set of documentation included with every 
valid Perl distribution, like the one on your system.
See also http://language.perl.com/newdocs/pod/perlfaq9.html
if your negligent system adminstrator has been remiss in his duties.)

  How do I check a valid mail address?

    You can't, at least, not in real time. Bummer, eh?

    Without sending mail to the address and seeing whether there's a human
    on the other hand to answer you, you cannot determine whether a mail
    address is valid. Even if you apply the mail header standard, you can
    have problems, because there are deliverable addresses that aren't RFC-
    822 (the mail header standard) compliant, and addresses that aren't
    deliverable which are compliant.

    Many are tempted to try to eliminate many frequently-invalid mail
    addresses with a simple regexp, such as `/^[\w.-]+\@([\w.-]\.)+\w+$/'.
    It's a very bad idea. However, this also throws out many valid ones, and
    says nothing about potential deliverability, so is not suggested.
    Instead, see
    http://www.perl.com/CPAN/authors/Tom_Christiansen/scripts/ckaddr.gz ,
    which actually checks against the full RFC spec (except for nested
    comments), looks for addresses you may not wish to accept mail to (say,
    Bill Clinton or your postmaster), and then makes sure that the hostname
    given can be looked up in the DNS MX records. It's not fast, but it
    works for what it tries to do.

    Our best advice for verifying a person's mail address is to have them
    enter their address twice, just as you normally do to change a password.
    This usually weeds out typos. If both versions match, send mail to that
    address with a personal message that looks somewhat like:

        Dear someuser@host.com,

        Please confirm the mail address you gave us Wed May  6 09:38:41
        MDT 1998 by replying to this message.  Include the string
        "Rumpelstiltskin" in that reply, but spelled in reverse; that is,
        start with "Nik...".  Once this is done, your confirmed address will
        be entered into our records.

    If you get the message back and they've followed your directions, you
    can be reasonably assured that it's real.

    A related strategy that's less open to forgery is to give them a PIN
    (personal ID number). Record the address and PIN (best that it be a
    random one) for later processing. In the mail you send, ask them to
    include the PIN in their reply. But if it bounces, or the message is
    included via a ``vacation'' script, it'll be there anyway. So it's best
    to ask them to mail back a slight alteration of the PIN, such as with
    the characters reversed, one added or subtracted to each digit, etc.

-- 
MS-DOS is CP/M on steroids, bigger bulkier and not much better.
Windows is MS-DOS with a bad copy of a Macintosh GUI.
NT is a Windows riddled with VMS.  


------------------------------

Date: 24 Feb 1999 03:45:43 -0700
From: Tom Christiansen <perlfaq-suggestions@perl.com>
Subject: FAQ 9.17: How do I decode a MIME/BASE64 string?  
Message-Id: <36d3d857@csnews>

(This excerpt from perlfaq9 - Networking 
    ($Revision: 1.24 $, $Date: 1999/01/08 05:39:48 $)
part of the standard set of documentation included with every 
valid Perl distribution, like the one on your system.
See also http://language.perl.com/newdocs/pod/perlfaq9.html
if your negligent system adminstrator has been remiss in his duties.)

  How do I decode a MIME/BASE64 string?

    The MIME-tools package (available from CPAN) handles this and a lot
    more. Decoding BASE64 becomes as simple as:

        use MIME::base64;
        $decoded = decode_base64($encoded);

    A more direct approach is to use the unpack() function's "u" format
    after minor transliterations:

        tr#A-Za-z0-9+/##cd;                   # remove non-base64 chars
        tr#A-Za-z0-9+/# -_#;                  # convert to uuencoded format
        $len = pack("c", 32 + 0.75*length);   # compute length byte
        print unpack("u", $len . $_);         # uudecode and print

-- 
s = (unsigned char*)(SvPVX(sv));    /* deeper magic */
    --Larry Wall, from util.c in the v5.0 perl distribution


------------------------------

Date: 24 Feb 1999 04:45:55 -0700
From: Tom Christiansen <perlfaq-suggestions@perl.com>
Subject: FAQ 9.18: How do I return the user's mail address?  
Message-Id: <36d3e673@csnews>

(This excerpt from perlfaq9 - Networking 
    ($Revision: 1.24 $, $Date: 1999/01/08 05:39:48 $)
part of the standard set of documentation included with every 
valid Perl distribution, like the one on your system.
See also http://language.perl.com/newdocs/pod/perlfaq9.html
if your negligent system adminstrator has been remiss in his duties.)

  How do I return the user's mail address?

    On systems that support getpwuid, the $< variable and the Sys::Hostname
    module (which is part of the standard perl distribution), you can
    probably try using something like this:

        use Sys::Hostname;
        $address = sprintf('%s@%s', getpwuid($<), hostname);

    Company policies on mail address can mean that this generates addresses
    that the company's mail system will not accept, so you should ask for
    users' mail addresses when this matters. Furthermore, not all systems on
    which Perl runs are so forthcoming with this information as is Unix.

    The Mail::Util module from CPAN (part of the MailTools package) provides
    a mailaddress() function that tries to guess the mail address of the
    user. It makes a more intelligent guess than the code above, using
    information given when the module was installed, but it could still be
    incorrect. Again, the best way is often just to ask the user.

-- 
If you consistently take an antagonistic approach, however, people are
going to start thinking you're from New York.   :-)
        --Larry Wall to Dan Bernstein in <10187@jpl-devvax.JPL.NASA.GOV>


------------------------------

Date: 24 Feb 1999 05:46:14 -0700
From: Tom Christiansen <perlfaq-suggestions@perl.com>
Subject: FAQ 9.19: How do I send mail?  
Message-Id: <36d3f496@csnews>

(This excerpt from perlfaq9 - Networking 
    ($Revision: 1.24 $, $Date: 1999/01/08 05:39:48 $)
part of the standard set of documentation included with every 
valid Perl distribution, like the one on your system.
See also http://language.perl.com/newdocs/pod/perlfaq9.html
if your negligent system adminstrator has been remiss in his duties.)

  How do I send mail?

    Use the `sendmail' program directly:

        open(SENDMAIL, "|/usr/lib/sendmail -oi -t -odq")
                            or die "Can't fork for sendmail: $!\n";
        print SENDMAIL <<"EOF";
        From: User Originating Mail <me\@host>
        To: Final Destination <you\@otherhost>
        Subject: A relevant subject line

        Body of the message goes here after the blank line
        in as many lines as you like.
        EOF
        close(SENDMAIL)     or warn "sendmail didn't close nicely";

    The -oi option prevents sendmail from interpreting a line consisting of
    a single dot as "end of message". The -t option says to use the headers
    to decide who to send the message to, and -odq says to put the message
    into the queue. This last option means your message won't be immediately
    delivered, so leave it out if you want immediate delivery.

    Or use the CPAN module Mail::Mailer:

        use Mail::Mailer;

        $mailer = Mail::Mailer->new();
        $mailer->open({ From    => $from_address,
                        To      => $to_address,
                        Subject => $subject,
                      })
            or die "Can't open: $!\n";
        print $mailer $body;
        $mailer->close();

    The Mail::Internet module uses Net::SMTP which is less Unix-centric than
    Mail::Mailer, but less reliable. Avoid raw SMTP commands. There are many
    reasons to use a mail transport agent like sendmail. These include
    queueing, MX records, and security.

-- 
The Unix Way of doing something [...] is to make it look as much like a filter
as possible.  (Richard O'Keefe)        


------------------------------

Date: Thu, 25 Feb 1999 13:21:06 -0000
From: "Mike Piff" <M.Piff@shef.ac.uk>
Subject: Re: FAQ 9.19: How do I send mail?
Message-Id: <7b3iov$oqd$1@bignews.shef.ac.uk>


Tom Christiansen wrote in message <36d3f496@csnews>...
>(This excerpt from perlfaq9 - Networking
>    ($Revision: 1.24 $, $Date: 1999/01/08 05:39:48 $)
>part of the standard set of documentation included with every
>valid Perl distribution, like the one on your system.
>See also http://language.perl.com/newdocs/pod/perlfaq9.html
>if your negligent system adminstrator has been remiss in his duties.)
>
>  How do I send mail?
>
>    Use the `sendmail' program directly:


Ho! Ho!  One of the people who thinks everyone uses Unix.





------------------------------

Date: 25 Feb 1999 15:31:38 +0100
From: Terje Kvernes <terjekv@ifi.uio.no>
Subject: Re: FAQ 9.19: How do I send mail?
Message-Id: <wxxg17uo8qd.fsf@ljod.ifi.uio.no>

"Mike Piff" <M.Piff@shef.ac.uk> writes:

> Ho! Ho!  One of the people who thinks everyone uses Unix.

Well, most people sould. =)

Seriously though; I agree. A general socket-routine is _much_
preferred. But a lot of the windows-machines I've been running perl on
aren't exactly happy about sockets, and sendmail is, *ehrm* easy?

If you should happen to have a problem on the mail/socket thing say
"quibble" or something. ;)

BTDT.

-- 
Terje


------------------------------

Date: 23 Feb 1999 11:44:55 -0700
From: Tom Christiansen <perlfaq-suggestions@perl.com>
Subject: FAQ 9.1: My CGI script runs from the command line but not the browser.   (500 Server Error)  
Message-Id: <36d2f727@csnews>

(This excerpt from perlfaq9 - Networking 
    ($Revision: 1.24 $, $Date: 1999/01/08 05:39:48 $)
part of the standard set of documentation included with every 
valid Perl distribution, like the one on your system.
See also http://language.perl.com/newdocs/pod/perlfaq9.html
if your negligent system adminstrator has been remiss in his duties.)

  My CGI script runs from the command line but not the browser.   (500 Server Error)

    If you can demonstrate that you've read the following FAQs and that your
    problem isn't something simple that can be easily answered, you'll
    probably receive a courteous and useful reply to your question if you
    post it on comp.infosystems.www.authoring.cgi (if it's something to do
    with HTTP, HTML, or the CGI protocols). Questions that appear to be Perl
    questions but are really CGI ones that are posted to comp.lang.perl.misc
    may not be so well received.

    The useful FAQs and related documents are:

        CGI FAQ
            http://www.webthing.com/page.cgi/cgifaq

        Web FAQ
            http://www.boutell.com/faq/

        WWW Security FAQ
            http://www.w3.org/Security/Faq/

        HTTP Spec
            http://www.w3.org/pub/WWW/Protocols/HTTP/

        HTML Spec
            http://www.w3.org/TR/REC-html40/
            http://www.w3.org/pub/WWW/MarkUp/

        CGI Spec
            http://www.w3.org/CGI/

        CGI Security FAQ
            http://www.go2net.com/people/paulp/cgi-security/safe-cgi.txt

-- 
    What about WRITING it first and rationalizing it afterwords?  :-)
                    --Larry Wall in <8162@jpl-devvax.JPL.NASA.GOV>


------------------------------

Date: 12 Dec 98 21:33:47 GMT (Last modified)
From: Perl-Request@ruby.oce.orst.edu (Perl-Users-Digest Admin) 
Subject: Special: Digest Administrivia (Last modified: 12 Dec 98)
Message-Id: <null>


Administrivia:

Well, after 6 months, here's the answer to the quiz: what do we do about
comp.lang.perl.moderated. Answer: nothing. 

]From: Russ Allbery <rra@stanford.edu>
]Date: 21 Sep 1998 19:53:43 -0700
]Subject: comp.lang.perl.moderated available via e-mail
]
]It is possible to subscribe to comp.lang.perl.moderated as a mailing list.
]To do so, send mail to majordomo@eyrie.org with "subscribe clpm" in the
]body.  Majordomo will then send you instructions on how to confirm your
]subscription.  This is provided as a general service for those people who
]cannot receive the newsgroup for whatever reason or who just prefer to
]receive messages via e-mail.

The Perl-Users Digest is a retransmission of the USENET newsgroup
comp.lang.perl.misc.  For subscription or unsubscription requests, send
the single line:

	subscribe perl-users
or:
	unsubscribe perl-users

to almanac@ruby.oce.orst.edu.  

To submit articles to comp.lang.perl.misc (and this Digest), send your
article to perl-users@ruby.oce.orst.edu.

To submit articles to comp.lang.perl.announce, send your article to
clpa@perl.com.

To request back copies (available for a week or so), send your request
to almanac@ruby.oce.orst.edu with the command "send perl-users x.y",
where x is the volume number and y is the issue number.

The Meta-FAQ, an article containing information about the FAQ, is
available by requesting "send perl-users meta-faq". The real FAQ, as it
appeared last in the newsgroup, can be retrieved with the request "send
perl-users FAQ". Due to their sizes, neither the Meta-FAQ nor the FAQ
are included in the digest.

The "mini-FAQ", which is an updated version of the Meta-FAQ, is
available by requesting "send perl-users mini-faq". It appears twice
weekly in the group, but is not distributed in the digest.

For other requests pertaining to the digest, send mail to
perl-users-request@ruby.oce.orst.edu. Do not waste your time or mine
sending perl questions to the -request address, I don't have time to
answer them even if I did know the answer.


------------------------------
End of Perl-Users Digest V8 Issue 4971
**************************************

home help back first fref pref prev next nref lref last post