The path to learning in Perl

Source: Internet
Author: User
Tags define local



Perl comes from the Unix system and is currently rooted in Unix. Perl is a practical extract and report language (Practical Extraction and Report Language). The capital P means "Perl" refers to the programming language, while the lowercase p means "perl" refers to the interpreter that actually compiles and runs the program. Its creator is Larry. The purpose of his creation is to be able to program quickly like Sell or awk, to have advanced functions such as grep, cut, sort, and sed, and to fill low-level languages such as c / c ++ or programming languages and advanced The gap between languages. Perl is suitable for word processing, business process management, and not for "closed" programs (such as your secret algorithm that others cannot see you).

      First, let's analyze a section of perl code I wrote

      #! / usr / bin / perl -w # This is the absolute path of your perl execution program

    use strict; #Introduce the strict package which contains some coding rules under the package. If the code does not meet the specifications, a warning will be issued

    use warnings;

    use Getopt :: Long;

    use Data :: Dumper;

    use FindBin qw ($ Bin $ Script);

    use File :: Basename qw (basename dirname);

    my $ BEGIN_TIME = time ();

    my $ version = "1.0.0";


# GetOptions

my ($ fIn, $ fOut); # This is to define local variables


GetOptions (

                "help |?" => \ & USAGE,

                "o: s" => \ $ fOut,

                "i: s" => \ $ fIn,

                ) or & USAGE;

& USAGE unless ($ fIn and $ fOut); #This is to get the command line parameters


open (IN, $ fIn) or die $!; # Whether opening a file is successful, if it is not successful, it will output the error provided by the system ($!)

open (OUT, "> $ fOut") or die $ !;


# $ / = ">"; # This is a delimiter, which separates the characters of the text by ">" and gives you the corresponding variable


while (<IN>) {# <IN> is when you open the file in a normal state and no exception occurs

    chomp;

    next if (/ $ /);

    my @ lines = split / \ n /, $ _;

}


close IN;

close OUT;



print STDOUT "\ nDone. Total elapsed time:", time ()-$ BEGIN_TIME, "s \ n";




# sub function


#Define your function as C language, but do not need to be declared like C

sub ABSOLUTE_DIR {# $ pavfile = & ABSOLUTE_DIR ($ pavfile);

    my $ cur_dir = `pwd`; chomp ($ cur_dir);

    my ($ in) [email protected] _;

    my $ return = "";

    if (-f $ in) {

        my $ dir = dirname ($ in);

        my $ file = basename ($ in);

        chdir $ dir; $ dir = `pwd`; chomp $ dir;

        $ return = "$ dir / $ file";

    } elsif (-d $ in) {

        chdir $ in; $ return = `pwd`; chomp $ return;

    } else {

        warn "Warning just for file and dir \ n";

        exit;

    }

    chdir $ cur_dir;

    return $ return;

}




sub max {# & max (lists or arry);

    #Find the maximum value in the list

    my $ max = shift;

    my $ temp;

    while (@_) {

        $ temp = shift;

        $ max = $ max> $ temp? $ max: $ temp;

    }

    return $ max;

}




sub min {# & min (lists or arry);

    #Find the minimum value in the list

    my $ min = shift;

    my $ temp;

    while (@_) {

        $ temp = shift;

        $ min = $ min <$ temp? $ min: $ temp;

    }

    return $ min;

}




sub revcom () {# & revcom ($ ref_seq);

    #Get the reverse complement of the string sequence and return it as a string. ATTCCC-> GGGAAT

    my $ seq = shift;

    $ seq = ~ tr / ATCGatcg / TAGCtagc /;

    $ seq = reverse $ seq;

    return uc $ seq;

}




sub GetTime {

    my ($ sec, $ min, $ hour, $ day, $ mon, $ year, $ wday, $ yday, $ isdst) = localtime (time ());

    return sprintf ("% 4d-% 02d-% 02d% 02d:% 02d:% 02d", $ year + 1900, $ mon + 1, $ day, $ hour, $ min, $ sec);

}



sub USAGE {

    my $ usage = << "USAGE";

 ProgramName:

     Version: $ version

     Contact: Simon Young <yangxh \ @ biomarker.com.cn>

Program Date: 2012.07.02

      Modify:

 Description: This program is used to ......

       Usage:

        Options:

        -i <file> input file, xxx format, forced


        -o <file> output file, optional


        -h help


USAGE

    print $ usage;

    exit;

}


This article is from the "Listen to stop very" blog, please be sure to keep this source http://drxin.blog.51cto.com/10182098/1678158



The path to learning in Perl


Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.