passing variables to perl script from html - html

I am trying to call a perl script from my HTML page. The way am trying to do is to call the url of the perl script located on the server.
Here is the piece of code:
HTML:
var fname = "Bob";
var url='http://xxx.com:30000/cgi-bin/abc.pl?title=fname';
window.open(url,"_self");
The way am trying to retrieve it in perl as:
Perl:
print "$ARGV[0]\n";
Now, I have 3 questions:
I think this is the correct way to pass the variables but am not able to print the argument in perl.
If i want to pass another variable lname, how do i append it to the url?
My window.open should open the output in the same window, since it uses the parameter _self. Still it doesn't.
Could anybody point out the problems?
Thanks,
Buzz

No #ARGV contains command line arguments and will be empty.
You need the CGI module
use warnings;
use strict;
use CGI;
my $query = CGI->new;
print $query->param( 'title' );
Edit:
Take a look at dan1111's answer on how to generate HTML and display it in the browser.

In addition to what Matteo said, a simple print statement is not enough to send some output to the browser.
Please see a recent answer I wrote giving a sample CGI script with output.
In regard to your other issues:
Variables are appended to a url separated with &:
var url='http://xxx.com:30000/cgi-bin/abc.pl?title=fname&description=blah';
Based on this question, perhaps you should try window.location.href = url; instead (though that doesn't explain why your code isn't working).

There are two different environments that each pass variables two different ways. The command line can pass variables through the #ARGV and the browser can pass variables through #ENV. It doesn't matter what language you use, those are the arrays that you will have to employ.

Related

Extracting values from hash created by perl JSON::Syck::Load

I've got a very simple Perl issue that I can't for the life of me figure out.
I'm consuming JSON formatted data from a REST endpoint in a perl script. The data is shaped like this:
{
"ScriptRunning": true
}
There's some other stuff, but really all I care about is the ScriptRunning tag. I'm consuming this data using JSON::Syck::Load like so:
my $running_scripts = JSON::Syck::Load($output_from_rest_call)
(in my current environment it is not possible to get other libraries for CPAN, so I'm stuck with that). All that is working correctly as far as I can tell, I used Data::Dumper to confirm the data looks good:
$VAR1 = {
'ScriptRunning' => 1 # or '' if false
}
However, I can't figure out how to actually get the value of 'ScriptRunning'. I've done print ref $running_scripts and confirmed that it is a HASH, however when I try to index into the hash I'm not getting anything. I've tried the following:
my $script_is_running = $running_scripts{'ScriptRunning'};
my $script_is_running = $running_scripts{ScriptRunning};
my $keys_in_running_scripts = keys $running_scripts; # keys_in_running_scripts is empty
my $keys_in_running_scripts = keys %running_scripts; # keys_in_running_scripts is empty
Any ideas?
You need to use strict; (and use warnings; while you are at it, maybe use diagnostics; too, when you are really stuck). As a general rule, ALWAYS use strict; and use warnings; because they prevent problematic code from running and give you some much more helpful output.
You should also read perldoc perlreftut, which helps explain what you are dealing with.
Your variable $running_scripts is not a hash, but a "hash reference", which is an important distinction. When you call ref on a real hash, it returns a false value, since it is not a reference.
What you need to do is "dereference" using the arrow operator to get the value.
To get the keys call to work, there's a separate syntax for dereferencing.
my $script_is_running = $running_scripts->{ScriptRunning};
my #keys_in_running_scripts = keys %{$running_scripts};

How to make public function

I would like to define function in file functions.ps1 and then call it from another script. Something like this:
Functions.ps1:
Function Hi()
{
"hi"
}
Call it from another script (Call.ps1).
Call.ps1:
invoke-expression -Command .\functions.ps1
Hi
But function is defined in local scope of script functions.ps1 and I get err:
The term 'hi' is not recognized as the name of a cmdlet, function,
script file , or operable program. Check the spelling of the name, or
if a path was included, v erify that the path is correct and try
again.
Is there a simple way to fix this ?
You have to dotsource your script to load it into your current runspace:
. .\functions.ps1
Hi
I think the way to do this would be to dot soruce the file in the script that should use the function(s).
So in the script that will use the function put something like this.
. .\functions.ps1
Then you can start calling funcitons in that file.
A bit by memory , but think it would work.
Edit: removed a brainfart.. :)
Yup, dotsource is what you're looking for.
If your script contains spaces, you would dot source it like this
."C:\Dotsourced script.ps1"
Without space, is as the other people say
.C:\function1.ps1
All the functions and logic contained in the dotsourced script will be loaded upon sourcing it. So try to keep the scripts your'e dotsourcing in functions, otherwise it will run when sourced.
Read more about it on SS64 for example:
https://ss64.com/ps/source.html

