CGI/Perl Guide | Learning Center | Forums | Advertise | Login
Site Search: in

  Main Index MAIN
INDEX
Search Posts SEARCH
POSTS
Who's Online WHO'S
ONLINE
Log in LOG
IN

Home: Perl Programming Help: Intermediate:
Create VAR from KEY names

 



S_Shrum
User

Nov 11, 2001, 11:56 PM

Post #1 of 9 (2892 views)
Create VAR from KEY names Can't Post

I'm pretty sure this is possible but don't know how it's done:

I have a hash from CGI.PM so $input->param('KEYNAME'); that I want to convert to variables of the same name.

I want this because most of the Perl functions seem to have problems dealing with the way the hash is formatted (try passing a $input key to SPLIT(); yuck).

Anyway, I need a way to go throught the $input hash and just make variables of the same name, kinda like this:


Code
$delimiter = $input->param('delimiter')

...but in a loop for all keys.

TIA

Sean Shrum
sean@shrum.net
http://www.shrum.net/programming - Free Perl Scripts
http://www.shrum.net/soaring - Sailplane Soaring and Combat


mhx
Enthusiast / Moderator

Nov 12, 2001, 12:11 AM

Post #2 of 9 (2891 views)
Re: Create VAR from KEY names [In reply to] Can't Post

Although this could be easily done with

Code
$$_ = $input->param($_) for $input->param();

I cannot recommend to do it, because you'd be using soft references. (They're cool, but I don't like them too much.)
The problem you have is that method calls don't interpolate in within strings. So I'd say either copy selected values to variables, or copy all values to a hash:

Code
$in{$_} = $input->param($_) for $input->param();

Or use the following trick to interpolate method calls in strings if you don't need it too often:

Code
split /@{[$input->param('whatever')]}/;

Hope this helps.

-- Marcus


Code
s$$ab21b8d15c3d97bd6317286d$;$"=547269736;split'i',join$,,map{chr(($*+= 
($">>=1)&1?-hex:hex)+0140)}/./g;$"=chr$";s;.;\u$&;for@_[0,2];print"@_,"



S_Shrum
User

Nov 12, 2001, 2:58 PM

Post #3 of 9 (2885 views)
Re: Create VAR from KEY names [In reply to] Can't Post

I guess I'd be willing to use the @{[ .. ]} method, but here's the problem I am having now with this:


Code
if ( index( @{[$input->param('page')]}, "http:" ) > -1 ) }

I am passing a param of:


Code
...&page=http://www.myserver.com/somepage.shtml...

...but the line is returning false (-1).

Any ideas why? If I pass the $input to a soft ref, it works. Is my syntax correct?

TIA

Sean Shrum
sean@shrum.net
http://www.shrum.net/programming - Free Perl Scripts
http://www.shrum.net/soaring - Sailplane Soaring and Combat


mhx
Enthusiast / Moderator

Nov 12, 2001, 7:52 PM

Post #4 of 9 (2883 views)
Re: Create VAR from KEY names [In reply to] Can't Post

You don't need that trick with the index function. You only need it if you want to interpolate function/method calls within strings.

-- Marcus


