home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Chip 2000 May
/
Chip_2000-05_cd1.bin
/
zkuste
/
Perl
/
ActivePerl-5.6.0.613.msi
/
䆊䌷䈹䈙䏵-䞅䞆䞀㡆䞃䄦䠥
/
_13c6061d36e4ad5debcd04119ff39bf8
< prev
next >
Wrap
Text File
|
2000-03-23
|
16KB
|
337 lines
<HTML>
<HEAD>
<TITLE>Text::Vpp - Perl extension for a versatile text pre-processor</TITLE>
<LINK REL="stylesheet" HREF="../../../Active.css" TYPE="text/css">
<LINK REV="made" HREF="mailto:">
</HEAD>
<BODY>
<TABLE BORDER=0 CELLPADDING=0 CELLSPACING=0 WIDTH=100%>
<TR><TD CLASS=block VALIGN=MIDDLE WIDTH=100% BGCOLOR="#cccccc">
<STRONG><P CLASS=block> Text::Vpp - Perl extension for a versatile text pre-processor</P></STRONG>
</TD></TR>
</TABLE>
<A NAME="__index__"></A>
<!-- INDEX BEGIN -->
<UL>
<LI><A HREF="#name">NAME</A></LI><LI><A HREF="#supportedplatforms">SUPPORTED PLATFORMS</A></LI>
<LI><A HREF="#synopsis">SYNOPSIS</A></LI>
<LI><A HREF="#description">DESCRIPTION</A></LI>
<LI><A HREF="#nondescription">NON-DESCRIPTION</A></LI>
<LI><A HREF="#input file syntax">INPUT FILE SYNTAX</A></LI>
<UL>
<LI><A HREF="#comments">Comments</A></LI>
<LI><A HREF="#inline eval">in-line eval</A></LI>
<LI><A HREF="#multiline input">Multi-line input</A></LI>
<LI><A HREF="#variables substitution">Variables substitution</A></LI>
<LI><A HREF="#advanced variables substitution">Advanced variables substitution</A></LI>
<LI><A HREF="#setting variables">Setting variables</A></LI>
<LI><A HREF="#conditional statements">Conditional statements</A></LI>
<LI><A HREF="#loop statements">Loop statements</A></LI>
<LI><A HREF="#inclusion">Inclusion</A></LI>
</UL>
<LI><A HREF="#constructor">Constructor</A></LI>
<UL>
<LI><A HREF="#new(file, optional_var_hash_ref, ...)">new(file, optional_var_hash_ref, ...)</A></LI>
</UL>
<LI><A HREF="#methods">Methods</A></LI>
<UL>
<LI><A HREF="#substitute([output_file])"><CODE>substitute([output_file])</CODE></A></LI>
<LI><A HREF="#rewind()"><CODE>rewind()</CODE></A></LI>
<LI><A HREF="#gettext()"><CODE>getText()</CODE></A></LI>
<LI><A HREF="#geterrors()"><CODE>getErrors()</CODE></A></LI>
<LI><A HREF="#setvar( key1=> value1, key2 => value2 ,...) or setvar(hash_ref)">setVar( key1=> value1, key2 => value2 ,...) or <CODE>setVar(hash_ref)</CODE></A></LI>
<LI><A HREF="#setvarfromfile( filename_or_ref )">setVarFromFile( Filename_or_Ref )</A></LI>
<LI><A HREF="#setactionchar(char)"><CODE>setActionChar(char)</CODE></A></LI>
<LI><A HREF="#setcommentchar(char)"><CODE>setCommentChar(char)</CODE></A></LI>
<LI><A HREF="#setprefixchar(char)"><CODE>setPrefixChar(char)</CODE></A></LI>
<LI><A HREF="#setsuffixchar(char)"><CODE>setSuffixChar(char)</CODE></A></LI>
<LI><A HREF="#setsubstitute([prefix,suffix])"><CODE>setSubstitute([prefix,suffix])</CODE></A></LI>
<LI><A HREF="#ignorebackslash()"><CODE>ignoreBackslash()</CODE></A></LI>
</UL>
<LI><A HREF="#caveats">CAVEATS</A></LI>
<LI><A HREF="#author">AUTHOR</A></LI>
<LI><A HREF="#see also">SEE ALSO</A></LI>
</UL>
<!-- INDEX END -->
<HR>
<P>
<H1><A NAME="name">NAME</A></H1>
<P>Text::Vpp - Perl extension for a versatile text pre-processor</P>
<P>
<HR>
<H1><A NAME="supportedplatforms">SUPPORTED PLATFORMS</A></H1>
<UL>
<LI>Linux</LI>
<LI>Solaris</LI>
<LI>Windows</LI>
</UL>
<HR>
<H1><A NAME="synopsis">SYNOPSIS</A></H1>
<PRE>
use Text::Vpp ;</PRE>
<PRE>
$fin = Text::Vpp-> new('input_file_name') ;</PRE>
<PRE>
$fin->setVar('one_variable_name' => 'value_one',
'another_variable_name' => 'value_two') ;</PRE>
<PRE>
$res = $fin -> substitute ; # or directly $fin -> substitute('file_out')</PRE>
<PRE>
die "Vpp error ",$fin->getErrors,"\n" unless $res ;</PRE>
<PRE>
$fout = $fin->getText ;</PRE>
<PRE>
print "Result is : \n\n",join("\n",@$fout) ,"\n";</PRE>
<P>
<HR>
<H1><A NAME="description">DESCRIPTION</A></H1>
<P>This class enables to preprocess a file a bit like cpp.</P>
<P>First you create a Vpp object passing the name of the file to process, then
you call <CODE>setvar()</CODE> to set the variables you need.</P>
<P>Finally you call substitute on the Vpp object.</P>
<P>
<HR>
<H1><A NAME="nondescription">NON-DESCRIPTION</A></H1>
<P>Note that it's not designed to replace the well known cpp. Note also
that if you think of using it to pre-process a perl script, you're
likely to shoot yourself in the foot. Perl has a lot of built-in
mechanisms so that a pre-processor is not necessary for most cases.</P>
<P>On the other hand some advanced perl users do use Vpp to pre-process their
code to gain speed. But in this case you should really think hard about the
maintenance of your code. Adding Vpp syntax in your code will make it
more difficult to maintain. Even more so if the code maintainer will not
be yourself. Furthermore, the build procedure may also be more complex.
So please, do consider the trade-off between speed and complexity.</P>
<P>
<HR>
<H1><A NAME="input file syntax">INPUT FILE SYNTAX</A></H1>
<P>
<H2><A NAME="comments">Comments</A></H2>
<P>All lines beginning with '#' are skipped. (May be changed with
<CODE>setCommentChar())</CODE></P>
<P>When <CODE>setActionChar()</CODE> is called with '#' as a parameter, Vpp doesn't
skip lines beginning with '#'. In this case, there's no comment possible.</P>
<P>
<H2><A NAME="inline eval">in-line eval</A></H2>
<P>Lines beginning with '@EVAL' (@ being pompously named the 'action char')
are evaluated as small perl script.
If a line contains (multiple) @@ Perl-Expression @@ constructs these
are replaced by the value of that Perl-Expression.
You can access all (non-lexically scoped) variables and subroutines from
any Perl package iff you use fully qualified names, i.e. for a subroutine
<EM>foo</EM> in package <EM>main</EM> use <EM>::foo</EM> or <EM>main::foo</EM>
To call one of the methods of a Vpp-object, like setActionChar, this
has to called as ``${self}->setActionChar('@');''
Be sure you know what you do, if you call such methods from within
an @EVAL line.</P>
<P>
<H2><A NAME="multiline input">Multi-line input</A></H2>
<P>Lines ending with \ are concatenated with the following line.</P>
<P>
<H2><A NAME="variables substitution">Variables substitution</A></H2>
<P>You can specify variables in your text beginning with $ (like in perl,
but may be changed with <CODE>setPrefixChar()</CODE> ) and optionally ending
in a Suffix which can be specified by setSuffixChar().
These variables can be set either by the <CODE>setVar()</CODE> method, the
<CODE>setVarFromFile()</CODE> method or by the 'eval' capability of Vpp (See below).</P>
<P>
<H2><A NAME="advanced variables substitution">Advanced variables substitution</A></H2>
<P>To use more complicated variables like hash or array accesses you have to
use either the 'in-line eval' above or a cheaper and more convenient
method. For that you can 'QUOTE' lines like</P>
<PRE>
@QUOTE
any lines
@ENDQUOTE</PRE>
<P>or</P>
<PRE>
@QUOTE ( ListPrefix [,ListSeparator] )
any lines
@ENDQUOTE</PRE>
<P>In both cases the lines between the '@QUOTE' and '@ENDQUOTE' are
concatenated while keeping the end-of-line character.</P>
<P>In the resulting string all '$' are protected unless $prefix or $suffix
or $ListPrefix contains a '$'. Furthermore all '@' are protected unless
one of these variables contains a '@'.</P>
<P>Then all variables (defined by $prefix/$suffix) are preprocessed to
make them ready for substitution later on. Likewise $ListPrefix (if
given) is converted to '@'.</P>
<P>Then this possible multiline construct is quoted by Perl's 'qq' and given
to Perl's eval.</P>
<P>Therefore any constructs which interpolate in a double quoted string,
will interpolate here too, i.e. variable starting with '$' or '@'
(unless protected, see above) and all characters escaped by '\'.</P>
<P>Note the standard trick to interpolate everything within a double
quoted string by using the anonymous array construct `` @{[expression]}
''. The $ListSeparator is used to locally set Perl's variable '$``' (or
$LIST_SEPARATOR in module English.pm). You can take any delimiting
character but not brackets of any sort to delimit either ListPrefix or
ListSeparator .</P>
<P>Note that this feature which raised a lot of discussions between the
Vpp contributors should be considered as 'alpha' stage. We may have
simpler ideas in the future to implement the same functionnality (hint:
all other ideas are welcome). So the interface or the feature itself
may be changed. Contact Helmut for further discussions.</P>
<P>
<H2><A NAME="setting variables">Setting variables</A></H2>
<P>Lines beginning by @ are 'evaled' using variables defined by <CODE>setVar()</CODE>
or setVarFromFile(). You can use only scalar variables. This way, you can
also define variables in your text which can be used later.</P>
<P>
<H2><A NAME="conditional statements">Conditional statements</A></H2>
<P>Text::Vpp understands @IF, @ELSIF, @ENDIF,and so on. @INCLUDE and @IF
can be nested.</P>
<P>@IF and @ELSIF are followed by a Perl expression which will be evaled using
the variables you have defined (either with setVar(), <CODE>setVarFromFile()</CODE>
or in an @EVAL line).</P>
<P>
<H2><A NAME="loop statements">Loop statements</A></H2>
<P>Text::Vpp also understands</P>
<P>@FOREACH $MyLoopVar ( Perl-List-Expression )
... (any) lines which may depend on $MyLoopVar
@ENDFOR</P>
<P>These loops may be nested.</P>
<P>
<H2><A NAME="inclusion">Inclusion</A></H2>
<P>Text::Vpp understands
@INCLUDE Filename or Perl-Expression
@INCLUDE { action => '\\', backslash => 0, file => 'add_on.001' }</P>
<P>The file name may be a bare words if it contains only alphanumeric
characters or '-', '.' or '_'. Otherwise, the file name must be quoted.</P>
<P>If the Perl-Expression is a string, it is taken as a
filename.</P>
<P>If it is an anonymous hash, it must have a value for the key 'file'
and it may have values for 'action', 'comment', 'prefix', 'suffix',
'substitute' and 'backslash'. If given these values will override the
current values during the processing of the included file.</P>
<P>
<HR>
<H1><A NAME="constructor">Constructor</A></H1>
<P>
<H2><A NAME="new(file, optional_var_hash_ref, ...)">new(file, optional_var_hash_ref, ...)</A></H2>
<P>The constructor call
<CODE>new(file, optional_var_hash_ref,optional_action_char,</CODE>
<CODE> optional_comment_char, optional_prefix_char,</CODE>
<CODE> optional_suffix_char, optional_substitute,</CODE>
<CODE> optional_backslash_switch);</CODE></P>
<P>creates the Vpp object. The file parameter may be a filename or
a blessed reference for an object which ``can('getline')''.
The second parameter can be a hash containing all
variables needed for the substitute method, the following (optional)
parameters specify the corresponding special characters.</P>
<P>
<HR>
<H1><A NAME="methods">Methods</A></H1>
<P>
<H2><A NAME="substitute([output_file])"><CODE>substitute([output_file])</CODE></A></H2>
<P>Perform the substitute, inclusion, and so on and write the result in
<EM>output_file</EM>. This maybe a filename or a blessed reference which
``can('print')'' .
Returns 1 on completion, 0 in case of an error.</P>
<P>If output_file is not specified this function stores the substitution result
in an internal variable. The result can be retrieved with <CODE>getText()</CODE></P>
<PRE>
You may prefix the filename with >> to get the output
appended to an existing file.</PRE>
<P>
<H2><A NAME="rewind()"><CODE>rewind()</CODE></A></H2>
<P>If method 'substitute' is called more than once, you have to call
'rewind' in between.
CAUTION If you have called method 'new' with a blessed reference
instead of a filename, you must not call 'rewind' unless
your object has a 'seek' method. Otherwise you have to do
actions yourself which simulate 'rewind' for your object.</P>
<P>
<H2><A NAME="gettext()"><CODE>getText()</CODE></A></H2>
<PRE>
Returns an array ref containing the result. You can then get the total
file with join "\n",@{VppObj->getText}</PRE>
<P>
<H2><A NAME="geterrors()"><CODE>getErrors()</CODE></A></H2>
<P>Returns an array ref containing the errors.</P>
<P>
<H2><A NAME="setvar( key1=> value1, key2 => value2 ,...) or setvar(hash_ref)">setVar( key1=> value1, key2 => value2 ,...) or <CODE>setVar(hash_ref)</CODE></A></H2>
<P>Declare variables for the substitute.
Note that calling this function clobbers previously stored values.</P>
<P>
<H2><A NAME="setvarfromfile( filename_or_ref )">setVarFromFile( Filename_or_Ref )</A></H2>
<P>Declares a File or an object which can 'getline'.
The file must contain a valid Perl expression yielding an
anonymous hash, as created e.g. by Data::Dumper
Note that calling this function clobbers previously stored values.</P>
<P>
<H2><A NAME="setactionchar(char)"><CODE>setActionChar(char)</CODE></A></H2>
<P>Enables the user to use different char as action char. (default @)</P>
<P>Example: <CODE>setActionChar('#')</CODE> will enable Vpp to understand #include, #ifdef ..</P>
<P>
<H2><A NAME="setcommentchar(char)"><CODE>setCommentChar(char)</CODE></A></H2>
<P>Enables the user to use different char as comment char. (default #)
This value may be set to undef so that no comments are possible.</P>
<P>
<H2><A NAME="setprefixchar(char)"><CODE>setPrefixChar(char)</CODE></A></H2>
<P>Enables the user to use different <CODE>char(s)</CODE> as prefix char(s), i.e. variables
in your text (only) are prefixed by that <CODE>character(s)</CODE> instead of the
default '$'. If no suffix <CODE>character(s)</CODE> has been defined (or set to 'undef')
variables may be specified in the form ${variable} where '$' is the
current prefix char(s). This form is necessary, if any character which
is allowed within a name (regexp '\w') immediately follows the variable.
Note, that all variables in 'actions' (like @@ @EVAL @FOREACH @IF)
must still be prefixed by '$'.</P>
<P>
<H2><A NAME="setsuffixchar(char)"><CODE>setSuffixChar(char)</CODE></A></H2>
<P>Enables the user to use different <CODE>char(s)</CODE> as suffix char(s), i.e. variables
in your text (only) are suffixed by that character(s).
Note, that all variables in 'actions' (like @@ @EVAL @FOREACH @IF)
don't use this.</P>
<P>
<H2><A NAME="setsubstitute([prefix,suffix])"><CODE>setSubstitute([prefix,suffix])</CODE></A></H2>
<P>Enables the user to specify the prefix and suffix used to mark
a Perl expression within the text that will be replaced by its
value. The default value is twice the 'action' char as suffix
and prefix.</P>
<P>
<H2><A NAME="ignorebackslash()"><CODE>ignoreBackslash()</CODE></A></H2>
<P>By default, line ending with '\' are glued to the following line (like in
ksh). Once this method is called '\' will be left as is.</P>
<P>
<HR>
<H1><A NAME="caveats">CAVEATS</A></H1>
<P>Version 1.0 now requires files included with '@INCLUDE' to be quoted.
Version 1.1 now requires calls to method 'rewind' if 'substitute' is
called more than once for the same Vpp-object.</P>
<P>
<HR>
<H1><A NAME="author">AUTHOR</A></H1>
<P>Dominique Dumont <A HREF="mailto:Dominique_Dumont@grenoble.hp.com">Dominique_Dumont@grenoble.hp.com</A></P>
<P>Copyright (c) 1996-1999 Dominique Dumont. All rights reserved. This
program is free software; you can redistribute it and/or modify it
under the same terms as Perl itself.</P>
<P>Additional bugs have been introduced by
Helmut Jarausch <A HREF="mailto:jarausch@igpm.rwth-aachen.de">jarausch@igpm.rwth-aachen.de</A></P>
<P>
<HR>
<H1><A NAME="see also">SEE ALSO</A></H1>
<P>perl(1),Text::Template(3).</P>
<TABLE BORDER=0 CELLPADDING=0 CELLSPACING=0 WIDTH=100%>
<TR><TD CLASS=block VALIGN=MIDDLE WIDTH=100% BGCOLOR="#cccccc">
<STRONG><P CLASS=block> Text::Vpp - Perl extension for a versatile text pre-processor</P></STRONG>
</TD></TR>
</TABLE>
</BODY>
</HTML>