Pass values and parameters from HTML to CGI

I am using post method in html to pass the parameters and values to cgi file. From that cgi file I'm extracting the values and parameters. I have tried the code below,
HTML:
<form action="http://hostname.com/manipulation.cgi?name5=value5&name6=value6&name7=value7&name8=value8" method="POST"> </form>
CGI: manipulation.cgi
use strict;
use CGI;
my $query ->new CGI;
print $query->header();
my #paramNames = $query->param; #Receive N number of parameters
my #paramValues = map $query->param($_), $query->param; #Receive N number of parameters values
print $query->h4("#paramNames,#paramValues");
But I'm not able to get the expected output.
Please help me on this.
Thanks in advance.
url_param — get a parameter from the URL.
use strict;
use CGI;
my $query ->new CGI;
print $query->header();
my #paramNames = $query->url_param; #Receive N number of parameters
my #paramValues = map $query->url_param($_), $query->url_param; #Receive N number of parameters values
print $query->h4("#paramNames,#paramValues");
What you are doing makes no sense. If all of your parameters are in the URL, then why are you using POST, not GET? In fact, why are you using a form at all?
If you are using GET then param will give you the parameters from the URL.
If you are using POST then param will give you the parameters from the HTTP request body and url_param will give you the parameters from the URL.
So it seems that you need to switch to using url_param.
It's worth pointing out that learning CGI in 2014 is a bit like learning to use a typewriter. It will work, and some of the skills will be useful. But you'd be far better advised to learn something a little more up to date.

Need suggestion in printing the matched result one by one by using HTML-TreeBuilder-XPath findnodes() method

I am parsing an html content by using HTML-TreeBuilder-XPath
in Perl . i have got the xpath location of the data i need. The issue i am facing is ,There are several matches of the xpath $html->findnodes()which is returned by single result ,but i need to print it one by one. Need some suggestion .Thank you.
You can iterate over using
for my $node (#$paraelements) { ..... }
A more complete example
use HTML::TreeBuilder::XPath;
my $tree= HTML::TreeBuilder::XPath->new;
$tree->parse_file( "mypage.html");
my $paraelements= $tree->findnodes( '//p') ;
for my $node (#$paraelements) {
say $node->as_HTML() ;
}
It returns a reference to a list (ARRAYREF). To get back the list, put a # before the variable to tell Perl to treat that as the [location/memory address] of the list just as JIT's sample code

Display output in HTML format by perl

I have a hashmap with some information(key and value) in a perl file. I want to display them in HTML output and each displayed (key, value) will link to something. When I click the link then there will be some information there.
Anyone suggests me how can I do that. Is this similar to creating a CGI file and use CGI.pm? I will update more detail on this question later.
Yes, you can use the excellent CGI module to render HTML content for you, even if you are not processing CGI forms (i.e. use the module only on output, rather than also for input processing):
use CGI;
my $q = CGI->new;
my #html_list = map {
$q->li($_ . ": " . $hash{$_};
} keys %hash;
print $q->ul($q->li({-type=>'foo'}, #html_list);
Depending on the data you're trying to display, something like HTML::Table may be useful if you want to display it in tabular format and don't want the drugery of assembling the appropriate HTML yourself.
For instance, you could do something like:
my $table = HTML::Table->new(-columns => 2);
for my $key (sort keys %hash) {
$table->addRow($key, $hash{$key});
}
$table->print;
Also, there is a free Beginning Perl book available online, which has a chapter devoted to CGI scripts, along with a lot of other useful information.
If this is more than just a simple one-off script, you might also wish to consider using one of the many Perl web frameworks like Dancer, Catalyst, Mojo etc.