sub node { shift->tree->[0] } # sub parse { shift->_delegate(parse => shift) } # sub _tag { shift->new->tree(shift)->xml(shift) }
sub tree { shift->_delegate(tree => @_) } # .. _delegate(tree => [], deep => 5) _delegate(tree => [], 5, 0) sub log { shift->emit('message', lc shift, @_) }
sub _instance { ref $_[0] ? $_[0] : $_[0]->singleton } # sub find { $_[0]->_collect(@{$_[0]->_css->select($_[1])}) }
sub at { my $self = shift; return undef unless my $result = $self->_css->select_one(@_); return _tag($self, $result, $self->xml); }
return '' if (my $tree = $self->tree)->[0] ne 'tag'; return $tree->[1] unless $type; # return if !(my $expires = delete $session->{expires}) && $expiration;
package Cat; use Mojo::Base 'Mojo::EventEmitter'; # Emit events sub poke { my $self = shift; $self->emit(roar => 3); } package main; # Subscribe to events my $tiger = Cat->new; $tiger->on(roar => sub { my ($tiger, $times) = @_; say 'RAWR!' for 1 .. $times; }); $tiger->poke;
sub once { my ($self, $name, $cb) = @_; weaken $self; my $wrapper; $wrapper = sub { $self->unsubscribe($name => $wrapper); $cb->(@_); }; $self->on($name => $wrapper); weaken $wrapper; return $wrapper; } # sub timeout { my $self = shift; # weaken $self; $self->{timer} = $reactor->timer($timeout => sub { $self->emit_safe('timeout')->close }); return $self; }
$_ eq $child ? last : $i++ for @$parent[$i .. $#$parent];
s/\%20/\+/g for $name, $value;
my $params = $self->{params} = []; return $params unless length $str;
if ($params->[$i] eq $name) { splice @$params, $i, 2 } else { $i += 2 }
package Foo; use Mojo::Base -base; has [qw(kept_alive local_address original_remote_address remote_port)]; # has req => sub { Mojo::Message::Request->new }; has max => 4; # has str => 'Hi!'; has list => sub { [] }; # has dict => sub { {} }; # has hint => <<EOF; # See 'APPLICATION help COMMAND' for more information on a specific command. EOF # my $Foo = Foo->new({kept_alive =>$kept_alive, local_address => $local_address});
use Carp 'croak'; sub server_write { croak 'Method "server_write" not implemented by subclass' }
use constant { PC_BASE => 36, PC_TMIN => 1, PC_TMAX => 26 }; use constant PC_BASE => 36;
die qq{Unknown command "$name", maybe you need to install it?\n} unless $module; # sub quote { my $str = shift; $str =~ s/(["\\])/\\$1/g; return qq{"$str"}; }
my $options = { domain => $self->cookie_domain, expires => $session->{expires}, httponly => 1, path => $self->cookie_path, secure => $self->secure }; $c->signed_cookie($self->cookie_name, $value, $options);
my $new; unless ($new = $self->_class($old, $field)) { return !!defined $new } # if (my $code = ($tx->req->error // {})->{advice}) { $res->code($code) } # my $stash = {}; if (my $sub = $tx->can('stash')) { ($stash, $tx) = ($tx->$sub, $tx->tx) }
$self->plugin($_) for qw(HeaderCondition DefaultHelpers TagHelpers EPLRenderer EPRenderer); # my %NORMALCASE = map { lc($_) => $_ } ( qw(Accept Accept-Charset Accept-Encoding Accept-Language Accept-Ranges), qw(Allow Authorization Cache-Control Connection Content-Disposition), # ... ); # for my $name (qw(app flash param stash session url_for validation)) { $app->helper($name => sub { shift->$name(@_) }); }
return $self->emit(read => $chunk) unless $self->is_compressed && $self->auto_relax; # return unless (my $class = ref $self || $self) && $attrs; # return unless $self->cleanup && defined(my $path = $self->path);
warn qq/Problem loading URL "@{[$tx->req->url->to_abs]}". ($err->{message})\n/ if $err && !$err->{code}; # $app->helper($_ => $self->can("_$_")) for qw(accepts content content_for csrf_token current_route delay), qw(inactivity_timeout is_fresh url_with); # return $self->error( {message => 'Maximum buffer size exceeded', advice => 400}) if $self->content->is_limit_exceeded;
eval "use Mojolicious::Lite; 1" or die $@; # use constant IPV6 => $ENV{MOJO_NO_IPV6} ? 0 : eval 'use IO::Socket::IP 0.20 (); 1';
sub monkey_patch { my ($class, %patch) = @_; no strict 'refs'; # no warnings 'redefine'; # , *{"${class}::$_"} = $patch{$_} for keys %patch; }
package ojo; # sub import { # monkey_patch $caller, # ... g => sub { _request($ua, 'GET', @_) }, # ... x => sub { Mojo::DOM->new(@_) }; # , perl -Mojo -E 'say g("mojolicio.us")->dom->at("title")->text' # habrahabr.ru perl -Mojo -C -E 'g("habrahabr.ru")->dom->find("a.post_title")->each(sub{ say $_->text })'
package Mojo::Collection; # sub each { my ($self, $cb) = @_; return @$self unless $cb; my $i = 1; $_->$cb($i++) for @$self; return $self; } # $dom->find('p[id]')->each(sub { say $_->{id} });
my $timers = $self->{timers} //= {}; my $id; do { $id = md5_sum('t' . steady_time . rand 999) } while $timers->{$id};
} # New socket else {
sub reset { delete @{shift()}{qw(io poll timers)} } # sub get { (shift->{cache} || {})->{shift()} }
package Mojo::Collection; use Mojo::Base -strict; use overload bool => sub { !!@{shift()} }, '""' => sub { shift->join("\n") }, fallback => 1; # my $collection = Mojo::Collection->new(qw(just works)); say $collection; say "empty" unless $collection;
$self->_finish($_, 1) for keys %{$self->{connections} || {}};
my $stream = $self->_loop($c->{nb})->stream($id)->timeout($self->inactivity_timeout);
return undef if $self->tree->[0] eq 'root'; # return unless defined $lines->[0][$num - 1]; # sub foo { return undef } if (my @x = foo()) { print "oops, we think we got a result"; } # sub foo { return } %x = ('foo' => foo(), 'bar' => 'baz'); if (!exists $x{'bar'}) { print "oops, bar became a value, not a key"; }
sub has_data { !!keys %{shift->input} }
Source: https://habr.com/ru/post/233991/
All Articles