ActivePerl Documentation
|
NAMEText::Vpp - Perl extension for a versatile text pre-processor
SUPPORTED PLATFORMS
SYNOPSISuse Text::Vpp ;
$fin = Text::Vpp-> new('input_file_name') ;
$fin->setVar('one_variable_name' => 'value_one',
'another_variable_name' => 'value_two') ;
$res = $fin -> substitute ; # or directly $fin -> substitute('file_out')
die "Vpp error ",$fin->getErrors,"\n" unless $res ; $fout = $fin->getText ;
print "Result is : \n\n",join("\n",@$fout) ,"\n";
DESCRIPTIONThis class enables to preprocess a file a bit like cpp. First you create a Vpp object passing the name of the file to process, then
you call Finally you call substitute on the Vpp object.
NON-DESCRIPTIONNote 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. 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.
INPUT FILE SYNTAX
CommentsAll lines beginning with '#' are skipped. (May be changed with
When
in-line evalLines 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 foo in package main use ::foo or main::foo 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.
Multi-line inputLines ending with \ are concatenated with the following line.
Variables substitutionYou can specify variables in your text beginning with $ (like in perl,
but may be changed with
Advanced variables substitutionTo 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 @QUOTE any lines @ENDQUOTE or @QUOTE ( ListPrefix [,ListSeparator] ) any lines @ENDQUOTE In both cases the lines between the '@QUOTE' and '@ENDQUOTE' are concatenated while keeping the end-of-line character. 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 '@'. Then all variables (defined by $prefix/$suffix) are preprocessed to make them ready for substitution later on. Likewise $ListPrefix (if given) is converted to '@'. Then this possible multiline construct is quoted by Perl's 'qq' and given to Perl's eval. 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 '\'. 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 . 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.
Setting variablesLines beginning by @ are 'evaled' using variables defined by
Conditional statementsText::Vpp understands @IF, @ELSIF, @ENDIF,and so on. @INCLUDE and @IF can be nested. @IF and @ELSIF are followed by a Perl expression which will be evaled using
the variables you have defined (either with setVar(),
Loop statementsText::Vpp also understands @FOREACH $MyLoopVar ( Perl-List-Expression ) ... (any) lines which may depend on $MyLoopVar @ENDFOR These loops may be nested.
InclusionText::Vpp understands @INCLUDE Filename or Perl-Expression @INCLUDE { action => '\\', backslash => 0, file => 'add_on.001' } The file name may be a bare words if it contains only alphanumeric characters or '-', '.' or '_'. Otherwise, the file name must be quoted. If the Perl-Expression is a string, it is taken as a filename. 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.
Constructor
new(file, optional_var_hash_ref, ...)The constructor call
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.
Methods
|
|
Text::Vpp - Perl extension for a versatile text pre-processor |