Hello & Welcome to our community. Is this your first visit? Register
Follow us on
Follow us on Facebook Follow us on Twitter Watch us on YouTube


MMOCoin

Likes Likes:  0
Results 1 to 1 of 1
  1. #1
    Premium
    G4M3R's Avatar
    Join Date
    Mar 2013
    Location
    LEGEN....wait for it....DARY!
    Posts
    33
    Post Thanks / Like
    Rep Power
    12
    Reputation
    38

    Arrow >>Perl Basics 2<<


    Register to remove this ad
    Perl Statements

    A Perl script consists of a series of statements and comments. Each statement is a command that is recognized by the Perl interpreter and executed. Statements are terminated by the semicolon character (. They are also usually separated by a newline character to enhance readability.A comment begins with the # sign and can appear anywhere. Everything from the # to the end of the line is ignored by the Perl interpreter. Commonly used for human-readable notes.
    Some Statements

    $sum = 2 + 2; # this is a statement$f = <STDIN>; $g = $f++; # these are two statements$g = $f / $sum; # this is one statement, spread across 3 lines
    The Perl interpreter will start at the top of the script and execute all the statements, in order from top to bottom, until it reaches the end of the script. This execution order can be modified by loops and control structures.
    Blocks

    It is common to group statements into blocks using curly braces. You can execute the entire block conditionally, or turn it into a subroutine that can be called from many different places.Example blocks:

    { # block starts my $EcoRI = 'GAATTC'; my $sequence = <STDIN>; print "Sequence contains an EcoRI site" if $sequence=~/$EcoRI/;} # block endsmy $sequence2 = <STDIN>;if (length($sequence) < 100) { # another block starts print "Sequence is too small. Throw it back\n"; exit 0;} # and endsforeach $sequence (@sequences) { # another block print "sequence length = ",length($sequence),"\n";}
    Labeled Blocks

    You can also attach a label to a block of statements like this:

    READ_SEQUENCE: { $sequence = <STDIN> print "length = ",length($sequence),"\n";}

    Literals

    Literals are constant values that you embed directly in the program code. Perl supports both string literals and numeric literals.
    String Literals

    String literals are enclosed by single quotes (') or double quotes ("):


    'The quality of mercy is not strained.'; # a single-quoted string"The quality of mercy is not strained."; # a double-quoted string
    The difference between single and double-quoted strings is that variables and certain special escape codes are interpolated into double quoted strings, but not in single-quoted ones. Here are some escape codes:
    \n New line
    \t Tab
    \r Carriage return
    \f Form feed
    \a Ring bell
    \040 Octal character (octal 040 is the space character)
    \0x2a Hexadecimal character (hex 2A is the "*" character)
    \cA Control character (This is the ^A character)
    \u Uppercase next character
    \l Lowercase next character
    \U Uppercase everything until \E
    \L Lowercase everything until \E
    \Q Quote non-word characters until \E
    \E End \U, \L or \Q operation

    "Here goes\n\tnothing!"; # evaluates to: # Here goes # nothing!'Here goes\n\tnothing!'; # evaluates to: # Here goes\n\tnothing!"Here goes \unothing!"; # evaluates to: # Here goes Nothing!"Here \Ugoes nothing\E"; # evaluates to: # Here GOES NOTHING!"Alert! \a\a\a"; # evaluates to: # Alert! (ding! ding! ding!)
    Putting backslashes in strings is a problem because they get interpreted as escape sequences. To inclue a literal backslash in a string, double it:


    "My file is in C:\\Program Files\\Accessories\\wordpad.exe"; # evaluates to: C:\Program Files\Accessories\wordpad.exe
    Put a backslash in front of a quote character in order to make the quote character part of the string:


    "She cried \"Oh dear! The parakeet has flown the coop!\""; # evaluates to: She cried "Oh dear! The parakeet has flown the coop!"
    Numeric Literals



    You can refer to numeric values using integers, floating point numbers, scientific notation, hexadecimal notation, and octal. With some help from the Math::Complex module, you can refer to complex numbers as well:


    123; # an integer1.23; # a floating point number -1.23; # a negative floating point number 1_000_000; # you can use _ to improve readability 1.23E45; # scientific notation 0x7b; # hexadecimal notation (decimal 123) 0173; # octal notation (decimal 123)use Math::Complex; # bring in the Math::Complex module 12+3*i; # complex number 12 + 3i
    Backtick Strings

    You can also enclose a string in backtics (`). This has the unusual property of executing whatever is inside the string as a Unix system command, and returning its output:


    `ls -l`;# evaluates to a string containing the output of running the# ls -l command
    Lists

    The last type of literal that Perl recognizes is the list, which is multiple values strung together using the comma operator (,) and enclosed by parentheses. Lists are closely related to arrays, which we talk about later.


    ('one', 'two', 'three', 1, 2, 3, 4.2); # this is 7-member list contains a mixure of strings, integers # and floats


    Operators

    Perl has numerous operators (over 50 of them!) that perform operations on string and numberic values. Some operators will be familiar from algebra (like "+", to add two numbers together), while others are more esoteric (like the "." string concatenation operator).
    Numeric & String Operators

    The "." operator acts on strings. The "!" operator acts on strings and numbers. The rest act on numbers.


    Operator Description Example Result
    . String concatenate 'Teddy' . 'Bear' TeddyBear
    = Assignment $a = 'Teddy' $a variable contains 'Teddy'
    + Addition 3+2 5
    - Subtraction 3-2 1
    - Negation -2 -2
    ! Not !1 0
    * Multiplication 3*2 6
    / Division 3/2 1.5
    % Modulus 3%2 1
    ** Exponentiation 3**2 9
    <FILEHANDLE> File input <STDIN> Read a line of input from standard input
    >> Right bit shift 3>>2 0 (binary 11>>2=00)
    << Left bit shift 3<<2 12 (binary 11<<2=1100)
    | Bitwise OR 3|2 3 (binary 11|10=11
    & Bitwise AND 3&2 2 (binary 11&10=10
    ^ Bitwise XOR 3^2 1 (binary 11^10=01
    Operator Precedence

    When you have an expression that contains several operators, they are evaluated in an order determined by their precedence. The precedence of the mathematical operators follows the rules of arithmetic. Others follow a precedence that usually does what you think they should do. If uncertain, use parentheses to force precedence:



    2+3*4; # evaluates to 14, multiplication has precedence over addition(2+3)*4; # evaluates to 20, parentheses force the precedence
    Logical Operators

    These operators compare strings or numbers, returning TRUE or FALSE:


    Numeric Comparison String Comparison
    3 == 2 equal to 'Teddy' eq 'Bear' equal to
    3 != 2 not equal to 'Teddy' ne 'Bear' not equal to
    3 < 2 less than 'Teddy' lt 'Bear' less than
    3 > 2 greater than 'Teddy' gt 'Bear' greater than
    3 <= 2 less or equal 'Teddy' le 'Bear' less than or equal
    3 >= 2 greater than or equal 'Teddy' ge 'Bear' greater than or equal
    3 <=> 2 compare 'Teddy' cmp 'Bear' compare
    'Teddy' =~ /Bear/ pattern match

    The <=> and cmp operators return:

    • -1 if the left side is less than the right side
    • 0 if the left side equals the right side
    • +1 if the left side is greater than the right side

    File Operators

    Perl has special file operators that can be used to query the file system. These operators generally return TRUE or FALSE.
    Example:


    print "Is a directory!\n" if -d '/usr/home';print "File exists!\n" if -e '/usr/home/lstein/test.txt';print "File is plain text!\n" if -T '/usr/home/lstein/test.txt';
    There are many of these operators. Here are some of the most useful ones:
    -e filename file exists
    -r filename file is readable
    -w filename file is writable
    -x filename file is executable
    -z filename file has zero size
    -s filename file has nonzero size (returns size)
    -d filename file is a directory
    -T filename file is a text file
    -B filename file is a binary file
    -M filename age of file in days since script launched
    -A filename same for access time







    › See More: >>Perl Basics 2<<
    Last edited by G4M3R; 23-03-13 at 06:39 PM.



  2. Related Threads - Scroll Down after related threads if you are only interested to view replies for above post/thread

 

 

Visitors found this page by searching for:

Nobody landed on this page from a search engine, yet!
SEO Blog

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
All times are GMT -5. The time now is 02:49 PM.
Powered by vBulletin® Copyright ©2000-2024, Jelsoft Enterprises Ltd.
See More links by ForumSetup.net. Feedback Buttons provided by Advanced Post Thanks / Like (Lite) - vBulletin Mods & Addons Copyright © 2024 DragonByte Technologies Ltd.
vBulletin Licensed to: MMOPro.org