1

So my problem is that i have a perl5.8 installation and i can't install any additional module. (I am a public servant and i have to use the servers as they are without any right on it or choosing what i can install on it, the process to modify something take years).

So there is a little web server script :

use HTTP::Daemon;
use HTTP::Status;

(my $d = new HTTP::Daemon 
LocalAddr => '127.0.0.1',
LocalPort => 52443
) || die;
print "Please contact me at: <URL:", $d->url, ">\n";
while (my $c = $d->accept) {
    while (my $r = $c->get_request) {
        if ($r->method eq 'GET' and $r->uri->path eq "/xyzzy") {
            # remember, this is *not* recommended practice :-)
            $c->send_file_response("D:/Script/index.html");
        }
        else {
            $c->send_error(RC_FORBIDDEN)
        }
    }
    $c->close;
    undef($c);
}

And i would like to return a json like : {"Status" : "Ok" }

regards

bussirs
  • 21
  • 3
  • 4
    Perl 5.8 was released in 2002 and has been out of support for a long time. Strongly consider upgrading to a more up to date version. – melpomene Sep 20 '19 at 07:33
  • 1
    What do you mean by "additional module"? What modules are already available? – melpomene Sep 20 '19 at 07:34
  • *i can't install any additional module* - That is rarely as true as people seem to think it is. It's unlikely that you wouldn't be able use [JSON::PP](https://metacpan.org/release/JSON-PP) as it's pure Perl (that's what 'PP' means). – Dave Cross Sep 20 '19 at 09:08
  • If you see the [documenation for a recent version of HTTP::Daemon](https://metacpan.org/pod/HTTP::Daemon) you'll see that they've started using the better constructor syntax `HTTP::Daemon->new()`. Please follow their example to avoid potential pain in your future. – Dave Cross Sep 20 '19 at 09:10
  • You can also install any CPAN module to a [local::lib](https://metacpan.org/pod/local::lib) in your home directory if it's an issue of permissions. [Get the cpanm script](https://metacpan.org/pod/App::cpanminus#Downloading-the-standalone-executable) and then you can `cpanm -l local Any::Module` and run your script with `perl -Ilocal script.pl`. – Grinnz Sep 20 '19 at 14:33
  • 1
    Re "*i can't install any additional module*", If you can install code from SO, you can install code from CPAN. – ikegami Sep 21 '19 at 02:52

1 Answers1

1

Rewriting the example in the documentation to return JSON would look something like this:

#!/usr/bin/perl

use strict;
use warnings;

use HTTP::Daemon;
use HTTP::Status;
use HTTP::Response;
use HTTP::Headers;
use JSON::PP;

my $headers = HTTP::Headers->new;
$headers->header(Content_Type => 'application/json');
my $content = JSON::PP->new->utf8->encode({ Status => 'Ok' });

my $d = HTTP::Daemon->new || die;
print "Please contact me at: <URL:", $d->url, ">\n";
while (my $c = $d->accept) {
    while (my $r = $c->get_request) {
        if ($r->method eq 'GET' and $r->uri->path eq "/xyzzy") {
            $c->send_response(
                HTTP::Response->new(200, 'OK', $headers, $content)
            );
        }
        else {
            $c->send_error(RC_FORBIDDEN)
        }
    }
    $c->close;
    undef($c);
}

But please note that writing a web application at this level is rarely a useful thing to do. You really want to install a web framework (I like Dancer2) as that will make your life far easier.

I'm not sure what is imposing these restrictions on you. But if you're not using a modern version of Perl (5.10 at the very least) and installing modules from CPAN, then you're making your Perl development career far harder than it needs to be.

Dave Cross
  • 68,119
  • 3
  • 51
  • 97
  • Thanks but i can t install any module on the servers. I'am a public servant and the process is ****. I have to use what are on the computers ... Now we are thinking about switching from perl 5.8 to python 2.6 (in 2019). So i have to use the tools that are given ... So no perl json module – bussirs Sep 20 '19 at 11:24
  • @bussirs You can just copy/paste the module code into your script. Or use App::FatPacker, which automates the process. – melpomene Sep 20 '19 at 11:37
  • @bussirs: As I like to say in situations like this - "I suggest you upgrade your Perl, your manager or your job as appropriate" :-) – Dave Cross Sep 20 '19 at 11:58
  • @DaveCross :) yes but my administration manage lifes so it's kind of up to people of good will to make these lifes and interactions with us less *** even if the tech here is a mess ... But your answer helped me so thanks :) – bussirs Sep 20 '19 at 12:14
  • 1
    @bussirs JSON::Tiny, a fork of Mojo::JSON is on CPAN, and is designed such that you could quite literally copy 300 lines and paste them into your code base. It's not a sustainable best practice, but totally possible, and not much different from copying lines of code out of StackOverflow. – DavidO Sep 20 '19 at 15:41
  • However, you do need to at least be on Perl 5.8.4; older versions of Exporter wouldn't work with J::T as-is. And 5.10 is strongly suggested, since it's possible for JSON with entities greater than 64kb to cause some hickups in old versions of Perl's regex engine. – DavidO Sep 20 '19 at 16:14
  • 2
    @bussirs Python 2 in 2019? It's all but [end-of-life](https://www.anaconda.com/end-of-life-eol-for-python-2-7-is-coming-are-you-ready/). It seems as though whomever you work for goes out of their way to make things difficult for themselves, with no future situational awareness whatsoever. – stevieb Sep 20 '19 at 16:51
  • 1
    You forgot to encode the content. `my $content = JSON::PP->new->encode({ Status => 'Ok' });` should be `my $content = JSON::PP->new->utf8->encode({ Status => 'Ok' });` or `my $content = encode_json({ Status => 'Ok' });` – ikegami Sep 21 '19 at 02:51