⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 loader.pm

📁 视频监控网络部分的协议ddns,的模块的实现代码,请大家大胆指正.
💻 PM
📖 第 1 页 / 共 2 页
字号:
package YAML::Loader;use strict; use warnings;use YAML::Base;use base 'YAML::Loader::Base';use YAML::Types;# Context constantsuse constant LEAF => 1;use constant COLLECTION => 2;use constant VALUE => "\x07YAML\x07VALUE\x07";use constant COMMENT => "\x07YAML\x07COMMENT\x07";# Common YAML character setsmy $ESCAPE_CHAR = '[\\x00-\\x08\\x0b-\\x0d\\x0e-\\x1f]';my $FOLD_CHAR = '>';my $LIT_CHAR = '|';    my $LIT_CHAR_RX = "\\$LIT_CHAR";    sub load {    my $self = shift;    $self->stream($_[0] || '');    return $self->_parse();}# Top level function for parsing. Parse each document in order and# handle processing for YAML headers.sub _parse {    my $self = shift;    my (%directives, $preface);    $self->{stream} =~ s|\015\012|\012|g;    $self->{stream} =~ s|\015|\012|g;    $self->line(0);    $self->die('YAML_PARSE_ERR_BAD_CHARS')       if $self->stream =~ /$ESCAPE_CHAR/;    $self->die('YAML_PARSE_ERR_NO_FINAL_NEWLINE')       if length($self->stream) and          $self->{stream} !~ s/(.)\n\Z/$1/s;    $self->lines([split /\x0a/, $self->stream, -1]);    $self->line(1);    # Throw away any comments or blanks before the header (or start of    # content for headerless streams)    $self->_parse_throwaway_comments();    $self->document(0);    $self->documents([]);    # Add an "assumed" header if there is no header and the stream is    # not empty (after initial throwaways).    if (not $self->eos) {        if ($self->lines->[0] !~ /^---(\s|$)/) {            unshift @{$self->lines}, '---';            $self->{line}--;        }    }    # Main Loop. Parse out all the top level nodes and return them.    while (not $self->eos) {        $self->anchor2node({});        $self->{document}++;        $self->done(0);        $self->level(0);        $self->offset->[0] = -1;        if ($self->lines->[0] =~ /^---\s*(.*)$/) {            my @words = split /\s+/, $1;            %directives = ();            while (@words && $words[0] =~ /^#(\w+):(\S.*)$/) {                my ($key, $value) = ($1, $2);                shift(@words);                if (defined $directives{$key}) {                    $self->warn('YAML_PARSE_WARN_MULTIPLE_DIRECTIVES',                      $key, $self->document);                    next;                }                $directives{$key} = $value;            }            $self->preface(join ' ', @words);        }        else {            $self->die('YAML_PARSE_ERR_NO_SEPARATOR');        }        if (not $self->done) {            $self->_parse_next_line(COLLECTION);        }        if ($self->done) {            $self->{indent} = -1;            $self->content('');        }        $directives{YAML} ||= '1.0';        $directives{TAB} ||= 'NONE';        ($self->{major_version}, $self->{minor_version}) =           split /\./, $directives{YAML}, 2;        $self->die('YAML_PARSE_ERR_BAD_MAJOR_VERSION', $directives{YAML})          if $self->major_version ne '1';        $self->warn('YAML_PARSE_WARN_BAD_MINOR_VERSION', $directives{YAML})          if $self->minor_version ne '0';        $self->die('Unrecognized TAB policy')          unless $directives{TAB} =~ /^(NONE|\d+)(:HARD)?$/;        push @{$self->documents}, $self->_parse_node();    }    return wantarray ? @{$self->documents} : $self->documents->[-1];}# This function is the dispatcher for parsing each node. Every node# recurses back through here. (Inlines are an exception as they have# their own sub-parser.)sub _parse_node {    my $self = shift;    my $preface = $self->preface;    $self->preface('');    my ($node, $type, $indicator, $escape, $chomp) = ('') x 5;    my ($anchor, $alias, $explicit, $implicit, $class) = ('') x 5;    ($anchor, $alias, $explicit, $implicit, $preface) =       $self->_parse_qualifiers($preface);    if ($anchor) {        $self->anchor2node->{$anchor} = CORE::bless [], 'YAML-anchor2node';    }    $self->inline('');    while (length $preface) {        my $line = $self->line - 1;        if ($preface =~ s/^($FOLD_CHAR|$LIT_CHAR_RX)(-|\+)?\d*\s*//) {             $indicator = $1;            $chomp = $2 if defined($2);        }        else {            $self->die('YAML_PARSE_ERR_TEXT_AFTER_INDICATOR') if $indicator;            $self->inline($preface);            $preface = '';        }    }    if ($alias) {        $self->die('YAML_PARSE_ERR_NO_ANCHOR', $alias)          unless defined $self->anchor2node->{$alias};        if (ref($self->anchor2node->{$alias}) ne 'YAML-anchor2node') {            $node = $self->anchor2node->{$alias};        }        else {            $node = do {my $sv = "*$alias"};            push @{$self->anchor2node->{$alias}}, [\$node, $self->line];         }    }    elsif (length $self->inline) {        $node = $self->_parse_inline(1, $implicit, $explicit);        if (length $self->inline) {            $self->die('YAML_PARSE_ERR_SINGLE_LINE');         }    }    elsif ($indicator eq $LIT_CHAR) {        $self->{level}++;        $node = $self->_parse_block($chomp);        $node = $self->_parse_implicit($node) if $implicit;        $self->{level}--;     }    elsif ($indicator eq $FOLD_CHAR) {        $self->{level}++;        $node = $self->_parse_unfold($chomp);        $node = $self->_parse_implicit($node) if $implicit;        $self->{level}--;    }    else {        $self->{level}++;        $self->offset->[$self->level] ||= 0;        if ($self->indent == $self->offset->[$self->level]) {            if ($self->content =~ /^-( |$)/) {                $node = $self->_parse_seq($anchor);            }            elsif ($self->content =~ /(^\?|\:( |$))/) {                $node = $self->_parse_mapping($anchor);            }            elsif ($preface =~ /^\s*$/) {                $node = $self->_parse_implicit('');            }            else {                $self->die('YAML_PARSE_ERR_BAD_NODE');            }        }        else {            $node = undef;        }        $self->{level}--;    }    $#{$self->offset} = $self->level;    if ($explicit) {        if ($class) {            if (not ref $node) {                my $copy = $node;                undef $node;                $node = \$copy;            }            CORE::bless $node, $class;        }        else {            $node = $self->_parse_explicit($node, $explicit);        }    }    if ($anchor) {        if (ref($self->anchor2node->{$anchor}) eq 'YAML-anchor2node') {            # XXX Can't remember what this code actually does            for my $ref (@{$self->anchor2node->{$anchor}}) {                ${$ref->[0]} = $node;                $self->warn('YAML_LOAD_WARN_UNRESOLVED_ALIAS',                    $anchor, $ref->[1]);            }        }        $self->anchor2node->{$anchor} = $node;    }    return $node;}# Preprocess the qualifiers that may be attached to any node.sub _parse_qualifiers {    my $self = shift;    my ($preface) = @_;    my ($anchor, $alias, $explicit, $implicit, $token) = ('') x 5;    $self->inline('');    while ($preface =~ /^[&*!]/) {        my $line = $self->line - 1;        if ($preface =~ s/^\!(\S+)\s*//) {            $self->die('YAML_PARSE_ERR_MANY_EXPLICIT') if $explicit;            $explicit = $1;        }        elsif ($preface =~ s/^\!\s*//) {            $self->die('YAML_PARSE_ERR_MANY_IMPLICIT') if $implicit;            $implicit = 1;        }        elsif ($preface =~ s/^\&([^ ,:]+)\s*//) {            $token = $1;            $self->die('YAML_PARSE_ERR_BAD_ANCHOR')               unless $token =~ /^[a-zA-Z0-9]+$/;            $self->die('YAML_PARSE_ERR_MANY_ANCHOR') if $anchor;            $self->die('YAML_PARSE_ERR_ANCHOR_ALIAS') if $alias;            $anchor = $token;        }        elsif ($preface =~ s/^\*([^ ,:]+)\s*//) {            $token = $1;            $self->die('YAML_PARSE_ERR_BAD_ALIAS')              unless $token =~ /^[a-zA-Z0-9]+$/;            $self->die('YAML_PARSE_ERR_MANY_ALIAS') if $alias;            $self->die('YAML_PARSE_ERR_ANCHOR_ALIAS') if $anchor;            $alias = $token;        }    }    return ($anchor, $alias, $explicit, $implicit, $preface); }# Morph a node to it's explicit type  sub _parse_explicit {    my $self = shift;    my ($node, $explicit) = @_;    my ($type, $class);    if ($explicit =~ /^\!?perl\/(hash|array|ref|scalar)(?:\:(\w(\w|\:\:)*)?)?$/) {        ($type, $class) = (($1 || ''), ($2 || ''));        # FIXME # die unless uc($type) eq ref($node) ?        if ( $type eq "ref" ) {            $self->die('YAML_LOAD_ERR_NO_DEFAULT_VALUE', 'XXX', $explicit)            unless exists $node->{VALUE()} and scalar(keys %$node) == 1;            my $value = $node->{VALUE()};            $node = \$value;        }                if ( $type eq "scalar" and length($class) and !ref($node) ) {            my $value = $node;            $node = \$value;        }        if ( length($class) ) {            CORE::bless($node, $class);        }        return $node;    }    if ($explicit =~ m{^!?perl/(glob|regexp|code)(?:\:(\w(\w|\:\:)*)?)?$}) {        ($type, $class) = (($1 || ''), ($2 || ''));        my $type_class = "YAML::Type::$type";        no strict 'refs';        if ($type_class->can('yaml_load')) {            return $type_class->yaml_load($node, $class, $self);        }        else {            $self->die('YAML_LOAD_ERR_NO_CONVERT', 'XXX', $explicit);        }    }    # This !perl/@Foo and !perl/$Foo are deprecated but still parsed    elsif ($YAML::TagClass->{$explicit} ||           $explicit =~ m{^perl/(\@|\$)?([a-zA-Z](\w|::)+)$}          ) {        $class = $YAML::TagClass->{$explicit} || $2;        if ($class->can('yaml_load')) {            require YAML::Node;            return $class->yaml_load(YAML::Node->new($node, $explicit));        }        else {            if (ref $node) {                return CORE::bless $node, $class;            }            else {                return CORE::bless \$node, $class;            }        }    }    elsif (ref $node) {        require YAML::Node;        return YAML::Node->new($node, $explicit);    }    else {        # XXX This is likely wrong. Failing test:        # --- !unknown 'scalar value'        return $node;    }}# Parse a YAML mapping into a Perl hashsub _parse_mapping {    my $self = shift;    my ($anchor) = @_;    my $mapping = {};    $self->anchor2node->{$anchor} = $mapping;    my $key;    while (not $self->done and $self->indent == $self->offset->[$self->level]) {        # If structured key:        if ($self->{content} =~ s/^\?\s*//) {            $self->preface($self->content);            $self->_parse_next_line(COLLECTION);            $key = $self->_parse_node();            $key = "$key";        }        # If "default" key (equals sign)         elsif ($self->{content} =~ s/^\=\s*//) {            $key = VALUE;        }        # If "comment" key (slash slash)        elsif ($self->{content} =~ s/^\=\s*//) {            $key = COMMENT;        }        # Regular scalar key:        else {            $self->inline($self->content);            $key = $self->_parse_inline();            $key = "$key";            $self->content($self->inline);            $self->inline('');        }                    unless ($self->{content} =~ s/^:\s*//) {            $self->die('YAML_LOAD_ERR_BAD_MAP_ELEMENT');        }        $self->preface($self->content);        my $line = $self->line;        $self->_parse_next_line(COLLECTION);        my $value = $self->_parse_node();        if (exists $mapping->{$key}) {            $self->warn('YAML_LOAD_WARN_DUPLICATE_KEY');        }        else {            $mapping->{$key} = $value;        }    }    return $mapping;}# Parse a YAML sequence into a Perl arraysub _parse_seq {    my $self = shift;    my ($anchor) = @_;    my $seq = [];    $self->anchor2node->{$anchor} = $seq;    while (not $self->done and $self->indent == $self->offset->[$self->level]) {        if ($self->content =~ /^-(?: (.*))?$/) {            $self->preface(defined($1) ? $1 : '');        }        else {            $self->die('YAML_LOAD_ERR_BAD_SEQ_ELEMENT');        }        if ($self->preface =~ /^(\s*)(\w.*\:(?: |$).*)$/) {            $self->indent($self->offset->[$self->level] + 2 + length($1));            $self->content($2);            $self->level($self->level + 1);            $self->offset->[$self->level] = $self->indent;            $self->preface('');            push @$seq, $self->_parse_mapping('');            $self->{level}--;            $#{$self->offset} = $self->level;        }        else {            $self->_parse_next_line(COLLECTION);            push @$seq, $self->_parse_node();

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -