[ Avaa Bypassed ]




Upload:

Command:

hmhc3928@3.23.101.75: ~ $
=head1 NAME

perlfaq9 - Web, Email and Networking

=head1 DESCRIPTION

This section deals with questions related to running web sites,
sending and receiving email as well as general networking.

=head2 Should I use a web framework?

Yes. If you are building a web site with any level of interactivity
(forms / users / databases), you
will want to use a framework to make handling requests
and responses easier.

If there is no interactivity then you may still want
to look at using something like L<Template Toolkit|https://metacpan.org/module/Template>
or L<Plack::Middleware::TemplateToolkit>
so maintenance of your HTML files (and other assets) is easier.

=head2 Which web framework should I use?
X<framework> X<CGI.pm> X<CGI> X<Catalyst> X<Dancer>

There is no simple answer to this question. Perl frameworks can run everything
from basic file servers and small scale intranets to massive multinational
multilingual websites that are the core to international businesses.

Below is a list of a few frameworks with comments which might help you in 
making a decision, depending on your specific requirements. Start by reading
the docs, then ask questions on the relevant mailing list or IRC channel.

=over 4

=item L<Catalyst>

Strongly object-oriented and fully-featured with a long development history and
a large community and addon ecosystem. It is excellent for large and complex
applications, where you have full control over the server.

=item L<Dancer>

Young and free of legacy weight, providing a lightweight and easy to learn API.
Has a growing addon ecosystem. It is best used for smaller projects and
very easy to learn for beginners.

=item L<Mojolicious>

Fairly young with a focus on HTML5 and real-time web technologies such as
WebSockets.

=item L<Web::Simple>

Currently experimental, strongly object-oriented, built for speed and intended
as a toolkit for building micro web apps, custom frameworks or for tieing
together existing Plack-compatible web applications with one central dispatcher.

=back

