This is a list of Perl Syntax/resources that generally we don't know or forget. And please, contribute with your own too.
## this will undef the array: $#array = -1 ; ## but @array = () is the usual. ## To use here-documents with codes, but without ## parse the code, you use '' as a delimiter of the <<EOF: print <<'CODE'; This $variable wont be parsed, and this data will br printed as it was. CODE ## To add some wild data in REGEXP, is common to see ## codes that filter the data before use it in REGEXP, ## but is much more easy to use \Q\E, instead of something ## like =~ s/[\.\\\/]/\\$1/s : my $find = 'http://www.domain.foo/' ; if ( $_ =~ /\Q$find\E/s ) { ... } ## The use \G to continue the search: while( $_ =~ /(.*)(<.*?>)/s ) { ... } ## And to get what was left by while (after the last <.*?>): my ($final) = ( $data =~ /\G(.*)$/s ) ; ## When we are looking on REGEXP for something that is after other, ## genreally "\n", we forget about the beggin of the string, ## that enables an occurrence that is not after "\n". ## Soo, to do this in only one REGEXP we use (?:^|\n) ## remove comments: $str =~ s/(?:^|\n)[ \t]*#//gs ; ## Use of substr(): substr($str , 0 , 0) = ">>" ; ## add data in the begin. substr($str , 0 , 1) = "ABC" ; ## replace the 1st char with a bigger + text. substr($str , -1 , 1) ; ## get the last char. substr($str , 0 , -1) ; ## get all the str, but leave the last char +out. ## Formatting padding: my $var = sprintf("%06d", 123) ; print "$var\n" ; ## print 000123 ## To generate an array: my @lyb = (a..z , A..Z , 0..9) ; ## The list: ## a b c d e f g h i j k l m n o p q r s t u v w x y z ## A B C D E F G H I J K L M N O P Q R S T U V W X Y Z ## 0 1 2 3 4 5 6 7 8 9 ## Creating an array with 10 elements 'A': my @l = ('A') x 10 ; ## creating an HASH with 2 arrays, ## one for the keys and other for the values: my %hash ; @hash{@keys} = @values ; ## Just to show to beginners the right way for ## concatenation: $str .= "abc" ; ## and not $str = $str . "abc" ; ## Declaration of my: if ( my $foo = <IO> ) { ## $foo is in this scope } open(my $fh , ">out") ; ## all in one line close($fh);

In reply to Perl Syntax/resources that we generally don't know or forget. by gmpassos

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.