perl – 我应该如何使用Mojo :: UserAgent处理HTML META标签?

前端之家收集整理的这篇文章主要介绍了perl – 我应该如何使用Mojo :: UserAgent处理HTML META标签?前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我必须使用一些配置错误的Web服务器,因此我开始处理 HTML标记以将信息反馈回Web用户代理对象.我在 Mojolicious尝试了各种各样的方法,并决定在响应中寻找“完成”事件.我的目标是使其对代码的其余部分几乎不可见,因此进程甚至不知道发生了这种情况.

尽管如此,这并不适合我,因为我无法完全放下手指.除了process_Meta_options中的特定代码之外,还有一种更为MojolicIoUs的方法吗?例如,Mojo::UserAgent get() with userdefined callback使用read事件,但我倾向于认为这可能会干扰事情.或者我可能只是过度思考它.

use v5.20;

use feature qw(signatures);
no warnings qw(experimental::signatures);

use Data::Dumper;
use Mojo::UserAgent;

my $ua = Mojo::UserAgent->new;

my $tx = $ua->build_tx( GET => 'http://blogs.perl.org' ); 

$tx->res->on(
    finish => \&process_Meta_options
    );

$tx = $ua->start( $tx );
say "At end,charset is ",$tx->res->content->charset;

sub process_Meta_options ( $res ) {
    $res
        ->dom
        ->find( 'head Meta[charset]' )  # HTML 5
        ->map( sub {
            my $content_type = $res->headers->header( 'Content-type' );
            return unless my $Meta_charset = $_->{charset};
            $content_type =~ s/;.*//;
            $res->headers->header( 'Content-type',"$content_type; charset=$_->{charset}" );
            } );
    }

解决方法

我认为答案正是我提出的.我没有发现任何我更喜欢的东西.

use v5.20;

use feature qw(signatures);
no warnings qw(experimental::signatures);

use Data::Dumper;
use Mojo::UserAgent;

my $ua = Mojo::UserAgent->new;

my $tx = $ua->build_tx( GET => 'http://blogs.perl.org' ); 

$tx->res->on(
    finish => \&process_Meta_options
    );

$tx = $ua->start( $tx );
say "At end,"$content_type; charset=$_->{charset}" );
            } );
    }

猜你在找的Perl相关文章