a class to extract sequences from the genome
[cxgn-corelibs.git] / lib / CXGN / Tools / Param.pm
blob387626d1c39caa5ea02e2a5585e3402439ae0492
2 =head1 CXGN::Tools::Param
4 Export OK functions to build URL and Form parameter strings
6 =head1 Author
8 C. Carpita <csc32@cornell.edu>
10 =cut
13 package CXGN::Tools::Param;
14 use strict;
16 use base qw/Exporter/;
18 BEGIN {
19 our @EXPORT_OK = qw/ hash2param hash2hiddenpost /
23 =head2 hash2param()
25 Given a hash reference, build an argument string
27 Args: A common hash ref with parameters, and optional
28 second hash ref with modified parameters
29 Ret: URL param string, starting w/ "&arg1=val1..."
30 Ex: my $argstring = hash2param($std_arghashref, { hide => 1, arg2 => "scooter", formica => "kitsch" });
31 print $argstring; # prints '&otherargs=stuff...&hide=1&arg2=scooter&formica=kitsch'
33 Useful when you maintain settings in a hash and need to modify
34 different arguments and build several different URL's with GET
35 params
37 =cut
39 sub hash2param {
40 my $hashref = shift;
41 my $modifyref = shift;
42 return unless ref($hashref) eq "HASH";
44 my $string = "";
46 my %hash = %$hashref;
47 while(my($k, $v) = each %$modifyref){
48 next unless $k;
49 $hash{$k} = $v;
52 while(my($k, $v) = each %hash){
53 next unless $k && defined $v;
54 $string .= "&$k=$v";
56 return $string;
59 =head2 hash2hiddenpost()
61 Works like hash2param, except it returns several lines of <input type="hidden ..>
62 tags instead of a URL string. Takes a third argument, an array reference of
63 fields to exclude, since another form element may be providing the argument.
65 =cut
67 sub hash2hiddenpost {
68 my $hashref = shift;
69 my $modifyref = shift;
70 my $exclude_array = shift;
71 return unless ref($hashref) eq "HASH";
73 my %hash = %$hashref;
74 if(ref($modifyref) eq "HASH"){
75 while(my($k, $v) = each %$modifyref){
76 next unless $k;
77 $hash{$k} = $v;
80 if(ref($exclude_array) eq "ARRAY"){
81 delete $hash{$_} foreach(@$exclude_array);
84 my $string = "";
85 while(my($k, $v) = each %hash){
86 next unless $k;
87 $v ||= '';
88 $string .= qq{\n<input type="hidden" name="$k" value="$v" />};
90 return $string;