create new tag
, view all tags

Proposed: New Function to Extract all Parameters

The functionality of the current parameter parser TWiki::Func::extractNameValuePair() is insufficient to discover all the parameters of a variable like %TEST{ "nameless" name1="val1" name2="val2" }%. It is also inefficient if you need to parse many parameters since the string parsing is done for each call.

This proposed function returns a hash that can be used easily:

extractAllParameters( $text ) ==> %hash

Description: Extract all parameters from a variable and return them as a hash
Parameter: $text Parameter text between {...} of a function
Return: %hash All parameters extracted and put into a hash; the nameless attribute is put into _DEFAULT

  • Example:
    • Variable: %TEST{ "nameless" name1="val1" name2="val2" }%
    • First extract text between {...} to get: "nameless" name1="val1" name2="val2"
    • Then call this on the text:
      my %params = TWiki::Func::extractAllParameters( $text );
    • The %params hash contains now ( "_DEFAULT" => "nameless", "name1" => "val1", "name2" => "val2" )

-- PeterThoeny - 28 Mar 2004

The Attrs.pm module used by all my plugins does this, and it has a nice interface.

my $attrs = new Attrs( $text );
my $val = $attrs->remove( "param" ); # returns value of param and removes from hash
my $val = $attrs->get( "param" );
$attrs->set( "param", "val" ); # if you need to set a val
$attrs->toString(); # regenerates the original param set
I was thinking of extending it to handle typed parameters as well, such as dates and integers, but I have'nt done so yet.

-- CrawfordCurrie - 28 Mar 2004

Did this go in a release? or is it in DEVELOP?

-- SamHasler - 14 Feb 2005

Edit | Attach | Watch | Print version | History: r4 < r3 < r2 < r1 | Backlinks | Raw View | Raw edit | More topic actions
Topic revision: r4 - 2005-02-14 - SamHasler
  • Learn about TWiki  
  • Download TWiki
This site is powered by the TWiki collaboration platform Powered by Perl Hosted by OICcam.com Ideas, requests, problems regarding TWiki? Send feedback. Ask community in the support forum.
Copyright © 1999-2017 by the contributing authors. All material on this collaboration platform is the property of the contributing authors.