SlideShare a Scribd company logo
1 of 76
Download to read offline
Фичи Perl 5.10
 на практике
5.10 RC1   17 ноября 2007
5.8.8      1 февраля 2006
5.8.6      27 ноября 2004
5.8.0      18 июля 2002
5.6.0      22 марта 2000
#!/usr/bin/perl5.10


> perl5.10 script.pl
#!/perl5.10/bin/perl


> perl5.10 script.pl
#!/usr/bin/perl


> perl script.pl
use feature
use feature qw(
    say
    switch
    state
);
use feature qw(
    say
    switch
    state
);

use feature quot;:5.10quot;;
use feature qw(
    say
    switch
    state
);

use feature quot;:5.10quot;;
use 5.10.0;
use feature qw(
    say
    switch
    state
);

use feature quot;:5.10quot;;
use v5.10.0;
#!/usr/bin/perl

use feature quot;***quot;;
#!/usr/bin/perl

use feature quot;sayquot;;
say quot;Perl 6?quot;;
#!/usr/bin/perl

use feature quot;sayquot;;
say quot;Perl 6?quot;;
no feature quot;sayquot;;
> perl ‐e 
  quot;use feature qw(say); say $$;quot;
> perl ‐e 
  quot;use feature qw(say); say $$;quot;


> perl ‐E quot;say $$;quot;
> perl ‐e 
  quot;use feature qw(say); say $$;quot;


> perl ‐E quot;say $$;quot;
Modern::Perl
#!/usr/bin/perl

use Modern::Perl;

say quot;Perl Tashkentquot;;
#!/usr/bin/perl

use Modern::Perl;
use utf8;

say quot;Perl Tashkentquot;;
//

defined‐or
my $a;
my $b = $a // 2;
say $b;           2
my $c = 0;
my $d = $c // 3;
say $d;           0

my $e = 0;
my $f = $e || 4;
say $f;           4
sub get_tagID {
    my $tag = shift;
    
    

    




    return $tagID;
}
sub get_tagID {
    my $tag = shift;
    
    # Попытка найти существующий

    




    return $tagID;
}
sub get_tagID {
    my $tag = shift;
    
    # Попытка найти существующий
    # Если нет, то добавить в базу
    




    return $tagID;
}
sub get_tagID {
    my $tag = shift;
    
    # Попытка найти существующий
    # Если нет, то добавить в базу
    # Узнать последний id




    return $tagID;
}
# Попытка найти существующий
my $sth = dbh‐>prepare(quot;
    select id from tags
    where tag = ?
quot;)
$sth‐>execute($tag);

my ($tagID) = 
    $sth‐>fetchrow_array();
$sth‐>finish();
# Если нет, то добавить в базу
unless ($tag_ID) {
    my $sth = dbh‐>prepare(quot;
        insert into tags
        (tag) values (?)
    quot;);
    $sth‐>execute($tag);
   
    . . . 
}
# Узнать последний id
unless ($tag_ID) {
    . . . 
    $sth = dbh‐>prepare(quot;
        select last_insert_id()
        from tags
    quot;);
    ($tagID) =
        $sth‐>fetchrow_array();
    $sth‐>finish();
}
# Узнать последний id
unless ($tag_ID) {
    . . . 
    $sth = dbh‐>prepare(quot;
        select last_insert_id()
        from tags
    quot;);
    ($tagID) =
        $sth‐>fetchrow_array();
    $sth‐>finish();
}
sub get_tagID {
    my $tag = shift;
    
    . . .
     $tagID //= new_tagID();

    return $tagID;
}
my $per_page = 
    $config{per_page} // 10;
my $_;
for (1..5) {
    my $_ = '*';
    print;
}               *****
$::_
for (1..5) {
    my $_ = '*';
    print $::_;
}               12345
our $_;
for (1..5) {
    our $_ = '*';
    print $::_;
}               *****
> perl5.10 ‐E 
  quot;say for 1..3quot;



> perl6 ‐e 
  quot;say for 1..3quot;
> perl5.10 ‐E     1
  quot;say for 1..3quot;   2
                   3

> perl6 ‐e        n
  quot;say for 1..3quot;   n
                   n
~~
~~
Smart matching
use feature 'say';

my $date 
    = 'Wed 13 May, 2009';
say 'Today' if $date ~~ /Wed/;
use feature 'switch';
use feature qw(switch say);