All of these interact with or use L<Plack> which is worth understanding
the basics of when building a website in Perl (there is a lot of useful
L<Plack::Middleware|https://metacpan.org/search?q=plack%3A%3Amiddleware>).

=head2 What is Plack and PSGI?

L<PSGI> is the Perl Web Server Gateway Interface Specification, it is
a standard that many Perl web frameworks use, you should not need to
understand it to build a web site, the part you might want to use is L<Plack>.

L<Plack> is a set of tools for using the PSGI stack. It contains
L<middleware|https://metacpan.org/search?q=plack%3A%3Amiddleware>
components, a reference server and utilities for Web application frameworks.
Plack is like Ruby's Rack or Python's Paste for WSGI.

You could build a web site using L<Plack> and your own code,
but for anything other than a very basic web site, using a web framework
(that uses L<Plack>) is a better option.

=head2 How do I remove HTML from a string?

Use L<HTML::Strip>, or L<HTML::FormatText> which not only removes HTML
but also attempts to do a little simple formatting of the resulting
plain text.

=head2 How do I extract URLs?

L<HTML::SimpleLinkExtor> will extract URLs from HTML, it handles anchors,
images, objects, frames, and many other tags that can contain a URL.
If you need anything more complex, you can create your own subclass of
L<HTML::LinkExtor> or L<HTML::Parser>. You might even use
L<HTML::SimpleLinkExtor> as an example for something specifically
suited to your needs.

You can use L<URI::Find> to extract URLs from an arbitrary text document.

=head2 How do I fetch an HTML file?

(contributed by brian d foy)

Use the libwww-perl distribution. The L<LWP::Simple> module can fetch web
resources and give their content back to you as a string:

    use LWP::Simple qw(get);

    my $html = get( "http://www.example.com/index.html" );

It can also store the resource directly in a file:

    use LWP::Simple qw(getstore);

    getstore( "http://www.example.com/index.html", "foo.html" );

If you need to do something more complicated, you can use
L<LWP::UserAgent> module to create your own user-agent (e.g. browser)
to get the job done. If you want to simulate an interactive web
browser, you can use the L<WWW::Mechanize> module.

=head2 How do I automate an HTML form submission?

If you are doing something complex, such as moving through many pages
and forms or a web site, you can use L<WWW::Mechanize>. See its
documentation for all the details.

If you're submitting values using the GET method, create a URL and encode
the form using the C<query_form> method:

    use LWP::Simple;
    use URI::URL;

    my $url = url('L<http://www.perl.com/cgi-bin/cpan_mod')>;
    $url->query_form(module => 'DB_File', readme => 1);
    $content = get($url);

If you're using the POST method, create your own user agent and encode
the content appropriately.

    use HTTP::Request::Common qw(POST);
    use LWP::UserAgent;

    my $ua = LWP::UserAgent->new();
    my $req = POST 'L<http://www.perl.com/cgi-bin/cpan_mod'>,
                   [ module => 'DB_File', readme => 1 ];
    my $content = $ua->request($req)->as_string;

=head2 How do I decode or create those %-encodings on the web?
X<URI> X<URI::Escape> X<RFC 2396>

Most of the time you should not need to do this as
your web framework, or if you are making a request,
the L<LWP> or other module would handle it for you.

To encode a string yourself, use the L<URI::Escape> module. The C<uri_escape>
function returns the escaped string:

    my $original = "Colon : Hash # Percent %";

    my $escaped = uri_escape( $original );

    print "$escaped\n"; # 'Colon%20%3A%20Hash%20%23%20Percent%20%25'

To decode the string, use the C<uri_unescape> function:

    my $unescaped = uri_unescape( $escaped );

    print $unescaped; # back to original

Remember not to encode a full URI, you need to escape each
component separately and then join them together.

=head2 How do I redirect to another page?

Most Perl Web Frameworks will have a mechanism for doing this,
using the L<Catalyst> framework it would be:

    $c->res->redirect($url);
    $c->detach();

If you are using Plack (which most frameworks do), then
L<Plack::Middleware::Rewrite> is worth looking at if you
are migrating from Apache or have URL's you want to always
redirect.

=head2 How do I put a password on my web pages?

See if the web framework you are using has an
authentication system and if that fits your needs.

Alternativly look at L<Plack::Middleware::Auth::Basic>,
or one of the other L<Plack authentication|https://metacpan.org/search?q=plack+auth>
options.

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

(contributed by brian d foy)

You can't prevent people from sending your script bad data. Even if
you add some client-side checks, people may disable them or bypass
them completely. For instance, someone might use a module such as
L<LWP> to submit to your web site. If you want to prevent data that
try to use SQL injection or other sorts of attacks (and you should
want to), you have to not trust any data that enter your program.

The L<perlsec> documentation has general advice about data security.
If you are using the L<DBI> module, use placeholder to fill in data.
If you are running external programs with C<system> or C<exec>, use
the list forms. There are many other precautions that you should take,
too many to list here, and most of them fall under the category of not
using any data that you don't intend to use. Trust no one.

=head2 How do I parse a mail header?

Use the L<Email::MIME> module. It's well-tested and supports all the
craziness that you'll see in the real world (comment-folding whitespace,
encodings, comments, etc.).

  use Email::MIME;

  my $message = Email::MIME->new($rfc2822);
  my $subject = $message->header('Subject');
  my $from    = $message->header('From');

If you've already got some other kind of email object, consider passing
it to L<Email::Abstract> and then using its cast method to get an
L<Email::MIME> object:

  my $mail_message_object = read_message();
  my $abstract = Email::Abstract->new($mail_message_object);
  my $email_mime_object = $abstract->cast('Email::MIME');

=head2 How do I check a valid mail address?

(partly contributed by Aaron Sherman)

This isn't as simple a question as it sounds. There are two parts:

a) How do I verify that an email address is correctly formatted?

b) How do I verify that an email address targets a valid recipient?

Without sending mail to the address and seeing whether there's a human
on the other end to answer you, you cannot fully answer part I<b>, but
the L<Email::Valid> module will do both part I<a> and part I<b> as far
as you can in real-time.

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. 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, include a link to
your site with the PIN included. If the mail bounces, you know it's not
valid. If they don't click on the link, either they forged the address or
(assuming they got the message) following through wasn't important so you
don't need to worry about it.

=head2 How do I decode a MIME/BASE64 string?

The L<MIME::Base64> package handles this as well as the MIME/QP encoding.
Decoding base 64 becomes as simple as:

    use MIME::Base64;
    my $decoded = decode_base64($encoded);

The L<Email::MIME> module can decode base 64-encoded email message parts
transparently so the developer doesn't need to worry about it.

=head2 How do I find the user's mail address?

Ask them for it. There are so many email providers available that it's
unlikely the local system has any idea how to determine a user's email address.

