Home : Einführung : Embperl::Object
Google Web perl.apache.org

Embperl 2 Advanced
Weitere Infos
Hinzufügen Infos

    Stable 2.4.0
    Beta 2.5.0_3
Unterstützen Sie Embperl! Mehr...
Website-Global Variables
[ << Prev: Hello World ] [ Content ] [ Next: Modular Files >> ]

Now let's look at a slightly more interesting example. When you create Perl variables in Embperl usually, their scope is the current file; so, they are effectively "local" to that file. When you come to split your website up into modules, however, it quickly becomes apparent that it is very useful to have variables which are global to the website, i.e. shared between multiple files.

To achieve this, EmbperlObject has special object which is automatically passed to every page as it is executed. This object is usually referred to as the "Request" object, because we get one of these objects created for every document request that the web server receives. This object is passed in on the stack, so you can retrieve it using the Perl "shift" statement. This object is also automatically destroyed after the request, so the Request object cannot be used to store data between requests. The idea is that you can store variables which are local to the current request, and shared between all documents on the current website; plus, as we'll see later, we can also use it to call object methods. For example, Let's say you set up some variables in base.epl, and then use them in file.html:


<HTML> <HEAD> <TITLE>Some title</TITLE> </HEAD> [- $req = shift; $req->{webmaster} = 'John Smith' -] <BODY> [- Execute ('*') -] </BODY> </HTML>


[- $req = shift -] Please send all suggestions to [+ $req->{webmaster} +].

You can see that EmbperlObject is allowing us to set up global variables in one place, and share them throughout the website. If you place base.epl in the root document directory, you can have any number of other files in this and subdirectories, and they will all get these variables whenever they are executed. No matter which file is requested, /base.epl is executed first, and then the requested file.

You don't even need to include the requested '*' file, but the usual case would be to do so - it would be a little odd to completely ignore the requested file!

[ << Prev: Hello World ] [ Content ] [ Next: Modular Files >> ]

© 1997-2012 Gerald Richter / ecos gmbh