my $tag = 'perlrus08';
given ($tag) {
    when ('perlrus08') {
        say 'Yes';
    }
}
use feature qw(switch say);

my $tag = 'perluz1';
given ($tag) {
    when ('perlz1') {
        say 'Yes';
    }
    default {say 'No';}
}
when (123)

when ($value)

when (undef)

when ([2001..2100])

when (/d+/)
when ($_ > 0)

when (int)

when (int $_)

when (&test_the_value)

when (test_the_value($_))
given ('perlrus08') {
    when (/d+/) {
        say 'digits';
        continue;
    }
    when (/perl/i) {
        say 'Perl';
    }
}
when ($what)

      ==
when ($_ ~~ $what)
$left ~~ $right

    ==
$right ~~ $left
use feature 'state';
sub counter{
    state $value = 0;
    $value++;
    say $value;
}

counter();             1
counter();             2
counter();             3
Регулярные
выражения
Именованные буферы

my $date = 'Wed 13 May 2009';
$date =~ /
    (           w+  )    s+
    (           d+  )    s+
    (           w+  )    s+
    (           d{4})
/x;

say $1;                   Thu
say $4;                   2007
Именованные буферы

my $date = 'Wed 13 May 2009';
$date =~ /
    (?<wday>    w+  )    s+
    (?<day>     d+  )    s+
    (?<month>   w+  )    s+
    (?<year>    d{4})
/x;

say $+{wday};             Thu
say $+{year};             2007
Именованные буферы

my $date = 'Wed 13 May 2009';

$date =~ s/
        (?<year>d{4})
    /
        $+{year} + 1
    /xe;

say $date;        Wed 13 May 2009
my $code = 'my $value = 100; say $value;';

$code =~ s/
    my                          s*
    (?<variable>    $[a‐z]+)   s*
    =                           s*
    (?<value>       [^;]+   )   s*
    ;                           s*
    
    (?<other_code>.*?)
    
    (k<variable>)
    
    /$+{other_code}$+{value}/x;

say $code;                           say 100;
my $leap_years = '1992 1996 2004 2008';

$leap_years =~ m/
    (
        ?<year>    1    d{3}
    )
    s*
    (
        ?<year>    2    d{3}
    )
/x;

say $_ for @{$‐{year}};           1996
                                  2004
my $leap_years = '1992 1996 2004 2008';

$leap_years =~ m/
    (
        ?<year>    1    d{3}
    )
   




/gx;

say $_ for @{$‐{year}};           1992
my $leap_years = '1992 1996 2004 2008';

$leap_years =~ m/
    (
        ?<year>    1    d{3}   s*
    )+
   




/gx;

say $_ for @{$‐{year}};           1996
use feature 'say';

my $expr = '1 + (2 + (3 + (4 + 5) + 6))';

$expr =~ s/
    (
            (
                [^()]+
            )
            
        |
            (?1)
    )
    /say $1;/xge;
Posessive quantifiers

        ?+
        *+
        ++
    {min, max}+
/
  quot;
    (?:
         [^quot;]++
       |
         .
    )*+
  quot;
/x
(?| ...)
my $re = qr/
      (d{4})(dd)(dd)
   |
      (w+),s*(d{4}))
/x;

'20090513' =~ $re;
say quot;$1 . $2 . $3quot;;

'May, 2009' =~ $re;
say quot;$4 . $5quot;;
my $re = qr/
   (?|(d{4})(dd)(dd)
   |
      (w+),s*(d{4})))
/x;

'20090513' =~ $re;
say quot;$1 . $2 . $3quot;;

'May, 2009' =~ $re;
say quot;$1 . $2quot;;
g{N}
 gN
g{‐N}
k<named>
  ==
g{named}
K
v
h
V
H
R
R   (?>
           x0Dx0A?
        |
           [
              x0A‐x0C
              x85
              x{2028}
              x{2029}
           ]
     )
__END__

__DATA__
Андрей Шитов — 2007, 2009
andy@shitov.ru  |  http://shitov.ru

More Related Content

More from Andrew Shitov

Creating a compiler in Perl 6
Creating a compiler in Perl 6Creating a compiler in Perl 6
Creating a compiler in Perl 6Andrew Shitov
 
Fun with Raspberry PI (and Perl)
Fun with Raspberry PI (and Perl)Fun with Raspberry PI (and Perl)
Fun with Raspberry PI (and Perl)Andrew Shitov
 
