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

📄 simple.pm

📁 视频监控网络部分的协议ddns,的模块的实现代码,请大家大胆指正.
💻 PM
📖 第 1 页 / 共 2 页
字号:
In other words, the previous example, would become:    package BANG;    use Filter::Simple;        FILTER {        s/BANG\s+BANG/die 'BANG' if \$BANG/g;    };    1 ;Note that the source code is passed as a single string, so any regex thatuses C<^> or C<$> to detect line boundaries will need the C</m> flag.=head2 Disabling or changing <no> behaviourBy default, the installed filter only filters up to a line consisting of one ofthe three standard source "terminators":    no ModuleName;  # optional commentor:    __END__or:    __DATA__but this can be altered by passing a second argument to C<use Filter::Simple>or C<FILTER> (just remember: there's I<no> comma after the initial block whenyou use C<FILTER>).That second argument may be either a C<qr>'d regular expression (which is thenused to match the terminator line), or a defined false value (which indicatesthat no terminator line should be looked for), or a reference to a hash(in which case the terminator is the value associated with the keyC<'terminator'>.For example, to cause the previous filter to filter only up to a line of theform:    GNAB esu;you would write:    package BANG;    use Filter::Simple;        FILTER {        s/BANG\s+BANG/die 'BANG' if \$BANG/g;    }    qr/^\s*GNAB\s+esu\s*;\s*?$/;or:    FILTER {        s/BANG\s+BANG/die 'BANG' if \$BANG/g;    }    { terminator => qr/^\s*GNAB\s+esu\s*;\s*?$/ };and to prevent the filter's being turned off in any way:    package BANG;    use Filter::Simple;        FILTER {        s/BANG\s+BANG/die 'BANG' if \$BANG/g;    }    "";    # or: 0or:    FILTER {        s/BANG\s+BANG/die 'BANG' if \$BANG/g;    }    { terminator => "" };B<Note that, no matter what you set the terminator pattern to,the actual terminator itself I<must> be contained on a single source line.>=head2 All-in-one interfaceSeparating the loading of Filter::Simple:    use Filter::Simple;from the setting up of the filtering:    FILTER { ... };is useful because it allows other code (typically parser support codeor caching variables) to be defined before the filter is invoked.However, there is often no need for such a separation.In those cases, it is easier to just append the filtering subroutine andany terminator specification directly to the C<use> statement that loadsFilter::Simple, like so:    use Filter::Simple sub {        s/BANG\s+BANG/die 'BANG' if \$BANG/g;    };This is exactly the same as:    use Filter::Simple;    BEGIN {        Filter::Simple::FILTER {            s/BANG\s+BANG/die 'BANG' if \$BANG/g;        };    }except that the C<FILTER> subroutine is not exported by Filter::Simple.=head2 Filtering only specific components of source codeOne of the problems with a filter like:    use Filter::Simple;    FILTER { s/BANG\s+BANG/die 'BANG' if \$BANG/g };is that it indiscriminately applies the specified transformation tothe entire text of your source program. So something like:    warn 'BANG BANG, YOU'RE DEAD';    BANG BANG;will become:    warn 'die 'BANG' if $BANG, YOU'RE DEAD';    die 'BANG' if $BANG;It is very common when filtering source to only want to apply the filterto the non-character-string parts of the code, or alternatively to I<only>the character strings.Filter::Simple supports this type of filtering by automaticallyexporting the C<FILTER_ONLY> subroutine.C<FILTER_ONLY> takes a sequence of specifiers that install separate(and possibly multiple) filters that act on only parts of the source code.For example:    use Filter::Simple;    FILTER_ONLY        code      => sub { s/BANG\s+BANG/die 'BANG' if \$BANG/g },        quotelike => sub { s/BANG\s+BANG/CHITTY CHITTY/g };The C<"code"> subroutine will only be used to filter parts of the sourcecode that are not quotelikes, POD, or C<__DATA__>. The C<quotelike>subroutine only filters Perl quotelikes (including here documents).The full list of alternatives is:=over=item C<"code">Filters only those sections of the source code that are not quotelikes, POD, orC<__DATA__>.=item C<"code_no_comments">Filters only those sections of the source code that are not quotelikes, POD,comments, or C<__DATA__>.=item C<"executable">Filters only those sections of the source code that are not POD or C<__DATA__>.=item C<"executable_no_comments">Filters only those sections of the source code that are not POD, comments, or C<__DATA__>.=item C<"quotelike">Filters only Perl quotelikes (as interpreted byC<&Text::Balanced::extract_quotelike>).=item C<"string">Filters only the string literal parts of a Perl quotelike (i.e. the contents of a string literal, either half of a C<tr///>, the secondhalf of an C<s///>).=item C<"regex">Filters only the pattern literal parts of a Perl quotelike (i.e. the contents of a C<qr//> or an C<m//>, the first half of an C<s///>).=item C<"all">Filters everything. Identical in effect to C<FILTER>.=backExcept for C<< FILTER_ONLY code => sub {...} >>, each ofthe component filters is called repeatedly, once for each componentfound in the source code.Note that you can also apply two or more of the same type of filter ina single C<FILTER_ONLY>. For example, here's a simple macro-preprocessor that is only applied within regexes,with a final debugging pass that prints the resulting source code:    use Regexp::Common;    FILTER_ONLY        regex => sub { s/!\[/[^/g },        regex => sub { s/%d/$RE{num}{int}/g },        regex => sub { s/%f/$RE{num}{real}/g },        all   => sub { print if $::DEBUG };=head2 Filtering only the code parts of source code Most source code ceases to be grammatically correct when it is broken upinto the pieces between string literals and regexes. So the C<'code'>and C<'code_no_comments'> component filter behave slightly differentlyfrom the other partial filters described in the previous section.Rather than calling the specified processor on each individual piece ofcode (i.e. on the bits between quotelikes), the C<'code...'> partialfilters operate on the entire source code, but with the quotelike bits(and, in the case of C<'code_no_comments'>, the comments) "blanked out".That is, a C<'code...'> filter I<replaces> each quoted string, quotelike,regex, POD, and __DATA__ section with a placeholder. Thedelimiters of this placeholder are the contents of the C<$;> variableat the time the filter is applied (normally C<"\034">). The remainingfour bytes are a unique identifier for the component being replaced.This approach makes it comparatively easy to write code preprocessorswithout worrying about the form or contents of strings, regexes, etc.For convenience, during a C<'code...'> filtering operation, Filter::Simpleprovides a package variable (C<$Filter::Simple::placeholder>) thatcontains a pre-compiled regex that matches any placeholder...andcaptures the identifier within the placeholder. Placeholders can bemoved and re-ordered within the source code as needed.In addition, a second package variable (C<@Filter::Simple::components>)contains a list of the various pieces of C<$_>, as they were originally splitup to allow placeholders to be inserted.Once the filtering has been applied, the original strings, regexes, POD,etc. are re-inserted into the code, by replacing each placeholder withthe corresponding original component (from C<@components>). Note thatthis means that the C<@components> variable must be treated with extremecare within the filter. The C<@components> array stores the "back-translations" of each placeholder inserted into C<$_>, as well as theinterstitial source code between placeholders. If the placeholderbacktranslations are altered in C<@components>, they will be similarlychanged when the placeholders are removed from C<$_> after the filteris complete.For example, the following filter detects concatenated pairs ofstrings/quotelikes and reverses the order in which they areconcatenated:    package DemoRevCat;    use Filter::Simple;    FILTER_ONLY code => sub {        my $ph = $Filter::Simple::placeholder;        s{ ($ph) \s* [.] \s* ($ph) }{ $2.$1 }gx    };Thus, the following code:    use DemoRevCat;    my $str = "abc" . q(def);    print "$str\n";would become:    my $str = q(def)."abc";    print "$str\n";and hence print:    defabc=head2 Using Filter::Simple with an explicit C<import> subroutineFilter::Simple generates a special C<import> subroutine foryour module (see L<"How it works">) which would normally replace anyC<import> subroutine you might have explicitly declared.However, Filter::Simple is smart enough to notice your existingC<import> and Do The Right Thing with it.That is, if you explicitly define an C<import> subroutine in a packagethat's using Filter::Simple, that C<import> subroutine will stillbe invoked immediately after any filter you install.The only thing you have to remember is that the C<import> subroutineI<must> be declared I<before> the filter is installed. If you use C<FILTER>to install the filter:    package Filter::TurnItUpTo11;    use Filter::Simple;    FILTER { s/(\w+)/\U$1/ };    that will almost never be a problem, but if you install a filteringsubroutine by passing it directly to the C<use Filter::Simple>statement:    package Filter::TurnItUpTo11;    use Filter::Simple sub{ s/(\w+)/\U$1/ };then you must make sure that your C<import> subroutine appears beforethat C<use> statement.=head2 Using Filter::Simple and Exporter togetherLikewise, Filter::Simple is also smart enoughto Do The Right Thing if you use Exporter:    package Switch;    use base Exporter;    use Filter::Simple;    @EXPORT    = qw(switch case);    @EXPORT_OK = qw(given  when);    FILTER { $_ = magic_Perl_filter($_) }Immediately after the filter has been applied to the source,Filter::Simple will pass control to Exporter, so it can do its magic too.Of course, here too, Filter::Simple has to know you're using Exporterbefore it applies the filter. That's almost never a problem, but if you'renervous about it, you can guarantee that things will work correctly byensuring that your C<use base Exporter> always precedes yourC<use Filter::Simple>.=head2 How it worksThe Filter::Simple module exports into the package that calls C<FILTER>(or C<use>s it directly) -- such as package "BANG" in the above example --two automagically constructedsubroutines -- C<import> and C<unimport> -- which take care of all thenasty details.In addition, the generated C<import> subroutine passes its own argumentlist to the filtering subroutine, so the BANG.pm filter could easily be made parametric:    package BANG;     use Filter::Simple;        FILTER {        my ($die_msg, $var_name) = @_;        s/BANG\s+BANG/die '$die_msg' if \${$var_name}/g;    };    # and in some user code:    use BANG "BOOM", "BAM";  # "BANG BANG" becomes: die 'BOOM' if $BAMThe specified filtering subroutine is called every time a C<use BANG> isencountered, and passed all the source code following that call, up toeither the next C<no BANG;> (or whatever terminator you've set) or theend of the source file, whichever occurs first. By default, any C<noBANG;> call must appear by itself on a separate line, or it is ignored.=head1 AUTHORDamian Conway (damian@conway.org)=head1 COPYRIGHT    Copyright (c) 2000-2001, Damian Conway. All Rights Reserved.    This module is free software. It may be used, redistributed    and/or modified under the same terms as Perl itself.

⌨️ 快捷键说明

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