如何打印Perl程序的进程号

$ perl -e 'print $$, "\n"' # just use $$

参考:

http://perldoc.perl.org/perlvar.html


Perl

这篇文章: 有用 无用
创建 2017-06-12 02:07:20 / 更新 2017-06-12 02:10:45

Mojo::UserAgent下载大文件的时候,如何打印下载进度

利用 Mojo::Message

#!/usr/bin/env perl
use feature qw(say);
use warnings;

use Mojo::UserAgent;

my $url = "https://cpan.metacpan.org/authors/id/S/SR/SRI/Mojolicious-7.30.tar.gz";
my $ua  = Mojo::UserAgent->new();
my $tx  = $ua->build_tx(GET => $url);
$tx->res->on(progress => sub {
    my $msg = shift;
    return unless my $len = $msg->headers->content_length;
    my $size = $msg->content->progress;
    say 'Progress: ', $size == $len ? 100 : int($size / ($len / 100)), '%';
});
$ua->start($tx);
say($tx->res->code, ' ', $tx->res->message);

利用 Mojo::Content

#!/usr/bin/env perl

use 5.12.0;
use warnings;

use Mojo::UserAgent;

my $url = "https://cpan.metacpan.org/authors/id/S/SR/SRI/Mojolicious-7.30.tar.gz";
my $ua  = Mojo::UserAgent->new();
my $tx  = $ua->build_tx(GET => $url);

$tx->res->content->on(read => sub {
    my $content = shift;
    return unless my $len = $content->headers->content_length;
    my $size = $content->progress;
    say 'Progress: ', $size == $len ? 100 : int($size / ($len / 100)), '%';
});
$ua->start($tx);

say($tx->res->code, ' ', $tx->res->message);

参考:

  1. http://stackoverflow.com/questions/10086592/mojouseragent-get-with-userdefined-callback
  2. https://metacpan.org/pod/Mojo::UserAgent#build_tx
  3. https://metacpan.org/pod/Mojo::Message#progress
  4. https://metacpan.org/pod/Mojo::Content#read

Perl

这篇文章: 有用 无用
创建 2017-04-16 05:25:46 / 更新 2017-04-16 05:25:46

在Perl中捕捉和处理 Signals

通过 %SIG Hash

use strict;
use warnings;

$SIG{INT} = sub {die "Caught a sigint $!"};

sleep(100);

如果运行这个Perl程序,然后再按control-c,就会发送 SIGINT 给Perl程序,然后 $SIG{INT} 对应的方法会捕捉到 SIGINT 信号

通过 sigtrap 指令

sigtrap 是用来更方便的维护 %SIG 的指令

sigtrap 把信号分为3组:

  • normal-signals(HUP, PIPE, INT and TERM)

  • error-signals(ABRT, BUS, EMT, FPE, ILL, QUIT, SEGV, SYS and TRAP)

  • old-interface-signals(ABRT, BUS, EMT, FPE, ILL, PIPE, QUIT, SEGV, SYS, TERM and TRAP)

例如:

use strict;
use warnings;
use sigtrap qw/die normal-signals/;

sleep(100);


use strict;
use warnings;
use sigtrap qw/handler signal_handler normal-signals/;

sub signal_handler {
    die "Caught a signal $!";
}

sleep(100);

关于 signal

The default signal for kill is TERM

control+c will send interrupt signal(SIGINT) to program

参考:

  1. http://perltricks.com/article/37/2013/8/18/Catch-and-Handle-Signals-in-Perl/
  2. https://superuser.com/questions/107543/bash-man-page-kill-pid-vs-kill-9-pid
  3. https://metacpan.org/pod/sigtrap

Perl

这篇文章: 有用 无用
创建 2017-04-16 03:41:41 / 更新 2017-04-16 05:26:18

PostgreSQL的一些基本操作

$ psql database_name -U myuser -h myhost # connect databse

\connect database_name # switch database

\du # list database roles, A role can be thought of as either a database user, or a group of database users, depending on how the role is set up.

\l # list all databases

\d # list all visible tables, views, sequences and foreign tables

\dt # list all tables in the current database

\dp # Lists tables, views and sequences with their associated access privileges

\d+ table_name # describe table structure

\q # exit psql

\x # Sets or toggles expanded table formatting mode. Like MySQL "\G". 

psql -c '\x' -c 'SELECT * FROM foo;'

psql <<EOF
\x
SELECT * FROM foo;
EOF

\password postgres # change password for postgres

参考:


PostgreSQL

这篇文章: 有用 无用
创建 2017-03-16 02:42:47 / 更新 2017-04-26 22:01:01

ProxyChains

使用Linux的时候经常需要下载国外的程序包,有的时候非常慢,有的时候资源被屏蔽,ProxyChains可以完美解决这个问题,当然前提是你得有一个代理

给ProxyChains配置代理:

$ cat .proxychains/proxychains.conf 
[ProxyList]
socks5 127.0.0.1 7090

怎么使用呢? 很简单, 原始命令前面添加proxychains即可:

$ proxychains google-cloud-sdk/install.sh
$ proxychains pip install scrapy
$ proxychains /opt/google/chrome/chrome

翻墙

这篇文章: 有用 无用
创建 2017-02-23 03:28:06 / 更新 2017-02-23 04:27:16