The exception is for organization-specific email (e.g. foo@yourcompany.com)
where policy can be codified in your program. In that case, you could look at
$ENV{USER}, $ENV{LOGNAME}, and getpwuid($<) in scalar context, like so:

  my $user_name = getpwuid($<)

But you still cannot make assumptions about whether this is correct, unless
your policy says it is. You really are best off asking the user.

=head2 How do I send email?

Use the L<Email::MIME> and L<Email::Sender::Simple> modules, like so:

  # first, create your message
  my $message = Email::MIME->create(
    header_str => [
      From    => 'you@example.com',
      To      => 'friend@example.com',
      Subject => 'Happy birthday!',
    ],
    attributes => {
      encoding => 'quoted-printable',
      charset  => 'ISO-8859-1',
    },
    body_str => "Happy birthday to you!\n",
  );

  use Email::Sender::Simple qw(sendmail);
  sendmail($message);

By default, L<Email::Sender::Simple> will try `sendmail` first, if it exists
in your $PATH. This generally isn't the case. If there's a remote mail
server you use to send mail, consider investigating one of the Transport
classes. At time of writing, the available transports include:

=over 4

=item L<Email::Sender::Transport::Sendmail>

This is the default. If you can use the L<mail(1)> or L<mailx(1)>
program to send mail from the machine where your code runs, you should
be able to use this.

=item L<Email::Sender::Transport::SMTP>

This transport contacts a remote SMTP server over TCP. It optionally
uses SSL and can authenticate to the server via SASL.

=item L<Email::Sender::Transport::SMTP::TLS>

This is like the SMTP transport, but uses TLS security. You can
authenticate with this module as well, using any mechanisms your server
supports after STARTTLS.

=back

Telling L<Email::Sender::Simple> to use your transport is straightforward.

  sendmail(
    $message,
    {
      transport => $email_sender_transport_object,
    }
  );

=head2 How do I use MIME to make an attachment to a mail message?

L<Email::MIME> directly supports multipart messages. L<Email::MIME>
objects themselves are parts and can be attached to other L<Email::MIME>
objects. Consult the L<Email::MIME> documentation for more information,
including all of the supported methods and examples of their use.

=head2 How do I read email?

Use the L<Email::Folder> module, like so:

  use Email::Folder;

  my $folder = Email::Folder->new('/path/to/email/folder');
  while(my $message = $folder->next_message) {
    # next_message returns Email::Simple objects, but we want
    # Email::MIME objects as they're more robust
    my $mime = Email::MIME->new($message->as_string);
  }

There are different classes in the L<Email::Folder> namespace for
supporting various mailbox types. Note that these modules are generally
rather limited and only support B<reading> rather than writing.

=head2 How do I find out my hostname, domainname, or IP address?
X<hostname, domainname, IP address, host, domain, hostfqdn, inet_ntoa,
gethostbyname, Socket, Net::Domain, Sys::Hostname>

(contributed by brian d foy)

The L<Net::Domain> module, which is part of the Standard Library starting
in Perl 5.7.3, can get you the fully qualified domain name (FQDN), the host
name, or the domain name.

    use Net::Domain qw(hostname hostfqdn hostdomain);

    my $host = hostfqdn();

The L<Sys::Hostname> module, part of the Standard Library, can also get the
hostname:

    use Sys::Hostname;

    $host = hostname();


The L<Sys::Hostname::Long> module takes a different approach and tries
harder to return the fully qualified hostname:

  use Sys::Hostname::Long 'hostname_long';

  my $hostname = hostname_long();

To get the IP address, you can use the C<gethostbyname> built-in function
to turn the name into a number. To turn that number into the dotted octet
form (a.b.c.d) that most people expect, use the C<inet_ntoa> function
from the L<Socket> module, which also comes with perl.

    use Socket;

    my $address = inet_ntoa(
        scalar gethostbyname( $host || 'localhost' )
    );

=head2 How do I fetch/put an (S)FTP file?

L<Net::FTP>, and L<Net::SFTP> allow you to interact with FTP and SFTP (Secure
FTP) servers.

=head2 How can I do RPC in Perl?

