home *** CD-ROM | disk | FTP | other *** search
/ PC Professionell 2004 December / PCpro_2004_12.ISO / files / webserver / tsw / TSW_3.4.0.exe / Apache2 / perl / Utils.pm < prev    next >
Encoding:
Perl POD Document  |  2003-11-12  |  2.4 KB  |  93 lines

  1. # Copyright (c) 1998-2003 by Jonathan Swartz. All rights reserved.
  2. # This program is free software; you can redistribute it and/or modify it
  3. # under the same terms as Perl itself.
  4.  
  5. #
  6. # Miscellaneous Mason-related utilities expected to be used by
  7. # external applications.
  8. #
  9.  
  10. package HTML::Mason::Utils;
  11.  
  12. use HTML::Mason::Tools qw(compress_path);
  13. use strict;
  14.  
  15. require Exporter;
  16.  
  17. use vars qw(@ISA @EXPORT_OK);
  18.  
  19. @ISA = qw(Exporter);
  20. @EXPORT_OK = qw(data_cache_namespace cgi_request_args);
  21.  
  22. sub data_cache_namespace
  23. {
  24.     my ($comp_id) = @_;
  25.     return compress_path($comp_id);
  26. }
  27.  
  28. sub cgi_request_args
  29. {
  30.     my ($q, $method) = @_;
  31.  
  32.     my %args;
  33.  
  34.     # Checking that there really is no query string when the method is
  35.     # not POST is important because otherwise ->url_param returns a
  36.     # parameter named 'keywords' with a value of () (empty array).
  37.     # This is apparently a feature related to <ISINDEX> queries or
  38.     # something (see the CGI.pm) docs.  It makes my head hurt. - dave
  39.     my @methods =
  40.         $method ne 'POST' || ! $ENV{QUERY_STRING} ? ( 'param' ) : ( 'param', 'url_param' );
  41.  
  42.     foreach my $key ( map { $q->$_() } @methods ) {
  43.     next if exists $args{$key};
  44.     my @values = map { $q->$_($key) } @methods;
  45.     $args{$key} = @values == 1 ? $values[0] : \@values;
  46.     }
  47.  
  48.     return wantarray ? %args : \%args;
  49. }
  50.  
  51.  
  52. 1;
  53.  
  54. __END__
  55.  
  56. =head1 NAME
  57.  
  58. HTML::Mason::Utils - Publically available functions useful outside of Mason
  59.  
  60. =head1 DESCRIPTION
  61.  
  62. The functions in this module are useful when you need to interface
  63. code you have written with Mason.
  64.  
  65. =head1 FUNCTIONS
  66.  
  67. =over 4
  68.  
  69. =item data_cache_namespace ($comp_id)
  70.  
  71. Given a component id, this method returns its default
  72. C<Cache::Cache> namespace.  This can be useful if you want to access
  73. the cached data outside of Mason.
  74.  
  75. With a single component root, the component id is just the component
  76. path. With multiple component roots, the component id is
  77. C<key>/C<path>, where C<key> is the key corresponding to the root that
  78. the component falls under.
  79.  
  80. =item cgi_request_args ($cgi, $method)
  81.  
  82. This function expects to receive a C<CGI.pm> object and the request
  83. method (GET, POST, etc).  Given these two things, it will return a
  84. hash in list context or a hashref in scalar context.  The hash(ref)
  85. will contain all the arguments passed via the CGI request.  The keys
  86. will be argument names and the values will be either scalars or array
  87. references.
  88.  
  89. =back
  90.  
  91. =cut
  92.  
  93.