![Phân tích tư tưởng của nhân dân qua đoạn thơ: Những người vợ nhớ chồng… Những cuộc đời đã hóa sông núi ta trong Đất nước của Nguyễn Khoa Điềm](https://timtailieu.net/upload/document/136415/phan-tich-tu-tuong-cua-nhan-dan-qua-doan-tho-039-039-nhung-nguoi-vo-nho-chong-nhung-cuoc-doi-da-hoa-song-nui-ta-039-039-trong-dat-nuoc-cua-nguyen-khoa-136415.jpg)
Embedding Perl in HTML with Mason Chapter 11: Recipes- P1
Số trang: 19
Loại file: pdf
Dung lượng: 43.43 KB
Lượt xem: 6
Lượt tải: 0
Xem trước 2 trang đầu tiên của tài liệu này:
Thông tin tài liệu:
Tham khảo tài liệu embedding perl in html with mason chapter 11: recipes- p1, công nghệ thông tin, kỹ thuật lập trình phục vụ nhu cầu học tập, nghiên cứu và làm việc hiệu quả
Nội dung trích xuất từ tài liệu:
Embedding Perl in HTML with Mason Chapter 11: Recipes- P1 Chapter 11: Recipes- P1No, we are not going teach you how to make a delicious tofu and soybeanstew. But this is almost as good. This chapter shows how to do somecommon Mason tasks, some of them with more than one implementation.SessionsFor many of our session examples, we will be using theApache::Session module. Despite its name, this module doesntactually require mod_perl or Apache, though that is the context in which itwas born and in which its most often used. It implements a simple tied hashinterface to a persistent object.1 It has one major gotcha: you must make surethat the session object gets cleaned up properly (usually by letting it go outof scope), so that it will be written to disk after each access.Nowadays, you might want to take a look atMasonX::Request::WithApacheSession on CPAN, if you really want tointegrate sessions into your Mason code. Again, Id prefer Catalyst forsession management.Dave, 2007Without Touching httpd.confHere is an example that doesnt involve changing any of your Apacheconfiguration settings. The following code should be placed in a top-levelautohandler. Any component that needs to use the session will have toinherit from this component, either directly or via a longer inheritance chain.It uses cookies to store the session. use Apache::Cookie; use Apache::Session::File; my %c = Apache::Cookie->fetch; my $session_id = exists $c{masonbook_session} ?$c{masonbook_session}->value : undef;First, it loads the necessary modules. Normally we recommend that you dothis at server startup via a PerlModule directive in your httpd.conf file or inyour handler.pl file to save memory, but we load them here just to show youwhich ones we are using. The component uses the Apache::Cookiemodule to fetch any cookies that might have been sent by the browser. Thenwe check for the existence of a cookie called masonbook_session,which if it exists should contain a valid session ID. local *MasonBook::Session; eval { tie %MasonBook::Session,Apache::Session::File, $session_id, { Directory => /tmp/sessions, LockDirectory => /tmp/sessions, }; }; if ($@) { die $@ unless $@ =~ /Object does not exist/;# Re-throw $m->redirect(/bad_session.html); }The first line ensures that when this component ends, the session variablewill go out of scope, which triggers Apache::Sessions cleanupmechanisms. This is quite important, as otherwise the data will never bewritten to disk. Even worse, Apache::Session may still be maintainingvarious locks internally, leading to deadlock. We use local()to localize the symbol table entry *MasonBook::Session; its notenough to localize just the hash %MasonBook::Session, because thetie()magic is attached to the symbol table entry. Its also worth mentioning thatwe use a global variable rather than a lexical one, because we want thisvariable to be available to all components.If the value in the $session_id variable is undef, that is not a problem.The Apache::Session module simply creates a new session ID.However, if $session_id is defined but does not represent a validsession, an exception will be thrown. This means either that the userssession has expired or that shes trying to feed us a bogus ID. Either way, wewant to tell her whats happened, so we redirect to another page that willexplain things. To trap the exception, we wrap the tie() in an eval {}block.If an exception is thrown, we check $@ to see whether the message indicatesthat the session isnt valid. Any other error is fatal. If the session isnt valid,we use the redirect() method provided by the request object.Finally, we send the user a cookie: Apache::Cookie->new( $r, name => masonbook_session, value =>$MasonBook::Session{_session_id}, path => /, expires => +1d, )->bake;This simply uses the Apache::Cookie module to ensure that a cookiewill be sent to the client with the response headers. This cookie is calledmasonbook_session and is the one we checked for earlier. It doesnthurt to send the cookie every time a page is viewed, though this will resetthe expiration time of the cookie each time it is set. If you want the cookie topersist for only a certain fixed length of time after the session is created,dont resend the cookie. $m->call_next; This line simply calls the next component in the inheritance chain.Presumably, other components down the line may change the contents of%MasonBook::Session, and those modifications will be written ...
Nội dung trích xuất từ tài liệu:
Embedding Perl in HTML with Mason Chapter 11: Recipes- P1 Chapter 11: Recipes- P1No, we are not going teach you how to make a delicious tofu and soybeanstew. But this is almost as good. This chapter shows how to do somecommon Mason tasks, some of them with more than one implementation.SessionsFor many of our session examples, we will be using theApache::Session module. Despite its name, this module doesntactually require mod_perl or Apache, though that is the context in which itwas born and in which its most often used. It implements a simple tied hashinterface to a persistent object.1 It has one major gotcha: you must make surethat the session object gets cleaned up properly (usually by letting it go outof scope), so that it will be written to disk after each access.Nowadays, you might want to take a look atMasonX::Request::WithApacheSession on CPAN, if you really want tointegrate sessions into your Mason code. Again, Id prefer Catalyst forsession management.Dave, 2007Without Touching httpd.confHere is an example that doesnt involve changing any of your Apacheconfiguration settings. The following code should be placed in a top-levelautohandler. Any component that needs to use the session will have toinherit from this component, either directly or via a longer inheritance chain.It uses cookies to store the session. use Apache::Cookie; use Apache::Session::File; my %c = Apache::Cookie->fetch; my $session_id = exists $c{masonbook_session} ?$c{masonbook_session}->value : undef;First, it loads the necessary modules. Normally we recommend that you dothis at server startup via a PerlModule directive in your httpd.conf file or inyour handler.pl file to save memory, but we load them here just to show youwhich ones we are using. The component uses the Apache::Cookiemodule to fetch any cookies that might have been sent by the browser. Thenwe check for the existence of a cookie called masonbook_session,which if it exists should contain a valid session ID. local *MasonBook::Session; eval { tie %MasonBook::Session,Apache::Session::File, $session_id, { Directory => /tmp/sessions, LockDirectory => /tmp/sessions, }; }; if ($@) { die $@ unless $@ =~ /Object does not exist/;# Re-throw $m->redirect(/bad_session.html); }The first line ensures that when this component ends, the session variablewill go out of scope, which triggers Apache::Sessions cleanupmechanisms. This is quite important, as otherwise the data will never bewritten to disk. Even worse, Apache::Session may still be maintainingvarious locks internally, leading to deadlock. We use local()to localize the symbol table entry *MasonBook::Session; its notenough to localize just the hash %MasonBook::Session, because thetie()magic is attached to the symbol table entry. Its also worth mentioning thatwe use a global variable rather than a lexical one, because we want thisvariable to be available to all components.If the value in the $session_id variable is undef, that is not a problem.The Apache::Session module simply creates a new session ID.However, if $session_id is defined but does not represent a validsession, an exception will be thrown. This means either that the userssession has expired or that shes trying to feed us a bogus ID. Either way, wewant to tell her whats happened, so we redirect to another page that willexplain things. To trap the exception, we wrap the tie() in an eval {}block.If an exception is thrown, we check $@ to see whether the message indicatesthat the session isnt valid. Any other error is fatal. If the session isnt valid,we use the redirect() method provided by the request object.Finally, we send the user a cookie: Apache::Cookie->new( $r, name => masonbook_session, value =>$MasonBook::Session{_session_id}, path => /, expires => +1d, )->bake;This simply uses the Apache::Cookie module to ensure that a cookiewill be sent to the client with the response headers. This cookie is calledmasonbook_session and is the one we checked for earlier. It doesnthurt to send the cookie every time a page is viewed, though this will resetthe expiration time of the cookie each time it is set. If you want the cookie topersist for only a certain fixed length of time after the session is created,dont resend the cookie. $m->call_next; This line simply calls the next component in the inheritance chain.Presumably, other components down the line may change the contents of%MasonBook::Session, and those modifications will be written ...
Tìm kiếm theo từ khóa liên quan:
thủ thuật máy tính công nghệ thông tin tin học quản trị mạng computer networkTài liệu liên quan:
-
52 trang 441 1 0
-
24 trang 366 1 0
-
Top 10 mẹo 'đơn giản nhưng hữu ích' trong nhiếp ảnh
11 trang 332 0 0 -
Làm việc với Read Only Domain Controllers
20 trang 323 0 0 -
74 trang 310 0 0
-
96 trang 307 0 0
-
Báo cáo thực tập thực tế: Nghiên cứu và xây dựng website bằng Wordpress
24 trang 299 0 0 -
Đồ án tốt nghiệp: Xây dựng ứng dụng di động android quản lý khách hàng cắt tóc
81 trang 293 0 0 -
Tài liệu dạy học môn Tin học trong chương trình đào tạo trình độ cao đẳng
348 trang 291 1 0 -
EBay - Internet và câu chuyện thần kỳ: Phần 1
143 trang 279 0 0