Параллельные вычисления в Perl 6
Параллельные вычисления в Perl 6Параллельные вычисления в Perl 6
Параллельные вычисления в Perl 6Andrew Shitov
 
Perl 6 for Concurrency and Parallel Computing
Perl 6 for Concurrency and Parallel ComputingPerl 6 for Concurrency and Parallel Computing
Perl 6 for Concurrency and Parallel ComputingAndrew Shitov
 
The Joy of Smartmatch
The Joy of SmartmatchThe Joy of Smartmatch
The Joy of SmartmatchAndrew Shitov
 
Perl 7, the story of
Perl 7, the story ofPerl 7, the story of
Perl 7, the story ofAndrew Shitov
 
Язык программирования Go для Perl-программистов
Язык программирования Go для Perl-программистовЯзык программирования Go для Perl-программистов
Язык программирования Go для Perl-программистовAndrew Shitov
 
Как очистить массив
Как очистить массивКак очистить массив
Как очистить массивAndrew Shitov
 
What's new in Perl 5.14
What's new in Perl 5.14What's new in Perl 5.14
What's new in Perl 5.14Andrew Shitov
 
Что нового в Perl 5.14
Что нового в Perl 5.14Что нового в Perl 5.14
Что нового в Perl 5.14Andrew Shitov
 
Text in search queries with examples in Perl 6
Text in search queries with examples in Perl 6Text in search queries with examples in Perl 6
Text in search queries with examples in Perl 6Andrew Shitov
 
There's more than one way to empty it
There's more than one way to empty itThere's more than one way to empty it
There's more than one way to empty itAndrew Shitov
 
How to clean an array
How to clean an arrayHow to clean an array
How to clean an arrayAndrew Shitov
 

More from Andrew Shitov (20)

Perl6 one-liners
Perl6 one-linersPerl6 one-liners
Perl6 one-liners
 
Creating a compiler in Perl 6
Creating a compiler in Perl 6Creating a compiler in Perl 6
Creating a compiler in Perl 6
 
Fun with Raspberry PI (and Perl)
Fun with Raspberry PI (and Perl)Fun with Raspberry PI (and Perl)
Fun with Raspberry PI (and Perl)
 
Perl6 in-production
Perl6 in-productionPerl6 in-production
Perl6 in-production
 
Параллельные вычисления в Perl 6
Параллельные вычисления в Perl 6Параллельные вычисления в Perl 6
Параллельные вычисления в Perl 6
 
AllPerlBooks.com
AllPerlBooks.comAllPerlBooks.com
AllPerlBooks.com
 
Perl 6 for Concurrency and Parallel Computing
Perl 6 for Concurrency and Parallel ComputingPerl 6 for Concurrency and Parallel Computing
Perl 6 for Concurrency and Parallel Computing
 
The Joy of Smartmatch
The Joy of SmartmatchThe Joy of Smartmatch
The Joy of Smartmatch
 
YAPC::Europe 2013
YAPC::Europe 2013YAPC::Europe 2013
YAPC::Europe 2013
 
Perl 7, the story of
Perl 7, the story ofPerl 7, the story of
Perl 7, the story of
 
Язык программирования Go для Perl-программистов
Язык программирования Go для Perl-программистовЯзык программирования Go для Perl-программистов
Язык программирования Go для Perl-программистов
 
Как очистить массив
Как очистить массивКак очистить массив
Как очистить массив
 
What's new in Perl 5.14
What's new in Perl 5.14What's new in Perl 5.14
What's new in Perl 5.14
 
Что нового в Perl 5.14
Что нового в Perl 5.14Что нового в Perl 5.14
Что нового в Perl 5.14
 
Perl6 grammars
Perl6 grammarsPerl6 grammars
Perl6 grammars
 
Text in search queries with examples in Perl 6
Text in search queries with examples in Perl 6Text in search queries with examples in Perl 6
Text in search queries with examples in Perl 6
 
There's more than one way to empty it
There's more than one way to empty itThere's more than one way to empty it
There's more than one way to empty it
 
Perl 6 by example
Perl 6 by examplePerl 6 by example
Perl 6 by example
 
How to clean an array
How to clean an arrayHow to clean an array
How to clean an array
 
Perl 5.10 и 5.12
Perl 5.10 и 5.12Perl 5.10 и 5.12
Perl 5.10 и 5.12
 

‎Фичи 5.10 на практике‎