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);

Replies are listed 'Best First'.
•Re: Perl Syntax that we generally don't know.
by merlyn (Sage) on Jan 17, 2004 at 23:35 UTC
    $#array = -1
    I prefer @array = () for far less magic understanding needed.
    if ( $_ =~ /\Q$find\E/s ) { ... }
    You can leave off the \E. And that $_ =~ would have me puzzled for a minute or two, being very non-idiomatic. Worth leaving off.
    my ($final) = ( $data =~ /\G(.*)$/s ) ;
    You should add /c and test the match, otherwise you'll mess up the outer loop if the match fails.
    substr($str , 0 , 0) = ">>"
    I prefer either $str = "$str>>" or $str =~ s/^/>>/ for that. substr gives me gas, sometimes.
    my @lyb = (a..z , A..Z , 0..9) ;
    That breaks under use strict, and if the begin or end point is one of the magic single-character syntax things like "q" or "s" or "y". Best to quote the begin/end points.

    -- Randal L. Schwartz, Perl hacker
    Be sure to read my standard disclaimer if this is a reply.

      I prefer:
      undef(@array);
      It says exactly what it does.
Re: Perl Syntax that we generally don't know or forget.
by rcaputo (Chaplain) on Jan 18, 2004 at 03:04 UTC

    Lvalue substr() is my special friend. Did you know you can bind regular expressions and transliterations to them?

    my $x = "x" x 50; substr($x, 12, 34) =~ tr[x][y];

    Foreach's iterator variable is an alias for an item in the list being iterated through. This works with values() and hash slices, too.

    my %x = (a=>1, b=>2, c=>3); $_ *= 2 foreach values %x;

    I'm so, so easily amused. :)

    -- Rocco Caputo - rcaputo@pobox.com - poe.perl.org

      No! Don't stop there! You can also use the lvalue bindings of argument passing! Three nested lvalue usages:
      my $value = "abcde"; sub attack { $_[0] = "x" } attack($_) for substr($value, 2, 1); print "$value\n"; # prints "abxde\n"
      And if I understood lvalue subroutines better, I could probably write "attack($_) = 'x'" instead, for a fourth layer. {grin}

      -- Randal L. Schwartz, Perl hacker
      Be sure to read my standard disclaimer if this is a reply.

        Like this?. Or is there something I'm missing here..
        sub attack :lvalue { substr($_[0], 2, 1) ; }

        -Lee

        "To be civilized is to deny one's nature."

      Wow, I didn't know that. You can even $a="abcdef"; substr($a,2,-2) x= 3; print $a,$/; that prints abcdcdcdef. This is amazing.

      And you can buy 2 cats as simple as: $_="I have 2 dogs and 9 cats.\n"; /(\d+)\s+cat/ or die; substr($_,$-[1],$+[1]-$-[1])+= 2; print; which prints `I have 2 dogs and 11 cats.'.

      See also Re: cut of first char of a string.

Re: Perl Syntax/resources that we generally don't know or forget.
by Abigail-II (Bishop) on Jan 18, 2004 at 18:57 UTC
    ## 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
    Seems that you "forgot" or "don't know" something as well, namely the value of /m. Furthermore, it seems you want a .* after the # as well, but not in combination with /s!

    I'd write the regex as:

    $str =~ s/^[ \t]*#.*\n//gm;

    Abigail

    A reply falls below the community's threshold of quality. You may see it by logging in.
Re: Perl Syntax that we generally don't know or forget.
by Anonymous Monk on Jan 18, 2004 at 04:38 UTC
    ## remove comments: $str =~ s/(?:^|\n)[ \t]*#//gs ;
    The juxtaposition leaves one to wonder, is the double ## style meant to be a comment resistant to removal?
      I put this just as an example of use of (?:^|foo).

      Actually the code to remove a comment line should be:

      $str =~ s/(?:^|\n)[ \t]*#[^\r\n]*//gs ;
      Thanks for the advice. ;-P

      Graciliano M. P.
      "Creativity is the expression of the liberty".

Re: Perl Syntax that we generally don't know or forget.
by Anonymous Monk on Jan 18, 2004 at 00:50 UTC
Re: Perl Syntax/resources that we generally don't know or forget.
by Anonymous Monk on Jan 18, 2004 at 12:00 UTC
    list of Perl Syntax/resources that generally we don't know or forget

    If you don't know, how can you list it?

    Seriously, though, your list is just a collection of badly digested unrelated Perl idioms (and merlyn has already pointed out the problems).

    It is a fat piece of cargo cult. If your purpose was to achieve fame and fortune with this node, please try something else. Thanks.

      I never say that we should use this resources. They are only things that is not common to use, or things that the Perl programmers only discover after some time.

      And about:

      If you don't know, how can you list it?

      Just a "Duhhh" for you!

      Graciliano M. P.
      "Creativity is the expression of the liberty".