This file contains 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
#!/usr/bin/perl | |
# Дан хеш %h. Необходимо удалить из него лишние пары, у которых | |
# значения повторяются (т.е. только 1 такую пару оставить) наиболее | |
# эффективным методом. В хеше может быть миллион пар, так что | |
# приоритет – процессорное время. | |
use Modern::Perl; | |
# Ахтунг! Во всех вариантах не учитывается порядок следования ключей. |
This file contains 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
#!/usr/bin/perl | |
use Modern::Perl; | |
use Carp qw/confess/; | |
use constant ARRAY_SIZE => 1_000_000; | |
use constant ITER_N => 1_000; | |
# ------------------------------------------------------------------------------ | |
# Поиск индекса ближайшего к заданному элемента отсортированного массива. | |
# Возвращает индекс в массиве и количество шагов, которое понадобилось |
This file contains 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
#!/usr/bin/perl | |
# Напишите на Perl примитивный базовый класс MyApp::Accessor для | |
# использования в качестве базового класса для генерации ацессоров | |
# (методов которые сохраняют и отдают свойство объекта). Ацессоры должны | |
# работать настолько быстро, насколько это возможно в принципе. Какими | |
# технологиями/модулями, по вашему, лучше всего пользоваться в реальной | |
# разработке для создания ацессоров? | |
# P.S. Accessor – это примитивная функция, которая служит для доступа к | |
# свойству объекта извне. Т.е. $obj->property() – возвращает значение, |
This file contains 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
# | |
# Что произойдет с памятью в этом примере и почему? Как исправить положение? | |
# | |
while (1) { | |
my $a = {}; | |
$a->{func} = sub { | |
$a->{cnt}++; | |
}; | |
} | |
# Здесь счётчик ссылок на $a сохраняется в контексте func, поэтому |
This file contains 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
package AA; | |
sub func { print "AA\n" } | |
package BB; | |
use parent 'AA'; | |
sub func { print "BB\n"; shift->SUPER::func(@_); } | |
package CC; | |
use parent 'AA' | |
sub func { print "CC\n"; shift->SUPER::func(@_); } |
This file contains 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
#!/usr/bin/perl | |
# Написать функцию http_get ($host, $path, $query, $timeout), которая | |
# делает http запрос на адрес http://$host/$path?$query с таймаутом $timeout. | |
# Реализация http должна быть примитивной, то есть мы расчитываем на ответ | |
# HTTP 200 OK с content-length. | |
# $query передают в функцию хешом. | |
# А также написать асинхронную версию этой функции, которая (для простоты | |
# задания) отличается тем, что пока ждет ответа занимается заполнением | |
# какого-нить массива числами и выводит на экран сколько элементов успела |
This file contains 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
class lp_timer_t { | |
private: | |
timer_t timerid; | |
bool started; | |
void* data; | |
struct sigevent sev; | |
struct sigaction sa; | |
struct sigaction prev; | |
struct itimerspec its; |
This file contains 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
#include <arpa/inet.h> | |
#include <string> | |
struct cidr; | |
static bool cidr_match(const cidr& src, const cidr& net); | |
static bool addr2cidr(const char* addr, cidr& entry); | |
/* | |
src & net can be any of: | |
"IPv4" | |
"IPv6" |
This file contains 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
static bool match_here(const char* mask, const char* text); | |
static inline bool match_star(int c, const char* mask, const char* text) | |
{ | |
do { | |
if(match_here(mask, text)) | |
return true; | |
} while(*text != '\0' && (*text++ == c || c == '?')); | |
return false; |
This file contains 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
# | |
# http://perldoc.perl.org/CORE.html#OVERRIDING-CORE-FUNCTIONS | |
# > To override a built-in globally (that is, in all namespaces), you need to | |
# > import your function into the CORE::GLOBAL pseudo-namespace at compile | |
# > time: | |
# > | |
# > BEGIN { | |
# > *CORE::GLOBAL::hex = sub { | |
# > # ... your code here | |
# > }; |