Code
s$$ab21b8d15c3d97bd6317286d$;$"=547269736;split'i',join$,,map{chr(($*+= 
($">>=1)&1?-hex:hex)+0140)}/./g;$"=chr$";s;.;\u$&;for@_[0,2];print"@_,"



yapp
User

Nov 13, 2001, 3:46 AM

Post #5 of 9 (2881 views)
Re: Create VAR from KEY names [In reply to] Can't Post

I think I know what you mean...
... just like in PHP, you want to have the variables have the same name as their key=value&key=value pairs at the request line.

I though about this once, but I never implemented it.

This might do the trick.
a. Write a new module, ie: SShrum::CGI::ParamNames
b. That module uses the param() function from the cgi module.
c. Next, iliterate through the returned array by param(),
and make a list of variables. You can use that list in a use vars directive, to make the variables.
d. Let your package export that list of variables...
e. type this in your package:

Code
... 
package main; # swich back to main for a short period....
use vars @YOUR::MODUKE::Parameters;
package YOUR::MODUKE; @ swich back

download it
I've created a XProgramming::CGI::Location module that detects the location of the current CGI script, and adds some variables into the main package... (like $THIS_PATH, $THIS_SITE etc...)

Therefore, take a look at http://www.cool-programming.f2s.com
Or download it directly http://www.cool-programming.f2s.com/cgi-bin/x-download.cgi?id=xmodules

Yet an Other Perl Programmer

_________________________________
Find out more about programming
http://www.cool-programming.f2s.com


yapp
User

Nov 13, 2001, 7:22 AM

Post #6 of 9 (2875 views)
Import Variables like in PHP [In reply to] Can't Post

Well...

As soon as I got home, I tried making a module as I've explained in my previous post. The trick is, that if a test=done pair is posted at the request line, a value names $test has the 'done'!!!

here is the full script:

./testparam.cgi

Code
#/usr/bin/perl -w 

# Call this script like: testparam.cgi?test=a_value_you_like

use CGI qw(:cgi);
# You can activate the next two lines if the modules are installed...
#use XProgramming::CGI::Location;
#use XProgramming::CGI::ErrorTrap;
use XProgramming::CGI::Params; # Receive script parameters just like in PHP

print header;
print "Parameter: $test";


Now install the required module...
You need to make a directory, named ./XProgramming::CGI and place the file Params.pm in it. You may change the directory, if you also change every line where this directory is written.

Param.pm

Code
package XProgramming::CGI::Params; 

# This file should be located in the directory
# ./XProgramming/CGI
# The . represents the directory where the cgi-script is located.
# Under Microsoft IIS however, the . is the www-root,
# unless you load XProgramming::CGI::Location first. (fixes the problem)

#
# Diederik van der Boor
#
# webmaster@cool-programming.f2s.com
# http://www.cool-programming.f2s.com
#
# Original concept from a topic posted at
# http://forums.perlguru.com
# by Sean Shrum
# http://www.shrum.net/programming


use strict;
use CGI qw(param);


######################################################################################################
## Make the file settings...

BEGIN
{
use Exporter ();
use vars qw($VERSION @ISA @EXPORT @EXPORT_OK %EXPORT_TAGS);
use vars qw(@VARNAMES $PARAM_VALUE);

push @VARNAMES, "\$$_" foreach param();

$VERSION = 1.00;
@ISA = qw(Exporter);

@EXPORT = qw(@VARNAMES $PARAM_VALUE); # By default
@EXPORT_OK = (); # By request
%EXPORT_TAGS = (); # By tag
}


# Load in the variable names
package main;
# Errors that happen in this import are reported at errors in the main package...
use vars @XProgramming::CGI::Params::VARNAMES;
package XProgramming::CGI::Params;

# Assign their values...
foreach my $Name (@VARNAMES)
{
my $VarName = substr($Name, 1);
$XProgramming::CGI::Params::PARAM_VALUE = param($VarName);

# Execute : $main::the_var_name = $XProgramming::CGI::Param::PARAM_VALUE;
eval('$main::' .$VarName. ' = $XProgramming::CGI::Params::PARAM_VALUE');
}

1;


__END__

=head1 NAME

XProgramming::CGI::Param - Load CGI Parameters as Variables

=head1 SYNOPSIS

use XProgramming::CGI::Param;

print "Parameter test at commandline is $test\n";

=head1 DESCRIPTION

This module adds all pameters received by the cgi script
to the main package, as variable names, like PHP does aswell.
Example:

If the webbrowser requests
script.cgi?product=34532&page=2

script.cgi now has two new variables in the main package,
called $product and $page.


=head1 AUTHOR

Diederik van der Boor

webmaster@cool-programming.f2s.com
http://www.cool-programming.f2s.com

Original concept from a topic posted at
http://forums.perlguru.com
by Sean Shrum
http://www.shrum.net/programming

=cut
# The AUTHOR text needs some change.
# I didn't put my copyright under it...



I think you can help yourself out now...


Yet an Other Perl Programmer

_________________________________
Find out more about programming
http://www.cool-programming.f2s.com


Kanji
User

Nov 13, 2001, 3:11 PM

Post #7 of 9 (2869 views)
Re: Create VAR from KEY names [In reply to] Can't Post

CGi already lets you do something along these lines with it's import_names function/method...


Code
$input->import_names('IN'); # import param()s into %IN::

... so that you can then use $IN::delimiter or @IN::delimiter (multi-valued params being something all the other proposed solutions ignore...) instead of $input->param('delimiter').



S_Shrum
User

Nov 13, 2001, 3:56 PM

Post #8 of 9 (2868 views)
Re: Create VAR from KEY names [In reply to] Can't Post

If I use this method, does it work in function calls like SPLIT and GREP without having to interpolate out the result?

Sean Shrum
sean@shrum.net
http://www.shrum.net/programming - Free Perl Scripts
http://www.shrum.net/soaring - Sailplane Soaring and Combat


Kanji
User

Nov 13, 2001, 8:14 PM

Post #9 of 9 (2866 views)
Re: Create VAR from KEY names [In reply to] Can't Post

Try it. :-)


 
 


Search for (options) Powered by Gossamer Forum v.1.2.0

Web Applications & Managed Hosting Powered by Gossamer Threads
Visit our Mailing List Archives