{
    "content": [
        {
            "type": "text",
            "text": "# perlfaq9 (man)\n\n## NAME\n\nperlfaq9 - Web, Email and Networking\n\n## DESCRIPTION\n\nThis section deals with questions related to running web sites, sending and receiving email\nas well as general networking.\n\n## Sections\n\n- **NAME**\n- **VERSION**\n- **DESCRIPTION** (22 subsections)\n- **AUTHOR AND COPYRIGHT**\n\nUse structuredContent.sections for detailed options, examples, and full documentation.\n"
        }
    ],
    "structuredContent": {
        "command": "perlfaq9",
        "section": "",
        "mode": "man",
        "summary": "perlfaq9 - Web, Email and Networking",
        "synopsis": null,
        "tldr_summary": null,
        "tldr_examples": [],
        "tldr_source": null,
        "flags": [],
        "examples": [],
        "see_also": [],
        "section_outline": [
            {
                "name": "NAME",
                "lines": 2,
                "subsections": []
            },
            {
                "name": "VERSION",
                "lines": 2,
                "subsections": []
            },
            {
                "name": "DESCRIPTION",
                "lines": 3,
                "subsections": [
                    {
                        "name": "Should I use a web framework?",
                        "lines": 7
                    },
                    {
                        "name": "Which web framework should I use?",
                        "lines": 31
                    },
                    {
                        "name": "What is Plack and PSGI?",
                        "lines": 12
                    },
                    {
                        "name": "How do I remove HTML from a string?",
                        "lines": 3
                    },
                    {
                        "name": "How do I extract URLs?",
                        "lines": 7
                    },
                    {
                        "name": "How do I fetch an HTML file?",
                        "lines": 19
                    },
                    {
                        "name": "How do I automate an HTML form submission?",
                        "lines": 25
                    },
                    {
                        "name": "How do I decode or create those %-encodings on the web?",
                        "lines": 21
                    },
                    {
                        "name": "How do I redirect to another page?",
                        "lines": 9
                    },
                    {
                        "name": "How do I put a password on my web pages?",
                        "lines": 6
                    },
                    {
                        "name": "How  do  I  make  sure  users  can't enter values into a form that causes my CGI script to do bad",
                        "lines": 1
                    },
                    {
                        "name": "things?",
                        "lines": 14
                    },
                    {
                        "name": "How do I parse a mail header?",
                        "lines": 15
                    },
                    {
                        "name": "How do I check a valid mail address?",
                        "lines": 23
                    },
                    {
                        "name": "How do I decode a MIME/BASE64 string?",
                        "lines": 9
                    },
                    {
                        "name": "How do I find the user's mail address?",
                        "lines": 12
                    },
                    {
                        "name": "How do I send email?",
                        "lines": 27
                    },
                    {
                        "name": "How do I use MIME to make an attachment to a mail message?",
                        "lines": 12
                    },
                    {
                        "name": "How do I read email?",
                        "lines": 15
                    },
                    {
                        "name": "How do I find out my hostname, domainname, or IP address?",
                        "lines": 32
                    },
                    {
                        "name": "How do I fetch/put an (S)FTP file?",
                        "lines": 2
                    },
                    {
                        "name": "How can I do RPC in Perl?",
                        "lines": 2
                    }
                ]
            },
            {
                "name": "AUTHOR AND COPYRIGHT",
                "lines": 12,
                "subsections": []
            }
        ],
        "sections": {
            "NAME": {
                "content": "perlfaq9 - Web, Email and Networking\n",
                "subsections": []
            },
            "VERSION": {
                "content": "version 5.20210520\n",
                "subsections": []
            },
            "DESCRIPTION": {
                "content": "This section deals with questions related to running web sites, sending and receiving email\nas well as general networking.\n",
                "subsections": [
                    {
                        "name": "Should I use a web framework?",
                        "content": "Yes. If you are building a web site with any level of interactivity (forms / users /\ndatabases), you will want to use a framework to make handling requests and responses easier.\n\nIf there is no interactivity then you may still want to look at using something like Template\nToolkit <https://metacpan.org/module/Template> or Plack::Middleware::TemplateToolkit so\nmaintenance of your HTML files (and other assets) is easier.\n"
                    },
                    {
                        "name": "Which web framework should I use?",
                        "content": "There is no simple answer to this question. Perl frameworks can run everything from basic\nfile servers and small scale intranets to massive multinational multilingual websites that\nare the core to international businesses.\n\nBelow is a list of a few frameworks with comments which might help you in making a decision,\ndepending on your specific requirements. Start by reading the docs, then ask questions on the\nrelevant mailing list or IRC channel.\n\nCatalyst\nStrongly  object-oriented  and fully-featured with a long development history and a large\ncommunity and addon ecosystem. It is excellent for large and complex applications,  where\nyou have full control over the server.\n\nDancer2\nFree  of  legacy  weight,  providing  a lightweight and easy to learn API.  Has a growing\naddon ecosystem. It is best used  for  smaller  projects  and  very  easy  to  learn  for\nbeginners.\n\nMojolicious\nSelf-contained and powerful for both small and larger projects, with a focus on HTML5 and\nreal-time web technologies such as WebSockets.\n\nWeb::Simple\nStrongly  object-oriented  and  minimal,  built  for  speed and intended as a toolkit for\nbuilding micro web apps,  custom  frameworks  or  for  tieing  together  existing  Plack-\ncompatible web applications with one central dispatcher.\n\nAll  of  these  interact  with  or  use Plack which is worth understanding the basics of when\nbuilding   a   website   in   Perl   (there   is   a   lot   of   useful    Plack::Middleware\n<https://metacpan.org/search?q=plack%3A%3Amiddleware>).\n"
                    },
                    {
                        "name": "What is Plack and PSGI?",
                        "content": "PSGI  is the Perl Web Server Gateway Interface Specification, it is a standard that many Perl\nweb frameworks use, you should not need to understand it to build a web site,  the  part  you\nmight want to use is Plack.\n\nPlack   is   a   set   of   tools   for   using   the  PSGI  stack.  It  contains  middleware\n<https://metacpan.org/search?q=plack%3A%3Amiddleware>  components,  a  reference  server  and\nutilities  for  Web  application frameworks.  Plack is like Ruby's Rack or Python's Paste for\nWSGI.\n\nYou could build a web site using Plack and your own code, but for anything other than a  very\nbasic web site, using a web framework (that uses <https://plackperl.org>) is a better option.\n"
                    },
                    {
                        "name": "How do I remove HTML from a string?",
                        "content": "Use  HTML::Strip,  or  HTML::FormatText which not only removes HTML but also attempts to do a\nlittle simple formatting of the resulting plain text.\n"
                    },
                    {
                        "name": "How do I extract URLs?",
                        "content": "HTML::SimpleLinkExtor will extract URLs from  HTML,  it  handles  anchors,  images,  objects,\nframes,  and  many other tags that can contain a URL.  If you need anything more complex, you\ncan create your  own  subclass  of  HTML::LinkExtor  or  HTML::Parser.  You  might  even  use\nHTML::SimpleLinkExtor as an example for something specifically suited to your needs.\n\nYou can use URI::Find or URL::Search to extract URLs from an arbitrary text document.\n"
                    },
                    {
                        "name": "How do I fetch an HTML file?",
                        "content": "(contributed by brian d foy)\n\nThe  core  HTTP::Tiny  module can fetch web resources and give their content back to you as a\nstring:\n\nuse HTTP::Tiny;\n\nmy $ua = HTTP::Tiny->new;\nmy $html = $ua->get( \"http://www.example.com/index.html\" )->{content};\n\nIt can also store the resource directly in a file:\n\n$ua->mirror( \"http://www.example.com/index.html\", \"foo.html\" );\n\nIf you need to do something more complicated, the HTTP::Tiny  object  can  be  customized  by\nsetting  attributes,  or  you  can  use  LWP::UserAgent  from the libwww-perl distribution or\nMojo::UserAgent from the Mojolicious distribution to make common tasks easier. If you want to\nsimulate an interactive web browser, you can use the WWW::Mechanize module.\n"
                    },
                    {
                        "name": "How do I automate an HTML form submission?",
                        "content": "If you are doing something complex, such as moving through many pages  and  forms  or  a  web\nsite, you can use WWW::Mechanize. See its documentation for all the details.\n\nIf  you're submitting values using the GET method, create a URL and encode the form using the\n\"wwwformurlencode\" method from HTTP::Tiny:\n\nuse HTTP::Tiny;\n\nmy $ua = HTTP::Tiny->new;\n\nmy $query = $ua->wwwformurlencode([ q => 'DBFile', lucky => 1 ]);\nmy $url = \"https://metacpan.org/search?$query\";\nmy $content = $ua->get($url)->{content};\n\nIf  you're  using  the  POST  method,  the  \"postform\"  method  will  encode   the   content\nappropriately.\n\nuse HTTP::Tiny;\n\nmy $ua = HTTP::Tiny->new;\n\nmy $url = 'https://metacpan.org/search';\nmy $form = [ q => 'DBFile', lucky => 1 ];\nmy $content = $ua->postform($url, $form)->{content};\n"
                    },
                    {
                        "name": "How do I decode or create those %-encodings on the web?",
                        "content": "Most of the time you should not need to do this as your web framework, or if you are making a\nrequest, the LWP or other module would handle it for you.\n\nTo  encode  a  string yourself, use the URI::Escape module. The \"uriescape\" function returns\nthe escaped string:\n\nmy $original = \"Colon : Hash # Percent %\";\n\nmy $escaped = uriescape( $original );\n\nprint \"$escaped\\n\"; # 'Colon%20%3A%20Hash%20%23%20Percent%20%25'\n\nTo decode the string, use the \"uriunescape\" function:\n\nmy $unescaped = uriunescape( $escaped );\n\nprint $unescaped; # back to original\n\nRemember not to encode a full URI, you need to escape each component separately and then join\nthem together.\n"
                    },
                    {
                        "name": "How do I redirect to another page?",
                        "content": "Most Perl Web Frameworks will have a mechanism for doing this, using the  Catalyst  framework\nit would be:\n\n$c->res->redirect($url);\n$c->detach();\n\nIf  you  are using Plack (which most frameworks do), then Plack::Middleware::Rewrite is worth\nlooking at if you are migrating from Apache or have URL's you want to always redirect.\n"
                    },
                    {
                        "name": "How do I put a password on my web pages?",
                        "content": "See if the web framework you are using has an authentication system and  if  that  fits  your\nneeds.\n\nAlternativly look at Plack::Middleware::Auth::Basic, or one of the other Plack authentication\n<https://metacpan.org/search?q=plack+auth> options.\n"
                    },
                    {
                        "name": "How  do  I  make  sure  users  can't enter values into a form that causes my CGI script to do bad",
                        "content": ""
                    },
                    {
                        "name": "things?",
                        "content": "(contributed by brian d foy)\n\nYou can't prevent people from sending your script bad data. Even if you add some  client-side\nchecks,  people may disable them or bypass them completely. For instance, someone might use a\nmodule such as LWP to submit to your web site. If you want to prevent data that  try  to  use\nSQL  injection  or other sorts of attacks (and you should want to), you have to not trust any\ndata that enter your program.\n\nThe perlsec documentation has general advice about data security.  If you are using  the  DBI\nmodule,  use placeholder to fill in data.  If you are running external programs with \"system\"\nor \"exec\", use the list forms. There are many other precautions that  you  should  take,  too\nmany  to  list  here, and most of them fall under the category of not using any data that you\ndon't intend to use. Trust no one.\n"
                    },
                    {
                        "name": "How do I parse a mail header?",
                        "content": "Use the Email::MIME module. It's well-tested and supports all the craziness that  you'll  see\nin the real world (comment-folding whitespace, encodings, comments, etc.).\n\nuse Email::MIME;\n\nmy $message = Email::MIME->new($rfc2822);\nmy $subject = $message->header('Subject');\nmy $from    = $message->header('From');\n\nIf you've already got some other kind of email object, consider passing it to Email::Abstract\nand then using its cast method to get an Email::MIME object:\n\nmy $abstract = Email::Abstract->new($mailmessageobject);\nmy $emailmimeobject = $abstract->cast('Email::MIME');\n"
                    },
                    {
                        "name": "How do I check a valid mail address?",
                        "content": "(partly contributed by Aaron Sherman)\n\nThis isn't as simple a question as it sounds. There are two parts:\n\na) How do I verify that an email address is correctly formatted?\n\nb) How do I verify that an email address targets a valid recipient?\n\nWithout  sending  mail  to the address and seeing whether there's a human on the other end to\nanswer you, you cannot fully answer part b, but the Email::Valid module will do both  part  a\nand part b as far as you can in real-time.\n\nOur  best  advice  for  verifying a person's mail address is to have them enter their address\ntwice, just as you normally do to change a password. This usually weeds out  typos.  If  both\nversions  match,  send  mail  to that address with a personal message. If you get the message\nback and they've followed your directions, you can be reasonably assured that it's real.\n\nA related strategy that's less open to forgery is to give them a PIN  (personal  ID  number).\nRecord  the  address and PIN (best that it be a random one) for later processing. In the mail\nyou send, include a link to your site with the PIN included. If the mail  bounces,  you  know\nit's  not valid. If they don't click on the link, either they forged the address or (assuming\nthey got the message) following through wasn't important so you don't need to worry about it.\n"
                    },
                    {
                        "name": "How do I decode a MIME/BASE64 string?",
                        "content": "The MIME::Base64 package handles this as well as the  MIME/QP  encoding.   Decoding  base  64\nbecomes as simple as:\n\nuse MIME::Base64;\nmy $decoded = decodebase64($encoded);\n\nThe  Email::MIME  module  can decode base 64-encoded email message parts transparently so the\ndeveloper doesn't need to worry about it.\n"
                    },
                    {
                        "name": "How do I find the user's mail address?",
                        "content": "Ask them for it. There are so many email providers available that  it's  unlikely  the  local\nsystem has any idea how to determine a user's email address.\n\nThe  exception is for organization-specific email (e.g. foo@yourcompany.com) where policy can\nbe codified in your program. In that case, you could look at $ENV{USER},  $ENV{LOGNAME},  and\ngetpwuid($<) in scalar context, like so:\n\nmy $username = getpwuid($<)\n\nBut  you still cannot make assumptions about whether this is correct, unless your policy says\nit is. You really are best off asking the user.\n"
                    },
                    {
                        "name": "How do I send email?",
                        "content": "Use the Email::Stuffer module, like so:\n\n# first, create your message\nmy $message = Email::Stuffer->from('you@example.com')\n->to('friend@example.com')\n->subject('Happy birthday!')\n->textbody(\"Happy birthday to you!\\n\");\n\n$message->sendordie;\n\nBy default, Email::Sender::Simple (the \"send\" and \"sendordie\" methods use  this  under  the\nhood)  will  try \"sendmail\" first, if it exists in your $PATH. This generally isn't the case.\nIf there's a remote mail server you use to send  mail,  consider  investigating  one  of  the\nTransport classes. At time of writing, the available transports include:\n\nEmail::Sender::Transport::Sendmail\nThis is the default. If you can use the mail(1) or mailx(1) program to send mail from the\nmachine where your code runs, you should be able to use this.\n\nEmail::Sender::Transport::SMTP\nThis  transport contacts a remote SMTP server over TCP. It optionally uses TLS or SSL and\ncan authenticate to the server via SASL.\n\nTelling Email::Stuffer to use your transport is straightforward.\n\n$message->transport($emailsendertransportobject)->sendordie;\n"
                    },
                    {
                        "name": "How do I use MIME to make an attachment to a mail message?",
                        "content": "Email::MIME directly supports multipart messages. Email::MIME objects  themselves  are  parts\nand  can  be attached to other Email::MIME objects. Consult the Email::MIME documentation for\nmore information, including all of the supported methods and examples of their use.\n\nEmail::Stuffer uses Email::MIME under the hood to construct  messages,  and  wraps  the  most\ncommon attachment tasks with the simple \"attach\" and \"attachfile\" methods.\n\nEmail::Stuffer->to('friend@example.com')\n->subject('The file')\n->attachfile('stuff.csv')\n->sendordie;\n"
                    },
                    {
                        "name": "How do I read email?",
                        "content": "Use the Email::Folder module, like so:\n\nuse Email::Folder;\n\nmy $folder = Email::Folder->new('/path/to/email/folder');\nwhile(my $message = $folder->nextmessage) {\n# nextmessage returns Email::Simple objects, but we want\n# Email::MIME objects as they're more robust\nmy $mime = Email::MIME->new($message->asstring);\n}\n\nThere  are  different  classes  in the Email::Folder namespace for supporting various mailbox\ntypes. Note that these modules are generally rather limited and only support  reading  rather\nthan writing.\n"
                    },
                    {
                        "name": "How do I find out my hostname, domainname, or IP address?",
                        "content": "(contributed by brian d foy)\n\nThe Net::Domain module, which is part of the Standard Library starting in Perl 5.7.3, can get\nyou the fully qualified domain name (FQDN), the host name, or the domain name.\n\nuse Net::Domain qw(hostname hostfqdn hostdomain);\n\nmy $host = hostfqdn();\n\nThe Sys::Hostname module, part of the Standard Library, can also get the hostname:\n\nuse Sys::Hostname;\n\n$host = hostname();\n\nThe  Sys::Hostname::Long  module  takes  a  different approach and tries harder to return the\nfully qualified hostname:\n\nuse Sys::Hostname::Long 'hostnamelong';\n\nmy $hostname = hostnamelong();\n\nTo get the IP address, you can use the \"gethostbyname\" built-in function  to  turn  the  name\ninto  a  number.  To  turn  that number into the dotted octet form (a.b.c.d) that most people\nexpect, use the \"inetntoa\" function from the Socket module, which also comes with perl.\n\nuse Socket;\n\nmy $address = inetntoa(\nscalar gethostbyname( $host || 'localhost' )\n);\n"
                    },
                    {
                        "name": "How do I fetch/put an (S)FTP file?",
                        "content": "Net::FTP, and Net::SFTP allow you to interact with FTP and SFTP (Secure FTP) servers.\n"
                    },
                    {
                        "name": "How can I do RPC in Perl?",
                        "content": "Use one of the RPC modules( <https://metacpan.org/search?q=RPC> ).\n"
                    }
                ]
            },
            "AUTHOR AND COPYRIGHT": {
                "content": "Copyright (c) 1997-2010 Tom Christiansen, Nathan Torkington, and other authors as noted.  All\nrights reserved.\n\nThis  documentation is free; you can redistribute it and/or modify it under the same terms as\nPerl itself.\n\nIrrespective of its distribution, all code examples in this file are hereby placed  into  the\npublic domain. You are permitted and encouraged to use this code in your own programs for fun\nor  for  profit as you see fit. A simple comment in the code giving credit would be courteous\nbut is not required.\n\nperl v5.38.2                                 2026-06-12                                  PERLFAQ9(1)",
                "subsections": []
            }
        }
    }
}