pass argument to perl script

how can i pass an argument to perl script when i call it, for example when a link is clicked it goes to a certain url , but i want the address  to remain the same so i thought if the site has different links and they all call the same perl script but with different arguments i can view different pages.

if there is any other way to go to other pages without changing the url  please tell me
Thanks
LVL 1
omashhourAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

kanduraCommented:
How is your script going to be able to tell which page you want to show, if the urls are all the same?
Would you object to urls with a query string?

e.g.
    http://myhost/script?page=this
    http://myhost/script?page=that
    http://myhost/script?page=such
    http://myhost/script?page=so

If that's okay, then you could use something like the following to get it going. Please note that CGI::Application wraps this up for you in a much cleaner, safer, and more extensible way. Consider using that instead.

    #!/usr/bin/perl -w
    use strict;
    use warnings;
    use CGI;

    my $q = new CGI;
    my %page_map = (
        this    => \&show_this,
        that    => \&show_that,
        # etc. mapping "page=" query string to subs
    );

    my $page = $q->param('page') || 'this';

    print $q->header;
    print $page_map{$page}->();

    ### end of script, run modes follow
    sub show_this {
        return "This is this";
    }

    sub show_that {
        return "And that is that";
    }
   
0
manav_mathurCommented:
The arguments that you are talking about are the objects of the form which calls your scripts. The above method shown will accept "page" as a FORM object (a hidden parameter).

Such form objects are returned through the Vars() method of CGI.
my %par_value_hash = $query->Vars ;

print "$par_value_hash{'page'}"

will print "this" or "that" depending on how the value of parameter 'page' was set in your FORM object when the script was called.

A usual method is to have hidden parameters in your FORM object like

<!--some part of your HTML-->
<form action="cgi-bin/your_script.pl">
<input type=hidden name="page" value="This">
<input type="submit" value="Click for this">
</form>
...
...
...
<!--another part of your HTML-->
<form action="cgi-bin/your_script.pl">
<input type=hidden name="page" value="That">
<input type="submit" value="Click for that">
</form>
...
...
</HTML>
(Please pardon my HTML, and any lingo I've used in that context)

Then your_script.pl can get to know which part called it as follows

#!/usr/bin/perl
use strict ;
use warnings ;
use CGI ;
my $query = new CGI;
my %form_value_hash = $query->Vars ;
if ($form_value_hash{'page'} eq 'This') {
##do something for "This"
elsif ($form_value_hash{'page'} eq 'That') {
##do something
} else {
##this may be another part having another value
}

However, Kandura's code looks more elegant. What I posted for just for descriptive purposes.....

Manav



0
kanduraCommented:
manav_mathur,
> The arguments that you are talking about are the objects of the form
> which calls your scripts. The above method shown will accept "page" as
> a FORM object (a hidden parameter).

or simply query parameters, which you can specify in a link:

    <a href="/cgi-bin/script?page=this">Show this</a>

> Such form objects are returned through the Vars() method of CGI.
> my %par_value_hash = $query->Vars ;

or directly with the param() method of CGI:

    my $page = $q->param('page');


> However, Kandura's code looks more elegant.

Thanks for the compliment :)
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Cloud Class® Course: MCSA MCSE Windows Server 2012

This course teaches how to install and configure Windows Server 2012 R2.  It is the first step on your path to becoming a Microsoft Certified Solutions Expert (MCSE).

kanduraCommented:
I just spotted a glaring error in my script:

> my $page = $q->param('page') || 'this';

> print $page_map{$page}->();

This will die with a nasty error message if the value for 'page' is not a key in the page_map. It's also case sensitive, which - depending on your point of view - is either a feature or a bug :)

So we need to check if the page parameter is specified _and_ valid:

    my $page = $q->param('page') || 'this'; # makes 'this' the default page
    $page = 'no_such_screen' unless $page_map{$page};
    # $page = lc($page); # if you want case-insensitive page values

and add a 'no_such_screen' runmode to %page_map to catch non-existent pages.
0
omashhourAuthor Commented:
i had something else in mind

if i have a perl script with more than one if statement

if ($page == 1){
# i will read an html file
}
else if($page ==2){
# another html file
}

and the url called is supposed to be this perl script with the $page variable as the argument
0
manav_mathurCommented:
- From where would this Perl script be called.....an HTML form or from your browser address-bar??

- What do you want to do when you say
"# i will read an html file"
do you want a redirection to different HTML pages based on value of $page??

Also, if I am understanding correctly, in your browser addressbox, you will have to put something like
http://www.your_web_page.com/cgi-bin/this_script.pl?page=1
or
http://www.your_web_page.com/cgi-bin/this_script.pl?page=2

and then the browser should display the correct header page.....Is this right??

Manav
0
kanduraCommented:
you don't have something else in mind. you're just confused as to how http works: you pass arguments in the query string.

0
omashhourAuthor Commented:
sorry i just wokeup when i read the first 4 replies  :)  i wasnt concentrating :)

><a href="/cgi-bin/script?page=this">Show this</a>  <== this is what i needed

thanks for your help :)
0
kanduraCommented:
omashhour,
> sorry i just wokeup

and you hadn't had any coffee yet, I suppose? ;^)

glad to be of help :)
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Perl

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.