+ }
+
+ sub TELL($)
+ {
+ my($self)=@_;
+
+ return $self->{"offset"};
+ }
+
+ sub TIEHANDLE($$$)
+ {
+ my($class,$data,$fh_orig)=@_;
+
+ my $self=bless {},$class;
+ $self->{"data"}=$data;
+ $self->{"offset"}=0;
+ $self->{"fh_orig"}=$fh_orig;
+ return $self;
+ }
+}
+
+sub cleanup($)
+{
+my($apache_request)=@_;
+
+ cluck "CORE::GLOBAL::exit hook not ran" if !$W->{"_exit_done"};
+ cluck "packages not finalized" if !$packages_used_hash{$W->{"__PACKAGE__"}}{"_done"};
+ cache_finish();
+ # Sanity protection.
+ $W=undef();
+ exit_hook_stop();
+ return OK;
+}
+
+# PerlResponseHandler is RUN_FIRST and &ModPerl::Util::exit returns OK, so no (sane) go.
+# PerlLogHandler is already too late to be able to produce any output.
+my $exit_orig;
+sub exit_hook
+{
+ cluck "Missing ->init while in exit_hook()" if !$W->{"_init_done"};
+ # &footer will call us recursively!
+ footer() if !$W->{"_exit_done"}++;
+ return &{$exit_orig}(@_);
+}
+sub exit_hook_start
+{
+ do { cluck "exit_hook_start() twice?"; return; } if defined $exit_orig;
+ $exit_orig=\&CORE::GLOBAL::exit;
+ # Prevent: Subroutine CORE::GLOBAL::exit redefined
+ no warnings 'redefine';
+ *CORE::GLOBAL::exit=\&exit_hook;
+}
+sub exit_hook_stop
+{
+ do { cluck "exit_hook_stop() without exit_hook_start()?"; return; }
+ if \&exit_hook ne \&CORE::GLOBAL::exit;
+ do { cluck "INTERNAL: exit_orig uninitialized"; return; }
+ if !$exit_orig;
+ # Prevent: Subroutine CORE::GLOBAL::exit redefined
+ no warnings 'redefine';
+ *CORE::GLOBAL::exit=$exit_orig;
+ $exit_orig=undef();
+}
+
+# Be aware other parts of code (non-My::Web) will NOT use this function!
+# Do not: Wprint $W->{"heading"},"undef"=>1;
+# as we would need to undef() it to turn it off and it would get defaulted in such case.
+# Do not: exists $W->{"heading"}
+# as we use a lot of 'for $W->{"heading"}' which instantiates it with the value: undef()
+sub Wprint($%)
+{
+my($text,%args)=@_;
+
+ cluck "undef Wprint" if !defined $text && !$args{"undef"};
+ delete $args{"undef"};
+ cluck join(" ","Invalid arguments:",keys(%args)) if keys(%args);
+ return if !defined $text;
+ # Do not: cluck "utf-8 untested" if Encode::is_utf8($text);
+ # as it is valid here.
+ $W->{"r"}->puts($text);
+}
+
+sub request_check(;$)
+{
+my($self)=@_;
+
+ # Use &eval to prevent: Global $r object is not available. Set:\n\tPerlOptions +GlobalRequest\nin ...
+ confess "Calling sensitive dynamic code from a static code" if !eval { Apache2::RequestUtil->request(); };
+ # Do not: confess "Calling sensitive dynamic code without My::Web::init" if !$W->{"__PACKAGE__"};
+ # as it is valid at least while preparing arguments to call: &project::Lib::init
+}
+
+# Do not: use CGI;
+# as it is too much backward compatible regarding the charset encodings etc.
+# and the resulting code is too dense with no additional functionality for the recent content.
+sub escapeHTML($)
+{
+my($text)=@_;
+
+ local $_=$text;
+ s{&}{&}gso;
+ s{<}{<}gso;
+ s{>}{>}gso;
+ s{"}{"}gso;
+ return $_;
+}
+
+# /home/user/www/webdir
+sub dir_top_abs_disk()
+{
+ our $dir_top_abs_disk;
+ if (!$dir_top_abs_disk) {
+ my $selfpkg_relpath=__PACKAGE__;
+ $selfpkg_relpath=~s{::}{/}g;
+ $selfpkg_relpath.=".pm";
+ my $selfpkg_abspath=$INC{$selfpkg_relpath} or do {
+ cluck "Unable to find self package $selfpkg_relpath";
+ return;
+ };
+ $selfpkg_abspath=~s{/*\Q$selfpkg_relpath\E$}{} or do {
+ cluck "Unable to strip myself \"$selfpkg_relpath\" from the abspath: $selfpkg_abspath";
+ return;
+ };
+ cluck "INC{myself} is relative?: $selfpkg_abspath" if $selfpkg_abspath!~m{^/};
+ $dir_top_abs_disk=$selfpkg_abspath;
+ }
+ return $dir_top_abs_disk;
+}
+
+sub unparsed_uri()
+{
+ request_check();
+ if (!$W->{"unparsed_uri"}) {
+ # Do not: $W->{"r"}
+ # as we may be called before &init from: &My::Project::init
+ my $r=Apache2::RequestUtil->request();
+ cluck "Calling ".'&unparsed_uri'." from a static code, going to fail" if !$r;
+ my $uri_string=$r->unparsed_uri() or cluck "Valid 'r' missing unparsed_uri()?";
+ my $uri=URI->new_abs($uri_string,"http://".$W->{"web_hostname"}."/");
+ $W->{"unparsed_uri"}=$uri;
+ }
+ return $W->{"unparsed_uri"};
+}
+
+sub in_to_uri_abs($)
+{
+my($in)=@_;
+
+ # Otherwise we may have been already processed and thus legally relativized.
+ # FIXME data: Currently disabled, all the data are too violating such rule.
+ if (0 && !ref $in) {
+ my $uri_check=URI->new($in);
+ $uri_check->scheme() || $in=~m{^\Q./\E} || $in=~m{^/}
+ or cluck "Use './' or '/' prefix for all the local references: $in";
+ }
+ my $uri=URI->new_abs($in,unparsed_uri());
+ $uri=$uri->canonical();
+ return $uri;
+}
+
+# $args{"uri_as_in"}=1 to permit passing URI objects as: $in
+# $args{"abs"}=0 || 1; # overrides: $W->{"args"}{"Wabs"}
+sub path_web($%)
+{
+my($in,%args)=@_;
+
+ cluck if !$args{"uri_as_in"} && ref $in;
+ my $uri=in_to_uri_abs($in);
+ if (uri_is_local($uri)) {
+ # Prefer the $uri values over "args_persistent" values.
+ # &query_form_hash comes from: URI::QueryParam
+ $uri->query_form_hash({
+ map({
+ my $key=$_;
+ my $val=$W->{"args"}{$key};
+ (!defined $val ? () : ($key=>$val));
+ } keys(%{$W->{"args_persistent"}})),
+ %{$uri->query_form_hash()},
+ });