Use one of the RPC modules( L<https://metacpan.org/search?q=RPC> ).

=head1 AUTHOR AND COPYRIGHT

Copyright (c) 1997-2010 Tom Christiansen, Nathan Torkington, and
other authors as noted. All rights reserved.

This documentation is free; you can redistribute it and/or modify it
under the same terms as Perl itself.

Irrespective of its distribution, all code examples in this file
are hereby placed into the public domain. You are permitted and
encouraged to use this code in your own programs for fun
or for profit as you see fit. A simple comment in the code giving
credit would be courteous but is not required.

Filemanager

Name Type Size Permission Actions
a2p.pod File 5.96 KB 0644
perl.pod File 15.43 KB 0644
perl5004delta.pod File 54.92 KB 0644
perl5005delta.pod File 33.48 KB 0644
perl5100delta.pod File 53.41 KB 0644
perl5101delta.pod File 42.86 KB 0644
perl5120delta.pod File 87.19 KB 0644
perl5121delta.pod File 9.91 KB 0644
perl5122delta.pod File 9.38 KB 0644
perl5123delta.pod File 4 KB 0644
perl5124delta.pod File 3.59 KB 0644
perl5140delta.pod File 140.94 KB 0644
perl5141delta.pod File 7.78 KB 0644
perl5142delta.pod File 6.73 KB 0644
perl5143delta.pod File 7.58 KB 0644
perl5160delta.pod File 130.52 KB 0644
perl5161delta.pod File 6 KB 0644
perl5162delta.pod File 3.51 KB 0644
perl5163delta.pod File 3.99 KB 0644
perl561delta.pod File 121.79 KB 0644
perl56delta.pod File 104.68 KB 0644
perl581delta.pod File 37.17 KB 0644
perl582delta.pod File 4.37 KB 0644
perl583delta.pod File 6.19 KB 0644
perl584delta.pod File 7.19 KB 0644
perl585delta.pod File 5.75 KB 0644
perl586delta.pod File 4.54 KB 0644
perl587delta.pod File 8.16 KB 0644
perl588delta.pod File 24.68 KB 0644
perl589delta.pod File 52.64 KB 0644
perl58delta.pod File 112.21 KB 0644
perlaix.pod File 18.77 KB 0644
perlamiga.pod File 6.87 KB 0644
perlapi.pod File 315.46 KB 0644
perlapio.pod File 18.88 KB 0644
perlartistic.pod File 6.85 KB 0644
perlbeos.pod File 2.87 KB 0644
perlbook.pod File 7.19 KB 0644
perlboot.pod File 183 B 0644
perlbot.pod File 182 B 0644
perlbs2000.pod File 7.73 KB 0644
perlcall.pod File 54.03 KB 0644
perlce.pod File 8.72 KB 0644
perlcheat.pod File 4.39 KB 0644
perlclib.pod File 7.5 KB 0644
perlcn.pod File 4.82 KB 0644
perlcommunity.pod File 6.29 KB 0644
perlcygwin.pod File 27.17 KB 0644
perldata.pod File 36.33 KB 0644
perldbmfilter.pod File 4.86 KB 0644
perldebguts.pod File 36.79 KB 0644
perldebtut.pod File 20.79 KB 0644
perldebug.pod File 38.15 KB 0644
perldelta.pod File 3.99 KB 0644
perldgux.pod File 2.75 KB 0644
perldiag.pod File 207.82 KB 0644
perldos.pod File 10.28 KB 0644
perldsc.pod File 24.84 KB 0644
perldtrace.pod File 6.21 KB 0644
perlebcdic.pod File 67.61 KB 0644
perlembed.pod File 35.21 KB 0644
perlepoc.pod File 3.57 KB 0644
perlexperiment.pod File 4.84 KB 0644
perlfaq.pod File 22.01 KB 0644
perlfaq1.pod File 13.52 KB 0644
perlfaq2.pod File 9.28 KB 0644
perlfaq3.pod File 37.46 KB 0644
perlfaq4.pod File 87.39 KB 0644
perlfaq5.pod File 54.11 KB 0644
perlfaq6.pod File 38.66 KB 0644
perlfaq7.pod File 36.35 KB 0644
perlfaq8.pod File 48.28 KB 0644
perlfaq9.pod File 14.71 KB 0644
perlfork.pod File 12.78 KB 0644
perlform.pod File 16.29 KB 0644
perlfreebsd.pod File 1.55 KB 0644
perlfunc.pod File 338.43 KB 0644
perlgit.pod File 29.75 KB 0644
perlglossary.pod File 110.66 KB 0644
perlgpl.pod File 13.54 KB 0644
perlguts.pod File 111.66 KB 0644
perlhack.pod File 35.03 KB 0644
perlhacktips.pod File 45.5 KB 0644
perlhacktut.pod File 6.07 KB 0644
perlhaiku.pod File 1.47 KB 0644
perlhist.pod File 43.32 KB 0644
perlhpux.pod File 28.07 KB 0644
perlhurd.pod File 1.94 KB 0644
perlintern.pod File 42.53 KB 0644
perlinterp.pod File 30 KB 0644
perlintro.pod File 22.08 KB 0644
perliol.pod File 33.03 KB 0644
perlipc.pod File 70.17 KB 0644
perlirix.pod File 4.29 KB 0644
perljp.pod File 7.57 KB 0644
perlko.pod File 7.52 KB 0644
perllexwarn.pod File 14.61 KB 0644
perllinux.pod File 1.45 KB 0644
perllocale.pod File 51.43 KB 0644
perllol.pod File 10.93 KB 0644
perlmacos.pod File 1001 B 0644
perlmacosx.pod File 10.4 KB 0644
perlmod.pod File 24.04 KB 0644
perlmodinstall.pod File 12.41 KB 0644
perlmodlib.pod File 78.49 KB 0644
perlmodstyle.pod File 20.76 KB 0644
perlmpeix.pod File 14.24 KB 0644
perlmroapi.pod File 3.13 KB 0644
perlnetware.pod File 6.35 KB 0644
perlnewmod.pod File 10.95 KB 0644
perlnumber.pod File 8.16 KB 0644
perlobj.pod File 33.65 KB 0644
perlootut.pod File 25.6 KB 0644
perlop.pod File 121.73 KB 0644
perlopenbsd.pod File 1.18 KB 0644
perlopentut.pod File 37.53 KB 0644
perlos2.pod File 90.53 KB 0644
perlos390.pod File 15.2 KB 0644
perlos400.pod File 4.51 KB 0644
perlpacktut.pod File 49.83 KB 0644
perlperf.pod File 50.05 KB 0644
perlplan9.pod File 5 KB 0644
perlpod.pod File 21.27 KB 0644
perlpodspec.pod File 66.2 KB 0644
perlpolicy.pod File 19.73 KB 0644
perlport.pod File 82.63 KB 0644
perlpragma.pod File 5.11 KB 0644
perlqnx.pod File 4.14 KB 0644
perlre.pod File 100.76 KB 0644
perlreapi.pod File 25.17 KB 0644
perlrebackslash.pod File 25.64 KB 0644
perlrecharclass.pod File 34.19 KB 0644
perlref.pod File 28.32 KB 0644
perlreftut.pod File 18.23 KB 0644
perlreguts.pod File 36 KB 0644
perlrequick.pod File 17.5 KB 0644
perlreref.pod File 14.19 KB 0644
perlretut.pod File 115.13 KB 0644
perlriscos.pod File 1.49 KB 0644
perlrun.pod File 49.58 KB 0644
perlsec.pod File 22.77 KB 0644
perlsolaris.pod File 28.63 KB 0644
perlsource.pod File 6.19 KB 0644
perlstyle.pod File 8.42 KB 0644
perlsub.pod File 55.15 KB 0644
perlsymbian.pod File 15.44 KB 0644
perlsyn.pod File 41.04 KB 0644
perlthrtut.pod File 45.41 KB 0644
perltie.pod File 37.02 KB 0644
perltoc.pod File 639 KB 0644
perltodo.pod File 362 B 0644
perltooc.pod File 183 B 0644
perltoot.pod File 183 B 0644
perltrap.pod File 40.28 KB 0644
perltru64.pod File 7.55 KB 0644
perltw.pod File 5.15 KB 0644
perlunicode.pod File 70.89 KB 0644
perlunifaq.pod File 13.31 KB 0644
perluniintro.pod File 35.44 KB 0644
perluniprops.pod File 229.74 KB 0644
perlunitut.pod File 7.76 KB 0644
perlutil.pod File 9.68 KB 0644
perluts.pod File 3.11 KB 0644
perlvar.pod File 69.19 KB 0644
perlvmesa.pod File 3.88 KB 0644
perlvms.pod File 51.33 KB 0644
perlvos.pod File 5.82 KB 0644
perlwin32.pod File 34.58 KB 0644
perlxs.pod File 71.66 KB 0644
perlxstut.pod File 48.52 KB 0644
perlxstypemap.pod File 22.97 KB 0644