home *** CD-ROM | disk | FTP | other *** search
/ PC Professionell 2004 December / PCpro_2004_12.ISO / files / webserver / xampp / xampp-perl-addon-1.4.9-installer.exe / ParamVal.pm < prev    next >
Encoding:
Perl POD Document  |  2002-06-14  |  7.2 KB  |  308 lines

  1. package MIME::Field::ParamVal;
  2.  
  3.  
  4. =head1 NAME
  5.  
  6. MIME::Field::ParamVal - subclass of Mail::Field, for structured MIME fields
  7.  
  8.  
  9. =head1 SYNOPSIS
  10.  
  11.     # Create an object for a content-type field:
  12.     $field = new Mail::Field 'Content-type';
  13.  
  14.     # Set some attributes:
  15.     $field->param('_'        => 'text/html');
  16.     $field->param('charset'  => 'us-ascii');
  17.     $field->param('boundary' => '---ABC---');
  18.  
  19.     # Same:
  20.     $field->set('_'        => 'text/html',
  21.         'charset'  => 'us-ascii',
  22.         'boundary' => '---ABC---');
  23.  
  24.     # Get an attribute, or undefined if not present:
  25.     print "no id!"  if defined($field->param('id'));
  26.  
  27.     # Same, but use empty string for missing values:
  28.     print "no id!"  if ($field->paramstr('id') eq '');
  29.  
  30.     # Output as string:
  31.     print $field->stringify, "\n";
  32.  
  33.  
  34. =head1 DESCRIPTION
  35.  
  36. This is an abstract superclass of most MIME fields.  It handles
  37. fields with a general syntax like this:
  38.  
  39.     Content-Type: Message/Partial;
  40.         number=2; total=3;
  41.         id="oc=jpbe0M2Yt4s@thumper.bellcore.com"
  42.  
  43. Comments are supported I<between> items, like this:
  44.  
  45.     Content-Type: Message/Partial; (a comment)
  46.         number=2  (another comment) ; (yet another comment) total=3;
  47.         id="oc=jpbe0M2Yt4s@thumper.bellcore.com"
  48.  
  49.  
  50. =head1 PUBLIC INTERFACE
  51.  
  52. =over 4
  53.  
  54. =cut
  55.  
  56. #------------------------------
  57.  
  58. require 5.001;
  59.  
  60. # Pragmas:
  61. use strict;
  62. use vars qw($VERSION @ISA);
  63.  
  64. # System modules:
  65.  
  66.  
  67. # Other modules:
  68. use Mail::Field;
  69.  
  70. # Kit modules:
  71. use MIME::Tools qw(:config :msgs);
  72.  
  73. @ISA = qw(Mail::Field);
  74.  
  75.  
  76. #------------------------------
  77. #
  78. # Public globals...
  79. #
  80. #------------------------------
  81.  
  82. # The package version, both in 1.23 style *and* usable by MakeMaker:
  83. $VERSION = substr q$Revision: 5.403 $, 10;
  84.  
  85.  
  86. #------------------------------
  87. #
  88. # Private globals...
  89. #
  90. #------------------------------
  91.  
  92. # Pattern to match parameter names (like fieldnames, but = not allowed):
  93. my $PARAMNAME = '[^\x00-\x1f\x80-\xff :=]+';
  94.  
  95. # Pattern to match the first value on the line:
  96. my $FIRST    = '[^\s\;\x00-\x1f\x80-\xff]+';
  97.  
  98. # Pattern to match an RFC-1521 token:
  99. #
  100. #      token      =  1*<any  (ASCII) CHAR except SPACE, CTLs, or tspecials>
  101. #
  102. my $TSPECIAL = '()<>@,;:\</[]?="';
  103. my $TOKEN    = '[^ \x00-\x1f\x80-\xff' . "\Q$TSPECIAL\E" . ']+';
  104.  
  105. # Encoded token:
  106. my $ENCTOKEN = "=\\?[^?]*\\?[A-Za-z]\\?[^?]+\\?=";
  107.  
  108. # Pattern to match spaces or comments:
  109. my $SPCZ     = '(?:\s|\([^\)]*\))*';
  110.  
  111.  
  112. #------------------------------
  113. #
  114. # Class init...
  115. #
  116. #------------------------------
  117.  
  118. #------------------------------
  119.  
  120. =item set [\%PARAMHASH | KEY=>VAL,...,KEY=>VAL]
  121.  
  122. I<Instance method.>  Set this field.
  123. The paramhash should contain parameter names
  124. in I<all lowercase>, with the special C<"_"> parameter name
  125. signifying the "default" (unnamed) parameter for the field:
  126.  
  127.    # Set up to be...
  128.    #
  129.    #     Content-type: Message/Partial; number=2; total=3; id="ocj=pbe0M2"
  130.    #
  131.    $conttype->set('_'       => 'Message/Partial',
  132.           'number'  => 2,
  133.           'total'   => 3,
  134.           'id'      => "ocj=pbe0M2");
  135.  
  136. Note that a single argument is taken to be a I<reference> to
  137. a paramhash, while multiple args are taken to be the elements
  138. of the paramhash themselves.
  139.  
  140. Supplying undef for a hashref, or an empty set of values, effectively
  141. clears the object.
  142.  
  143. The self object is returned.
  144.  
  145. =cut
  146.  
  147. sub set {
  148.     my $self = shift;
  149.     my $params = ((@_ == 1) ? (shift || {}) : {@_});
  150.     %$self = %$params;    # set 'em
  151.     $self;
  152. }
  153.  
  154. #------------------------------
  155.  
  156. =item parse_params STRING
  157.  
  158. I<Class/instance utility method.>
  159. Extract parameter info from a structured field, and return
  160. it as a hash reference.  For example, here is a field with parameters:
  161.  
  162.     Content-Type: Message/Partial;
  163.         number=2; total=3;
  164.         id="oc=jpbe0M2Yt4s@thumper.bellcore.com"
  165.  
  166. Here is how you'd extract them:
  167.  
  168.     $params = $class->parse_params('content-type');
  169.     if ($$params{'_'} eq 'message/partial') {
  170.         $number = $$params{'number'};
  171.         $total  = $$params{'total'};
  172.         $id     = $$params{'id'};
  173.     }
  174.  
  175. Like field names, parameter names are coerced to lowercase.
  176. The special '_' parameter means the default parameter for the
  177. field.
  178.  
  179. B<NOTE:> This has been provided as a public method to support backwards
  180. compatibility, but you probably shouldn't use it.
  181.  
  182. =cut
  183.  
  184. sub parse_params {
  185.     my ($self, $raw) = @_;
  186.     my %params = ();
  187.     my $param;
  188.  
  189.     # Get raw field, and unfold it:
  190.     defined($raw) or $raw = '';
  191.     $raw =~ s/\n//g;
  192.  
  193.     # Extract special first parameter:
  194.     $raw =~ m/\A$SPCZ($FIRST)$SPCZ/og or return {};    # nada!
  195.     $params{'_'} = $1;
  196.  
  197.     # Extract subsequent parameters.
  198.     # No, we can't just "split" on semicolons: they're legal in quoted strings!
  199.     while (1) {                     # keep chopping away until done...
  200.     $raw =~ m/\G$SPCZ\;$SPCZ/og or last;             # skip leading separator
  201.     $raw =~ m/\G($PARAMNAME)\s*=\s*/og or last;      # give up if not a param
  202.     $param = lc($1);
  203.     $raw =~ m/\G(\"([^\"]+)\")|\G($TOKEN)|\G($ENCTOKEN)/g or last;   # give up if no value
  204.     my ($qstr, $str, $token, $enctoken) = ($1, $2, $3, $4);
  205.     $params{$param} = defined($qstr) ? $str : (defined($token) ? $token : $enctoken);
  206.     debug "   field param <$param> = <$params{$param}>";
  207.     }
  208.  
  209.     # Done:
  210.     \%params;
  211. }
  212.  
  213. #------------------------------
  214.  
  215. =item parse STRING
  216.  
  217. I<Class/instance method.>
  218. Parse the string into the instance.  Any previous information is wiped.
  219. The self object is returned.
  220.  
  221. May also be used as a constructor.
  222.  
  223. =cut
  224.  
  225. sub parse {
  226.     my ($self, $string) = @_;
  227.  
  228.     # Allow use as constructor, for MIME::Head:
  229.     ref($self) or $self = bless({}, $self);
  230.     
  231.     # Get params, and stuff them into the self object:
  232.     $self->set($self->parse_params($string));
  233. }
  234.  
  235. #------------------------------
  236.  
  237. =item param PARAMNAME,[VALUE]
  238.  
  239. I<Instance method.>
  240. Return the given parameter, or undef if it isn't there.
  241. With argument, set the parameter to that VALUE.
  242. The PARAMNAME is case-insensitive.  A "_" refers to the "default" parameter.
  243.  
  244. =cut
  245.  
  246. sub param {
  247.     my ($self, $paramname, $value) = @_;
  248.     $paramname = lc($paramname);
  249.     $self->{$paramname} = $value if (@_ > 2);
  250.     $self->{$paramname}
  251. }
  252.  
  253. #------------------------------
  254.  
  255. =item paramstr PARAMNAME,[VALUE]
  256.  
  257. I<Instance method.>
  258. Like param(): return the given parameter, or I<empty> if it isn't there.
  259. With argument, set the parameter to that VALUE.
  260. The PARAMNAME is case-insensitive.  A "_" refers to the "default" parameter.
  261.  
  262. =cut
  263.  
  264. sub paramstr {
  265.     my $val = shift->param(@_);
  266.     (defined($val) ? $val : '');
  267. }
  268.  
  269. #------------------------------
  270.  
  271. =item stringify
  272.  
  273. I<Instance method.>
  274. Convert the field to a string, and return it.
  275.  
  276. =cut
  277.  
  278. sub stringify {
  279.     my $self = shift;
  280.     my ($key, $val);
  281.  
  282.     my $str = $self->{'_'};                   # default subfield
  283.     foreach $key (sort keys %$self) {
  284.     next if ($key !~ /^[a-z][a-z-_0-9]*$/);  # only lowercase ones!
  285.     defined($val = $self->{$key}) or next;
  286.     $str .= qq{; $key="$val"};
  287.     }
  288.     $str;
  289. }
  290.  
  291. #------------------------------
  292.  
  293. =item tag
  294.  
  295. I<Instance method, abstract.>
  296. Return the tag for this field.
  297.  
  298. =cut
  299.  
  300. sub tag { '' }
  301.  
  302. =back
  303.  
  304. =cut
  305.  
  306. #------------------------------
  307. 1;
  308.