How do you generate links for paginator in Perl? [closed] - perl

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
Each link has the same url as the current requested uri except that the page parameter may differ.
How do you generate such links in Perl?

You do not seem to know the basics. Go read an introductory book or two about Web programming.
Construct a URI object.
use URI qw();
In CGI, piece it together from the enviroment. (Stackers, is there a better way/convenience method I've overlooked?)
my $current = 'http://example.com/?search=foobar';
my $u = URI->new($current);
In PSGI, use the uri method.
use Plack::Request qw();
…
my $req = Plack::Request->new($env);
my $u = $req->uri;
Higher-level frameworks should provide their own accessors. In Catalyst:
my $u = $c->request->uri;
Mutate the query string to include the paging parameter.
use URI::QueryParam qw();
$u->query_param(page => 13);
$u->as_string; # returns http://example.com/?search=foobar&page=13
The query_param DTRT and overwrites the parameter even if it's already set.
$u->query_param(page => 42);
$u->as_string; # returns http://example.com/?search=foobar&page=42

Related

How to store an array in session in Perl [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 5 years ago.
Improve this question
I am using Mojolicious Perl framework in my application. I want to store an array in session, but is not successful.
my #returnResult;
$returnResult['fn'] = $decoded->{'fn'};
$returnResult['ln'] = $decoded->{'ln'};
$self->session(returnResult => #returnResult);
Please help.
See hashes in Modern Perl and perldata.
my %return_result;
$returnResult{fn} = $decoded->{fn};
$returnResult{ln} = $decoded->{ln};
or
my %return_result = (
fn => $decoded->{fn},
ln => $decoded->{ln},
);
or simply
# http://perldoc.perl.org/perl5200delta.html#New-slice-syntax
my %return_result = %$decoded{qw(fn ln)};
You do not get automatic references like in other languages. Use the \ operator.
$self->session(returnResult => \%return_result);

Remove common values from multidimension array perl [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 7 years ago.
Improve this question
I want remove common element from array. For example:
array1 =
[
{'id'=>78597,'data'=>'great'}
];
array2=
[
{'id'=>78345,'data'=>'first'},{'id'=>78597,'data'=>'great'},
{'id'=>78355,'data'=>'second'}
]
Now key Id '78597' is common in both array
Now i to want remove that element from array2 based on the key 'id'. The examples I referred where all single dimension.
You can build %seen hash lookup and filter #$array2,
my %seen;
#seen{ map $_->{id}, #$array1 } = ();
#$array2 = grep { !exists $seen{$_->{id}} } #$array2;

How do I create a Lookup table in Powershell? [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
I have a list of servernames
PARDC1 EURDC2 EURDC3 USADC1 USADC22 CHNDC1 CHNDC2
I have created a hashtable to pick the first 3 letters of servernames, and classify it in a region.
PAR = EMEA
EUR = EMEA
USA = NAM
CHN = APAC
I want to use the lookup table to classify all servers in appropriate regions.
How do I go about doing this in Powershell ?
You can create an empty hash table with
$hash = #{}
You can then add entries to it with
$hash['foo'] = $bar
or
$hash.foo = $bar
and access them the same way later again.

perl not clearing data on page exit [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 8 years ago.
Improve this question
sorry if the title doesn't make much sense, I'll explain.
I am learning Perl, which I have to use at work, so am running test programs. I am inputting data into a web form (name, age, location) and searching a MySQL database to find a match. When I search it returns any matching results printed on the screen. However, if I go back to the form and search again, knowing that the result will not be found, it just displays the last found record and I can't quite figure out why.
Can someone help me please?
Thanks in advance.
# Read the standard input (sent by the form):
read(STDIN, $FormData, $ENV{'CONTENT_LENGTH'});
# Get the name and value for each form input:
#pairs = split(/&/, $FormData);
# Then for each name/value pair....
$db = DBI->connect($conn->DataSource(), $conn->Username(), $conn->Password()) or die "Unable to connect: $DBI::errstr\n";
foreach $pair (#pairs)
{
# Separate the name and value:
($name, $value) = split(/=/, $pair);
#replace + with space as + means space when data is collected
$value =~ tr/+/ /;
$value =~ s/%([a-fA-F0-9][a-fA-F0-9])/pack("C", hex($1))/eg;
# Store values in a hash called %FORM:
$FORM{$name} = $value;
}
$query = $db->prepare("SELECT * FROM person WHERE Name = '".$FORM{'PName'}."' AND Age = '".$FORM{'Age'}."' AND Location = '".$FORM{'Location'}."'");
$query->execute();
# BIND TABLE COLUMNS TO VARIABLES
$query->bind_columns(undef, \$name, \$age, \$location);
# LOOP THROUGH RESULTS
$row = 0;
while($query->fetch())
{
push #Peeps, Person->New();
$Peeps[$row]->Name($name);
$Peeps[$row]->Age($age);
$Peeps[$row]->Location($location);
$row++;
}
$db->disconnect(); #disconnect from the db
print '<table>';
for($i = 0; $i <= $#Peeps; $i++)
{
print "<tr><td>$Peeps[$i]{'NAME'}</td><td>$Peeps[$i]{'AGE'}</td><td>$Peeps[$i]{'LOCATION'}</td></tr>";
}
print '</table>';
$db->disconnect(); #disconnect from the db
If you are using mod_perl or fastcgi where the server process is persistent over multiple requests, global variables (which you are using in your example) may not be reset between requests, but keep their old values.
In your example, this means that the next request may see the %FORM values set by the earlier request, if the request happens to come to the same server process.
If by adding %FORM = (); at the top of your script fixes the problem, then this is your issue. You can verify by saying e.g. print $test++; and see if it increments.
Note that this is not the real solution, I would recommend putting your real code inside a function or module and using local variables and parameters there, it is more maintainable as well. And using some of the web application frameworks available (minimally CGI.pm).
You also need to fix the SQL injection security problem, rule of thumb: Never put variables in SQL statement strings, but do something like:
$dbh->prepare("SELECT ... WHERE This = ? AND That = ?")
$dbh->execute($this, $that);
I'm afraid the question is too generic and for any useful answer, a bit of code/pseudo code is going to be essential. However, based entirely on what you've asked, it does look like a browser caching issue, or perhaps a session-data caching issue. Like I say, disclosing a bit of code / pseudo code will help understand your question better

Googling within a Perl script? [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 8 years ago.
Improve this question
I have a string in $var.
Using Perl, how can I pass this string to Google and get back an array of Google search results?
Before proceeding, please be aware of the Google Terms of Service.
You agree not to access (or attempt to access) any of the Services by any means other than through the interface that is provided by Google, unless you have been specifically allowed to do so in a separate agreement with Google. You specifically agree not to access (or attempt to access) any of the Services through any automated means (including use of scripts or web crawlers) and shall ensure that you comply with the instructions set out in any robots.txt file present on the Services.
That being said, there exists an official API to query web search programmatically.
The JSON/Atom Custom Search API lets you develop websites and programs to retrieve and display search results from your Google Custom Search programmatically. With this API, you can use RESTful requests to get search results in either JSON or Atom format.
You can use XML::Atom::Client or LWP+JSON::Any or many other libraries to perform the REST calls.
(You may still find references to the older Google Web Search API but it's deprecated and limited.)
Take a look at the Google Custom search API:
http://code.google.com/apis/customsearch/
If you need to search over a wider variety of hosts, you'll need to use the older, deprecated Websearch API, but that will limit the number of queries you can make per day.
Barring that, you'll need to do a lot of html scraping and parsing.
Here is how a simple script could look like (and yes it violates TOS so it's just PoC, and you shouldn't use it...)
use WWW::Mechanize;
use 5.10.0;
use strict;
use warnings;
my $mech = new WWW::Mechanize;
my $option = shift;
#you may customize your google search by editing this url (always end it with "q=" though)
my $google = 'http://www.google.co.uk/search?q=';
my #dork = ("this is my search one","this is my search two");
#declare necessary variables
my $max = 0;
my $link;
my $sc = scalar(#dork);
#start the main loop, one itineration for every google search
for my $i ( 0 .. $sc ) {
#loop until the maximum number of results chosen isn't reached
while ( $max <= $option ) {
#say $google . $dork[$i] . "&start=" . $max;
$mech->get( $google . $dork[$i] . "&start=" . $max );
#get all the google results
foreach $link ( $mech->links() ) {
my $google_url = $link->url;
if ( $google_url !~ /^\// && $google_url !~ /google/ ) {
say $google_url;
}
}
$max += 10;
}
}
By the way I wrote this a while back, so it's not exactly up to the par, but it does the job, and I am too lazy to boot linux to find the newer version of this...