This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| use v5.10; | |
| use Sub::Name; | |
| my $name1 = subname name1 => sub { | |
| say( (caller(0))[3] ); | |
| say( (caller(1))[3] ); | |
| say( (caller(2))[3] ); | |
| }; | |
| my $name2 = subname name2 => sub { |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| # Опасный код!!! | |
| my @ar = qw/h e l l o/; | |
| foreach (@ar) { | |
| do_something(); | |
| say $_; | |
| } | |
| # Никогда не используйте переменную "$_" | |
| # если в цикле вызывается какая-то внешняя функция. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| $text =~ m/$re/; | |
| # Такая запись тоже является опасной, поскольку | |
| # переменная $re может содержать пустую строку | |
| # Лучше записывайте так | |
| $text =~ m/(?:$re)/; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| if ( "test1" =~ /t1/ ) { | |
| say "YES"; | |
| } else { | |
| say "NO" | |
| } | |
| if ( "test2" =~ // ) { | |
| say "YES"; | |
| } else { | |
| say "NO" |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| use everywhere 'MooseX::Declare', | |
| matching => '^MyApp', | |
| use_here => 0; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| perl -e 'while (<ARGV>){ … }' f1 f2 f3 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| $content = do {local(@ARGV, $/) = $f; <ARGV>} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #---------------------------------------------------------# | |
| # Локализация пакетных переменных | |
| our $l = 5; | |
| { | |
| local $l = 4; | |
| say $l | |
| }; | |
| say $l; | |
| #4 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| # строка, которая в числовом контексте соответствуют | |
| # нулю, а в логическом - истине | |
| "0 but true" | |
| perl -wE 'say "0 but true"+5' | |
| #Получим 5 без никаких предупреждений | |
| # Но стоит изменить хоть один символ в заветной строке | |
| perl -wE 'say "0 but true!"+5' | |
| 'Argument "0 but true!" isn't numeric in addition (+) at -e line 1. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| my $rows_affected = $dbh->do('UPDATE ... WHERE') or die; | |
| my $rows_affected = $sth->execute(...) or die; # Для не "select" запросов. | |
| # $rows_affected может содержать ноль, но быть истинным. | |
| # Значит в Perl может быть истинное нулевое значение | |
| # И вот несколько вариантов при использовании | |
| # которых Perl не сыпет предупреждения | |
| "0.0" # Десятичная нотация. Я обычно использую именно этот вариант |