If
you have a question on how to do something in Perl, or
you need a Perl solution to an actual real-life problem, or
you're unsure why something you've tried just isn't working...
then this section is the place to ask. Post a new question!
However, you might consider asking in the chatterbox first (if you're a
registered user). The response time tends to be quicker, and if it turns
out that the problem/solutions are too much for the cb to handle, the
kind monks will be sure to direct you here.
I have a desire to create a script that would essentially present the user with an input form that they can fill out with values that would be used elsewhere in the process.
I could do so by prompting and getting input. Then prompt for the next value, etc. but this does not provide the user experience I hope to provide.
I have seen examples of using a web form. However, in this case, we are unable to install/use a web server. Is there a way to do data entry into a form on a terminal window?
A good answer to the question can be found at (http://search.cpan.org/~mgrabnar/File-Tail-0.99.3/Tail.pm).
The environment is ActiveState perl on a windows 7 box. I want to read records into my perl program from a file as they are being written to it by an external program. While windows does not have an internal tail command, MS does provide one as described at this site: ( http://www.windows-commandline.com/2010/08/tail-command-for-windows.html). Upon execution, this works fine except the output from tail goes to a command window and is not capture-able in perl by any means I can develop. I'm seeking an equivalent capability using perl's file IO functions.
Thanks;
#! /usr/bin/perl
my $cmdtorun = "tail -f D:/prjct/logger/2013-06-05.dlg |";
open(TL, $cmdtorun) || die "Failed: $!\n";
while ( <TL> )
{
print "Got: $_\n";
}
I need to talk to a memcached server in Perl program, and I need to uses SASL.
Now Perl has a whole bunch of memcached client libraries, but seems that none supports SASL.
The closest seems to be Memcached::libmemcached, which is a very thin wrapper around the C libmemcached library. In theory, all I need to do is to expose the memcached_set_sasl_auth_data function, call it once, and be happy. In practice, this segfaults (I've tried this in the sasl branch of this repository). It seems that the libmemcached library bundled with the perl module is very out of date, and upgrading to a new one is no easy task, because much of structure of the header files changed.
My next hope was that it could be relatively easy to pair up Authen::SASL and one of the existing memcached clients. However Memcached only supports SASL for the binary protocol, and all the other perl modules only support the text protocol (please correct me if I'm wrong here).
Finally there's a small pure-perl client in the tests of the memcached server, which seems to be my only remaining option. I'm reluctant to use it though, because it's not maintained as a "real" client, has no docs and no proper error handling.
So, my question is: Are there any options I overlooked? Or is somebody maybe even working on a client with sasl support? (Or is there another comparable open-source distributed memory cache with perl libraries that support authentication?).
Alright, I've spent way too long trying to figure out a way to make this work like I want. Perhaps the perlmonks can help me. :)
I basically want what array_each does only I want it to support a hash OR an array. If it gets a hash it should just do a standard hash to hash comparison, if it gets an array it should do what array_each normally does.
I at first tried to make my own sub called array_each_orHash that took a comparator value. Here's the code I wrote...
sub array_each_orHash {
my $comparator = shift;
my $handleArrayOrHash = sub {
my $data = shift;
return array_each($comparator) if (ref($data) eq "ARRAY");
return $comparator if (ref($data) eq "HASH");
};
return code( \&$handleArrayOrHash );
}
This was mysteriously always resulting in a passed test, until I realized the mystery. I wasn't supposed to return an object when using the "code( ... )" sub I was supposed to return 0 or 1, or an array with 0 and the reason it failed. Okay, so then I modified it a bit...
sub array_each_orHash {
my $comparator = shift;
my $handleArrayOrHash = sub {
my $data = shift;
my $result = (0, 'must pass an ARRAY or HASH');
$result = eq_deeply($data, array_each($comparator)) if (ref($data)
+eq "ARRAY");
$result = eq_deeply($data, $comparator) if (ref($data) eq "HASH");
return $result;
};
return code( \&$handleArrayOrHash );
}
This actually works except I get no useful debugging diagnostic info except that the test failed. And the diagnostic I do get tells me a CODE block was run. I don't really want that to be displayed. The "code" was really just a means to an end so I could try to overload array_each.
I thought about making my own cmp_deeply sub that actually just checks for array_each with a non-array and if found calls the real cmp_deeply without the array_each. If not found then it could call the real cmp_deeply with the array value and array_each. But the problem with this approach is in handling cases where the array_each is embedded deeply within a value's structure. Do I write my own recursive deep diving routine to search for array_each (Test::Deep::ArrayEach) class instances and look for what they are comparing against somehow? No way!
Ok, so what is the correct way to do this? It seems like simply making my own version of Test::Deep::ArrayEach is really the best solution here. Adding something like Test::Deep::ArrayEachOrHash. But I don't know how to make my own ArrayEach like this and haven't found useful information on how to do something like that. I did find the following code here: http://searchcode.com/codesearch/view/16731024 (Shown below)
But, how do I make my own "array_each_orHash" keyword and connected it to some code in package Test::Deep::ArrayEachOrHash? Am I grossly over-complicating things here or going the wrong direction with this? Any help would be appreciated. Thanks...
use strict;
use warnings;
package Test::Deep::ArrayEach;
use Test::Deep::Cmp;
sub init
{
my $self = shift;
my $val = shift;
$self->{val} = $val;
}
sub descend
{
my $self = shift;
my $got = shift;
my $exp = [ ($self->{val}) x @$got ];
return Test::Deep::descend($got, $exp);
}
1;
I created an executable from a perl script that works fine, but it requires that a few dll files are in the same directory. It's not a huge deal to just put everything into a zip archive, but I would prefer that everything was in one standalone executable.
This question, like a lot of my questions, if more looking for suggestions that actual code problems.
I have been thinking about creating a small, simple web services script and any place I look for information/tutorial seems to suggest it as being quite difficult. I have a really simple and crude script below and I am looking for thoughts on why this type of approach is bad! It is a simple process and the setup to return a JSON string.
Ideally I would create a backend DB with the auth details (for valid user API keys) and expand on the query/queries to return more attributes. My main concern is the method. Is this a really bad way to provide a web services service!? (Sitting on a standard Apache2 instance at the moment)
#!/usr/bin/perl
use strict;
use warnings;
use DBI;
use CGI;
use JSON::XS;
# Naively simple web service!?
my $cgi = new CGI;
my $auth_code = "XD9920399fldkkxzmfoJfkYYUS2";
my $lookup = $cgi->param("lookup");
my $client_key = $cgi->param("client_key");
my $print;
# Expand this later to return unique error for each possible omission
if ( (! defined $lookup) || (! defined $client_key) || ($client_key ne
+ $auth_code) ) {
$print = encode_json {error => 'You must provide a lookup and vali
+d client key'}
}
else{
my $dbh = new_dbh();
my $sth = $dbh->prepare("SELECT title,next_episode
FROM watcher
WHERE userid = ?");
$sth->execute($lookup);
$print = encode_json $sth->fetchrow_hashref();
}
print "Content-type: text/html\n\n";
# Send back the Json string
print "$print";
sub new_dbh {
my $db_driver = "mysql";
my $db_name = "shows";
my $db_host = "localhost";
my $db_port = "3306";
my $db_user = "root";
my $db_passwd = "xxxxxx";
my $dbh= DBI->connect("DBI:$db_driver:dbname=$db_name;host=$db_hos
+t;port=$db_port",
$db_user, $db_passwd,{RaiseError => 1,
mysql_enable_utf8 => 1,
});
$dbh->{TraceLevel} = 2; # disable when live
return $dbh;
}
I have a small issue that I have written a code where I have predefined my file , but now I want that the file should be entered by the user . So this is my code
my $file="Working_On.csv";
open(FILE,"<","$file");
my @lines=<FILE>;