Mojo::ByteStream

作者: JSON_NULL | 来源:发表于2017-09-28 17:02 被阅读8次

    简介

    use Mojo::ByteStream;
    
    # Manipulate bytestream
    my $stream = Mojo::ByteStream->new('foo_bar_baz');
    say $stream->camelize;
    
    # Chain methods
    my $stream = Mojo::ByteStream->new('foo bar baz')->quote;
    $stream = $stream->unquote->encode('UTF-8')->b64_encode('');
    say "$stream";
    
    # Use the alternative constructor
    use Mojo::ByteStream;
    my $stream = b('foobarbaz')->b64_encode('')->say;
    

    Mojo::ByteStream 是一个基于标题的bytestream容器,为Mojo::Util中的许多函数提供了面向对象的调用接口。

    # Access scalar directly to manipulate bytestream
    my $stream = Mojo::ByteStream->new('foo');
    $$stream .= 'bar';
    

    函数

    Mojo::ByteStream模块中仅有一个供单独导入的函数b

    my $stream = b('test123');
    

    构造一个新的基于标题的Mojo::ByteStream对象。

    方法

    Mojo::ByteStream 实现了很多方法,这些方法中多数是与Mojo::Util中的函数同名且功能也相同,仅是支持的参数列表不同。其实就是调用了Mojo::Util中的方法实现的。

    b64_decode

    $stream = $stream->b64_decode;
    

    使用Mojo::Util中的b64_decode函数对Mojo::ByteStream对象进行Base64解码。

    b64_encode

    $stream = $stream->b64_encode;
    $stream = $stream->b64_encode("\n");
    

    使用Mojo::Util中的b64_encode函数对Mojo::ByteStream对象进行Base64编码。

    # "Zm9vIGJhciBiYXo="
    b('foo bar baz')->b64_encode('');
    

    camelize

    $stream = $stream->camelize;
    

    调用Mojo::Util中的camelize方法对Mojo::ByteStream进行驼峰化处理。

    clone

    my $stream2 = $stream->clone;
    

    对当前Mojo::ByteStream对象进行复制,返回复制得到的新对象。

    decamelize

    $stream = $stream->decamelize;
    

    调用Mojo::Util中的decamelize方法对Mojo::ByteStream进行反驼峰化处理。

    decode

    $stream = $stream->decode;
    $stream = $stream->decode('iso-8859-1');
    

    使用Mojo::Util中的decode函数对Mojo::ByteStream对象进行解码,默认使用UTF-8编码。

    # "♥"
    b('%E2%99%A5')->url_unescape->decode;
    

    encode

    $stream = $stream->encode;
    $stream = $stream->encode('iso-8859-1');
    

    使用Mojo::Util中的encode函数对Mojo::ByteStream对象进行编码,默认使用UTF-8编码。

    # "%E2%99%A5"
    b('♥')->encode->url_escape;
    

    hmac_sha1_sum

    $stream = $stream->hmac_sha1_sum('passw0rd');
    

    使用Mojo::Util中的hmac_sha1_sum对Mojo::ByteStream中的字节流计算HMAC-SHA1校验和。

    # "7fbdc89263974a89210ea71f171c77d3f8c21471"
    b('foo bar baz')->hmac_sha1_sum('secr3t');
    

    html_unescape

    $stream = $stream->html_unescape;
    

    使用Mojo::Util中的html_unescape解压ByteStream对象中的所有HTML实体。

    # "%3Chtml%3E"
    b('<html>')->html_unescape->url_escape;
    

    md5_bytes

    $stream = $stream->md5_bytes;
    

    使用Mojo::Util中的md5_bytes处理当前Mojo::ByteStream对象,得到二进制的MD5校验和。

    md5_sum

    $stream = $stream->md5_sum;
    

    使用Mojo::Util中的md5_sum函数处理当前Mojo::ByteStream对象,得到字节流的MD5校验和。

    new

    my $stream = Mojo::ByteStream->new('test123');
    

    创建一个基于标量的Mojo::ByteStream对象。

    punycode_decode

    $stream = $stream->punycode_decode;
    

    使用Mojo::Util中的punycode_decode函数对当前对象进行Punycode解码。

    punycode_encode

    $stream = $stream->punycode_encode;
    

    使用Mojo::Util中的punycode_encode函数对当前对象进行Punycode编码。

    quote

    $stream = $stream->quote;
    

    使用Mojo::Util中的quote函数处理当前对象。

    say

    $stream = $stream->say;
    $stream = $stream->say(*STDERR);
    

    把当前对象的内容输出到一个文件句柄,并在最后输出换行符。默认使用STDOUT作为输出的文件句柄。

    secure_compare

    my $bool = $stream->secure_compare($str);
    

    使用Mojo::Util中的secure_compare函数对当前对象和函数中的字符串或Mojo::ByteStream对象进行比较。

    sha1_bytes

    $stream = $stream->sha1_bytes;

    使用Mojo::Util中的sha1_bytes处理当前Mojo::ByteStream对象,得到二进制的SHA1校验和。

    sha1_sum

    $stream = $stream->sha1_sum;

    使用Mojo::Util中的sha1_sum处理当前Mojo::ByteStream对象,得到字节流的SHA1校验和。

    size

    my $size = $stream->size;
    

    返回字节流的大小。

    slugify

    $stream = $stream->slugify;
    $stream = $stream->slugify($bool);
    

    使用Mojo::Util中的slugify函数处理当前Mojo::ByteStream对象。

    split

    my $collection = $stream->split(',');
    

    使用指定的字符,字符中,或正则对当前Mojo::ByteStream进行切割。把得到的每个字符串片段都包装成Mojo::ByteStream对象,最终返回由众Mojo::ByteStream对象组成的Mojo::Collection对象。

    # "One,Two,Three"
    b("one,two,three")->split(',')->map('camelize')->join(',');
    

    tap

    $stream = $stream->tap(sub {...});
    

    Mojo::Base中tap方法的别名。

    term_escape

    $stream = $stream->term_escape;
    

    使用Mojo::Util中的term_escape方法处理当前对象。

    to_string

    my $str = $stream->to_string;
    

    返回当前对象所基于的字符串。

    trim

    $stream = $stream->trim;
    

    使用Mojo::Util中的trim方法去除当前对象首尾的空白字符。

    unindent

    $stream = $stream->unindent;
    

    使用Mojo::Util中的unindent函数处理当前对象。

    unquote

    $stream = $stream->unquote;
    

    使用Mojo::Util中的unquote处理当前对象,取消引用。

    url_escape

    $stream = $stream->url_escape;
    $stream = $stream->url_escape('^A-Za-z0-9\-._~');
    

    使用Mojo::Util中的url_escape函数处理当前对象,对url中不安全的字符进行编码。

    # "%E2%98%83"
    b('☃')->encode->url_escape;
    

    url_unescape

    $stream = $stream->url_unescape;

    使用Mojo::Util中的url_unescape函数处理当前对象,对url中不安全的字符进行解码。

    # "<html>"
    b('%3Chtml%3E')->url_unescape->xml_escape;
    

    with_roles

    my $new_class = Mojo::ByteStream->with_roles('Mojo::ByteStream::Role::One');
    my $new_class = Mojo::ByteStream->with_roles('+One', '+Two');
    $stream       = $stream->with_roles('+One', '+Two');
    

    Mojo::Base中with_roles函数的别名。

    xml_escape

    $stream = $stream->xml_escape;
    

    使用Mojo::Util中的xml_escape函数处理当前对象,对其中的&<>"'进行编码处理。

    xor_encode

    $stream = $stream->xor_encode($key);
    

    使用Mojo::Util中的xor_encode函数处理当前对象,进行XOR编码。

    # "%04%0E%15B%03%1B%10"
    b('foo bar')->xor_encode('baz')->url_escape;
    

    重载的去处符

    bool

    my $bool = !!$bytestream;
    

    永远为真。

    stringify

    my $str = "$bytestream";
    

    to_string函数的别名。

    相关文章

      网友评论

        本文标题:Mojo::ByteStream

        本文链接:https://www.haomeiwen.com/subject/frnmextx.html