WWW::Webrobot::Util - some simple utilities


webrobot documentation Contained in the webrobot distribution.

Index


Code Index:

NAME

Top

WWW::Webrobot::Util - some simple utilities

SYNOPSIS

Top

 WWW::Webrobot::Util::ascii("a\x{76EE}b");

DESCRIPTION

Top

Some simple utility functions.

METHODS

Top

ascii

encode all multi-byte and control characters in printable form

textify

encode all multi-byte characters in printable form

octet

encode non-printables except control characters as octets

octet_all

encode non-printables as octets


webrobot documentation Contained in the webrobot distribution.
package WWW::Webrobot::Util;
use strict;
use warnings;

# Author: Stefan Trcek
# Copyright(c) 2004 ABAS Software AG


use Exporter;
use base qw/Exporter/;
our @EXPORT_OK = qw/ascii textify octet/;


sub _encode_text {
    my ($fun) = shift;
    if (wantarray) {
        return map {$fun->($_)} @_;
    }
    else {
        return join "", map {$fun->($_)} @_;
    }
}

sub ascii {
    _encode_text(sub {
        join("",
            map {
                $_ > 255 ?                      # if wide character...
                    sprintf("\\x{%04X}", $_)    #     \x{...}
                : chr($_) =~ /[[:cntrl:]]/ ?    # else if control character ...
                    sprintf("\\x%02X", $_)      #     \x..
                :                               # else
                    chr($_)                     #     as themselves
            } unpack("U*", $_[0])
        );
    },
    @_);
}

sub textify {
    _encode_text(sub {
        join("",
            map {
                $_ > 255 ?                      # if wide character...
                    sprintf("\\x{%04X}", $_)    #     \x{...}
                :                               # else
                    chr($_)                     #     as themselves
            } unpack("U*", $_[0])
        );
    },
    @_);
}
 

sub octet {
    _encode_text(sub {
        join("",
            map {
                $_ > 255 ?                      # if wide character...
                    sprintf("\\x{%04X}", $_)    #     \x{...}
                : $_ > 127 ?                    # if 1xxxxxxx
                    sprintf("\\x{%02X}", $_)      #     \x..
                :                               # else
                    chr($_)                     #     as themselves
            } unpack("C*", $_[0])
        );
    },
    @_);
}

sub octet_all {
    _encode_text(sub {
        join("",
            map {
                $_ > 255 ?                      # if wide character...
                    sprintf("\\x{%04X}", $_)    #     \x{...}
                : chr($_) =~ /[[:cntrl:]]/ ?    # else if control character ...
                    sprintf("\\x{%02X}", $_)      #     \x..
                : $_ > 127 ?                    # if 1xxxxxxx
                    sprintf("\\x{%02X}", $_)      #     \x..
                :                               # else
                    chr($_)                     #     as themselves
            } unpack("C*", $_[0])
        );
    },
    @_);
}

1;