?
Solved

pass argument to perl script

Posted on 2005-03-17
9
Medium Priority
?
4,754 Views
Last Modified: 2012-08-14
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
0
Comment
Question by:omashhour
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 5
  • 2
  • 2
9 Comments
 
LVL 18

Expert Comment

by:kandura
ID: 13563451
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
 
LVL 16

Expert Comment

by:manav_mathur
ID: 13563891
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
 
LVL 18

Accepted Solution

by:
kandura earned 600 total points
ID: 13564130
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
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 18

Expert Comment

by:kandura
ID: 13564210
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
 
LVL 1

Author Comment

by:omashhour
ID: 13564793
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
 
LVL 16

Expert Comment

by:manav_mathur
ID: 13564847
- 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
 
LVL 18

Expert Comment

by:kandura
ID: 13564851
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
 
LVL 1

Author Comment

by:omashhour
ID: 13565147
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
 
LVL 18

Expert Comment

by:kandura
ID: 13565219
omashhour,
> sorry i just wokeup

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

glad to be of help :)
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Many time we need to work with multiple files all together. If its windows system then we can use some GUI based editor to accomplish our task. But what if you are on putty or have only CLI(Command Line Interface) as an option to  edit your files. I…
Checking the Alert Log in AWS RDS Oracle can be a pain through their user interface.  I made a script to download the Alert Log, look for errors, and email me the trace files.  In this article I'll describe what I did and share my script.
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
Six Sigma Control Plans

752 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question