perl tutorials
Results 1 to 7 of 7

Thread: perl tutorials

  1. #1
    Member
    Join Date
    Dec 2001
    Posts
    50

    perl tutorials

    I have seen alot of post's on this BBS that ask "what is the best
    scripting lang to learn" I once asked myself the same thing.Rather then
    telling you what I believe.I think the best way to do this is
    to show you.Because I fell as A human we were given the right to make of own decisions.
    so let goooo.

    my fav scripting language perl.
    PERL is a powerful language that has similar syntax as c
    because i was writes in c.now lets get our hand's dirty.
    all you elites out there don't hate on me because I decided
    to start with the "hello world" programs.
    (this is not the best way to program but i have decided this might be the
    best way to show you)


    code:--------------------------------------------------------------------------------
    $string = "hello world"; #comments are started with the sign "#"
    #perl uses "$" for its variables

    print "$string"; #this print to the screen the string
    #"hello world"

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

    perl also is very good with html. lets make the simple program display
    html so you use it for the web.

    code:--------------------------------------------------------------------------------
    #!/usr/local/bin/perl #this line is very important
    #it points the program to the
    #perl interpreter you might have to
    #change depending on the system that you use

    $string = "hello world";

    print "<html><head><title>";
    print "simple program";
    print "</title></head>";
    print "<body>$string</body>";
    print "</html>";
    --------------------------------------------------------------------------------


    If you upload this file use the .pl or .cgi file exstention with the permission of 0755(for scripts you want to exicute use 0755 and 0644 for lib and log files) and place it in a working "cgi-bin"
    I wont get to deep into permissions at this time.but basically this means that
    you as the owner of the file can read write and execute the code but everybody else can just
    read and execute it.
    PERL is also great for math.lets take a look at simple math script.

    code:--------------------------------------------------------------------------------
    #!/usr/local/bin/perl

    $sales_for_00 = 2000;
    $sales_for_99 = 2043;
    $total_sales = $sales_for_00 + $sales_for_99;

    print "$total_sales";
    --------------------------------------------------------------------------------

    I know this is a very basic math script but you get the Idea.
    PERL is great for matching strings and replacing them with whatever you want.
    lets just say you put up a site and you wanted to make sure that the visiters
    dont enter a "*" or any other potentailly dangerious charicters.You could write
    a program to take them out

    code:--------------------------------------------------------------------------------
    !#/usr/local/bin/perl
    require "sub.lib";
    &parse_form;
    $name = $formdata{'name'};
    $email = $formdata{'email'};
    $comment = $formdata{'comment'};

    $name =~ tr/*/ /g;
    $email =~ tr/*/ /g;
    $comments =~ tr/*/ /g;
    print "content-type: text/html\n\n";
    print "this program takes out the "*" in\n";
    print "the name, email, and comment's and replaces\n";
    print "them with " " using the powerfull tr///; (trade) funtion\n";
    --------------------------------------------------------------------------------

    you can trade (tr/// substatute (s/// or match (m/// string's in perl.I think that this is one of the great funtions in PERL.
    Perl is good for logging vistiters if you need to for whatever reason.
    perl uses the $ENV{'<some_variable>'}; to get the enviromemt infomation they need.
    lets take a look.

    code:--------------------------------------------------------------------------------
    #!/usr/local/bin/perl

    $log_file = "/data/log.txt"; #sets the text file to use
    $name = $ENV{'REMOTE_NAME'}; #gets name of the visiters
    $ip = $ENV{'REMOTE_ADDR'}; #gets ip of visiters
    $browser = $ENV{'HTTP_USER_AGENT'}; #gets the browser used
    #now let's write this to file for later viewing
    OPEN(LOG,">>$log_file") || &errorfile; #this line opens the log file
    flock(LOG, 2); #and the ">>" appends the file
    print LOG "$name|$ip|$browser\n"; #the flock funtion stops the use of #the script by more then
    flock(LOG, 8); #one person at a time
    CLOSE (LOG);

    sub errorfile {
    print "content-type: text/html\n\n";
    print "could not open file";
    }
    --------------------------------------------------------------------------------

    now for my favorite part of PERL is great for processing text vie a form.If you don't know about form here is a crash course.

    code:--------------------------------------------------------------------------------
    <html>
    <head>
    <title>
    simple form
    </title>
    </head>
    <body>
    <form name=form method=post action="form.cgi">
    Name:<input type=text name=name>

    email:<input type=text name=email>

    <input type=submit value=submit>
    </form>
    </body>
    </html>
    --------------------------------------------------------------------------------


    ok now that we have a form we can save the the form as ooooohh "form.html"
    next we will need a script to process the info
    don't be upset if this is to much for you right now
    this script uses alot of PERL funtion's
    and progrmming tricks

    code:--------------------------------------------------------------------------------
    sub parse_form {
    if ($ENV{'REQUEST_METHOD'} eq 'get') {
    @pairs = split(/&/,$ENV{'QUERY_STRING'});

    }

    elsif ($ENV{'REQUEST_METHOD'} eq 'post') {
    read (STDIN, $buffer ,$ENV{'CONTENT_LENGTH'});
    @pairs = split(/&/, $buffer);

    if ($ENV{'QUERY_STRING'}) {
    @getpairs = split(/&/, $ENV{'QUERY_STRING'});
    push(@pairs,@getpairs);

    }

    else {

    print("content-type: text/html\n\n");
    print("use post or get ");

    }

    foreach $pair (@pairs) {
    ($key, $value) = split(/=/, $pair);
    $key =~ tr/+/ /;
    $key =~ s/%(..)/pack("c", hex($1))/eg;
    $value =~ tr/+/ /;
    $value =~ s/%(..)/pack("c", hex($1))/eg;
    $value =~ s// /g;


    if ($formdata{$key}) {
    $formdata{$key} .= ", $value";

    }

    else {
    $formdata{$key} = $value;

    }
    }
    }
    1;
    --------------------------------------------------------------------------------

    alot of code I know but the good thing is once you have this code you can use it from site to site.So save this in a file called sub.lib with the permission of "0644". next lets finish this program.Next we will need to write a script that will display the infomation.take a deep breath
    its not as bad as the last code.

    code:--------------------------------------------------------------------------------
    #!/usr/local/bin/perl
    require "sub.lib"; #calls the file for use in this script
    &parse_form; #this calls the sub to decode the text

    print "Content-type: text/html\n\n";

    foreach $key (key %formdata) {
    print "You have Entered $formdata{$key} in the $key field";
    }

    #this print the information that was Entered and displays it
    --------------------------------------------------------------------------------

    well thats it for now.IF anything I hope this will push you into perl,or some other language.This information is not to take the place of a good book.I have studied PERL for 2 years and let me tell you that I have a long way to go.Perl is a big language not to mention the writer's of PERL is working with the writer's of PYTHON so its just going to get bigger and better...but nothing a lot of coffee and time wont fix.
    [glowpurple]A_420_hacker_24::.\"A man without a computer is just a man, a man with a computer is a Admin\" ... \"If its not 4:20 on your clock, it\'s time to change the time\"..:Quotations from Larry Wall:.
    \"I think you didn\'t get a reply because you used the terms \"correct\" and \"proper\", neither of which has much meaning in Perl culture. :-) \"
    [/glowpurple]

  2. #2
    Senior Member
    Join Date
    Jul 2002
    Posts
    154
    Just a couple of things to add. If you are using activeperl because you use windows, then you don't use the #!/usr/local/bin/perl line, this line just lets the script know where perl is located to access it. If you are using perl you'll probably using something like this, #! c:\Perl\bin\perl.exe and that is assuming that you installe perl into the default directory and your drive is called c:. And just one more thing, lots of people think that all cgi scripts are written in perl, well that isn't true, there are cgi scripts that are written in other languages though they usually require another program to run. Just something to let people know. Good tutorial 420, keep it up.

  3. #3
    Senior Member
    Join Date
    Aug 2002
    Posts
    651
    Good one 420. This will be useful to me since I am learning PERL "on the side" whenever I have time to play with it. It seems like it could prove to be a very useful tool to me. Thanks again for the tut.
    Opinions are like holes - everybody\'s got\'em.

    Smile

  4. #4
    Member
    Join Date
    Dec 2001
    Posts
    50
    Thanks ..I think I will do one on the "CGI.pm" or "Tk.pm" soon but we will see how I do with my time(school,work,music).

    Later
    [glowpurple]A_420_hacker_24::.\"A man without a computer is just a man, a man with a computer is a Admin\" ... \"If its not 4:20 on your clock, it\'s time to change the time\"..:Quotations from Larry Wall:.
    \"I think you didn\'t get a reply because you used the terms \"correct\" and \"proper\", neither of which has much meaning in Perl culture. :-) \"
    [/glowpurple]

  5. #5
    Senior Member
    Join Date
    Apr 2002
    Posts
    889
    Not to burst your bubble but it is usually good here to post original things and this info and tut can be had all over the Web. Want in depth info I'd suggest all read the related links threads posted at the bottom. Nope not giving points here after all even Bill as far as he got Gate$ knew how to write Hello World and market the crap out of it through excliusive contracts that were found to be anti competive and was GUILTY. First step in any language is the standby Hello World. Ok I know school just started "HELLO SCHOOL WORLD"
    I believe that one of the characteristics of the human race - possibly the one that is primarily responsible for its course of evolution - is that it has grown by creatively responding to failure.- Glen Seaborg

  6. #6
    Member
    Join Date
    Dec 2001
    Posts
    50
    this tut I wrote while i was in europe.

    Originally posted here by Palemoon
    Not to burst your bubble but it is usually good here to post original things and this info and tut can be had all over the Web.
    You can get any Infomation on the web. I wrote this to help peeps out and the help me out. I can understand If i just cut and pasted sombodys stuff but I didnt......................but your not bursting my bubble ... but why must you get all neg about this post.
    [glowpurple]A_420_hacker_24::.\"A man without a computer is just a man, a man with a computer is a Admin\" ... \"If its not 4:20 on your clock, it\'s time to change the time\"..:Quotations from Larry Wall:.
    \"I think you didn\'t get a reply because you used the terms \"correct\" and \"proper\", neither of which has much meaning in Perl culture. :-) \"
    [/glowpurple]

  7. #7
    Junior Member
    Join Date
    Sep 2002
    Posts
    28
    Not bad...
    Flash rocks!

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •