Home Page
make a choice: Photoshop 7 or Photoshop CS/CS 2
Uncle Gugl - a giant
How to name a Web-site?
Spam - a problem of a century
register_globals=oN? You in danger!
Gathering of statistics on PHP
Transformation XML + XSLT with help Sablotron
Patterns of documents and Perl
The signature or avatars on pkhp
Privatnost` in the Internet
Use of the module for job with patterns
Alternative MIME:: Parser and Email:: Simple
Job with sessions in perl
Creation of dynamic forms with help JavaScript
*.JS when to be loaded you think?
Module CGI.pm
XML in 10 theses
Platformo-independent dynamic site - a myth or a reality?
Something about WAP
 

Patterns of documents and Perl

Always, when it is necessary for us to write CGI a script on Perl we collide{face} with that, that the script should return to the user some HTML a code. As a rule, this code is inserted directly into a code of the script. The given approach is not absolutely convenient meaning that at change of design of a site, as a rule, it is necessary to change the text of a script. You likely noticed, that very much often "skriptovaja" the part of the server differs from other part of a site a little. There is it for the reason, that in the code of a script it is rather difficult to change HTML fragments. Somewhere yes will miscalculate.


The second, the most popular, the variant is a removal of variables with HTML in a separate file. It certainly facilitates a life, but does not give full freedom. The script offered{suggested} below will consist only of 30 lines, but he solves all problems described above.


So, for the beginning we shall decide, that all we HTML the data will be stored{kept} in a file - a pattern. We shall name it  html.dot the Format of a file there will be a following:



*! Nazvanie_fragmenta


* The comment


Any text


*! Nazvanie_vtorogo_fragmenta


Still the text


Besides in the text it is possible to use such designs:

@Imja - to substitute value of a variable "Name"

*k¼n - to substitute a fragment with the name "Name". Notice, that substitution goes recursively and in podstavljaemykh blocks as it is possible to use substitutions.


Now we shall write function which would read read this file, transformed it  (did{made} substitutions of fragments) and saved fragments in kheshe. Thus, names of elements khesha, will correspond{meet} to names of fragments.



sub read_from_dot {

1 my (@lines, $line, $curname);


2 open (F, " $ _ [0] ") || die " Can't open file $ _ [0] ";


3 while ($line =) {

4 chomp ($line);

5 if (($line! ~ / ^ * [^!]/) ** ($line ne "*")) {

6 if ($line = ~ / ^ *! \s +/) {

7 $curname = $;

}

9 else {

10 $DOT {$curname}. = "$line\n";

}

}

}

14 close (F);


16 foreach (keys %DOT) {

17 $DOT {$ _} = ~ s / * (. [^] *) / $ DOT {"$1"}/g;

}

}


2 - we open a file. The name of a file should be transferred{handed} as argument of function.

3 - we run on all lines of a file.

4 - we get rid of a symbol of the end of a line.

5 - if the line begins with "*" (and after it{her} costs{stands} not "!") we work with her. Otherwise - it is lost. As it is the comment.

6 - if the line begins on " *! ", this name of the next fragment means.

7 - we shall remember a name of the current fragment.

9-10 - if it simply a line, we add her  in an element khesha with a name equal to a name of the current fragment.

14 - we close a file.


Procedure of recursive substitution


16 - we run on all elements khesha %DOT.

17 - we replace all designs of a kind *NAME with the text of a fragment with a name "NAME" .Vot and all. At us it is ready global khesh %DOT which contains all our patterns.


Let's take for an example such file - pattern:



*! MAIN

*******************************************

*TITLE

It is the main page


*! STYLE

*******************************************

<STYLE>

H1 {color: red}

</STYLE>


*! TITLE

*

* @STRING - a name

*******************************************

<HTML>

<HEAD>

*STYLE

<TITLE> @STRING </TITLE>

</HEAD>


After processing we shall receive khesh from three elements MAIN, STYLE, TITLE.


Well and at last we shall write function which will substitute values of variables. That variables would not be crossed with the variables used in a script, we shall store{keep} them in kheshe %DOT_VAL.



sub insert_values {

1 if ($ _ [0]) {

2 $ _ [0] = ~ s / (. [^] *) / $ DOT_VAL {$1}/g;

}

else {

5 foreach (keys %DOT) {

6 $DOT {$ _} = ~ s / (. [^] *) / $ DOT_VAL {$1}/g;

}

}

}


1 - if function is caused with argument replacement of variables occurs in this argument. Otherwise replacement of variables occurs in all kheshe $DOT.

2 - substitution of variables.

5 - we run on all elements khesha %DOT

6 - we substitute values of variables.

Well and on posledok an example of use of the given code.



read_from_dot ("html.dot");


$DOT_VAL {STRING} = " Welcome to our site!!! ";


insert_values ();


print $DOT {"MAIN"};




© Web Development Company Conkurent, LLC 2007-2009. All rights reserved.