Everything Else

php to perl

would someone be so kind to translate this php to perl code? the json bitchery is not needed.

Code: Select all

<?php
// process.php

$errors         = array();      // array to hold validation errors
$data           = array();      // array to pass back data

// validate the variables ======================================================
// if any of these variables don't exist, add an error to our $errors array

if (empty($_POST['name']))
$errors['name'] = 'Name is required.';

if (empty($_POST['email']))
$errors['email'] = 'Email is required.';

if (empty($_POST['superheroAlias']))
$errors['superheroAlias'] = 'Superhero alias is required.';

// return a response ===========================================================

// if there are any errors in our errors array, return a success boolean of false
if ( ! empty($errors)) {

// if there are items in our errors array, return those errors
$data['success'] = false;
$data['errors']  = $errors;
} else {

// if there are no errors process our form, then return a message

// DO ALL YOUR FORM PROCESSING HERE
// THIS CAN BE WHATEVER YOU WANT TO DO (LOGIN, SAVE, UPDATE, WHATEVER)

// show a message of success and provide a true success variable
$data['success'] = true;
$data['message'] = 'Success!';
}

// return all our data to an AJAX call
echo json_encode($data);



this is what i have tried, but this goes nowhere...

Code: Select all

#!/usr/bin/perl

use strict;
#use warnings;

use JSON;
use CGI;

my $cgi = CGI->new;

print $cgi->header('application/json;charset=UTF-8');

my @errors;
my @data;

my $lparnmame = $cgi->param('lparname');
my $from = $cgi->param('from');
my $to = $cgi->param('to');
my $email = $cgi->param('email');






if ( my $lparname eq "" ) { push(@errors, 'Lparname is required') };
if ( my $from eq "" ) { push(@errors, 'From is required') };
if ( my $to eq "" ) { push(@errors, 'To is required') };
if ( my $email eq "" ) { push(@errors, 'Email is required') };

if (!@errors) {
push(@data, @errors);
}
else {
push(@data, 'Success');
};

no plan
Well, for one thing, you're not printing anything. What are you planning to do with the @errors or @data array?
smit happens.

:Fuel: bigred , 900MHz R16K, 4GB RAM, V12 DCD, 6.5.30
:Indy: indy , 150MHz R4400SC, 256MB RAM, XL24, 6.5.10
:Indigo2IMP: purplehaze , R10000, Solid IMPACT
probably posted from Image bruce , Quad 2.5GHz PowerPC 970MP, 16GB RAM, Mac OS X 10.4.11
plus IBM POWER6 p520 * Apple Network Server 500 * HP C8000 * BeBox * Solbourne S3000 * Commodore 128 * many more...
Seems like you've mostly got it. I have suppressed most of my Perl, but this should perhaps give a hint.

Code: Select all

[...]

my %data;

if (@errors) {
$data{'success'}=0;
$data{'errors'}=\@errors;
} else {
$data{'success'}=1;
$data{'message'}='Success';
}

$json=JSON->new;
print $json->encode(\%data);



Edit: This is probably a terrible abuse of @errors, you should probably check with $#errors instead.
:Octane: halo , oct ane Image knightrider , d i g i t a l AlphaPC164, pond , soekris net6501, misc cool stuff in a rack
N.B.: I tend to talk out of my ass. Do not take it too seriously.
ClassicHasClass wrote: Well, for one thing, you're not printing anything. What are you planning to do with the @errors or @data array?


this should be a so called "ajax controller", a server side component of a jquery based webpage which processes / generates / mails nmonchart (aix) orders. so no printing of variables because this is done with javascript on the clientside.
no plan
Sure, but if that's end of your script, it just ends out in space. duck's solution should mostly work if you want JSON as the output.
smit happens.

:Fuel: bigred , 900MHz R16K, 4GB RAM, V12 DCD, 6.5.30
:Indy: indy , 150MHz R4400SC, 256MB RAM, XL24, 6.5.10
:Indigo2IMP: purplehaze , R10000, Solid IMPACT
probably posted from Image bruce , Quad 2.5GHz PowerPC 970MP, 16GB RAM, Mac OS X 10.4.11
plus IBM POWER6 p520 * Apple Network Server 500 * HP C8000 * BeBox * Solbourne S3000 * Commodore 128 * many more...
you're right, seems the json output part was lost during copy/paste. my fault.
no plan
ok modified the code to ducks suggestions and switched to hashes instead of arrays.

Code: Select all

#!/usr/bin/perl

use strict;
#use warnings;

use JSON;
use CGI;

my $cgi = CGI->new;

print $cgi->header('application/json;charset=UTF-8');

my %errors;
my %data;

my $lparnmame = $cgi->param('lparname');
my $from = $cgi->param('from');
my $to = $cgi->param('to');
my $email = $cgi->param('email');


if ( my $lparname eq "" ) { $errors{'lparname'}='Lparname is required'; }
if ( my $from eq "" ) { $errors{'from'}='From is required'; }
if ( my $to eq "" ) { $errors{'to'}='To is required'; }
if ( my $email eq "" ) { $errors{'email'}='Email is required'; }



if (%errors) {
$data{'success'}=0;
$data{'errors'}=\%errors;
} else {
$data{'success'}=1;
$data{'message'}='Success';
}

my $json=JSON->new;
print $json->encode(\%data);



this produces the following which is abolutely ok.

Code: Select all

root@nimmsag: /srv/www/nmonchart/ondemand # ./process.pl
Content-Type: application/json;charset=UTF-8

{"success":0,"errors":{"email":"Email is required","to":"To is required","from":"From is required","lparname":"Lparname is required"}}


the problem is it does not honor its input parameters.

when i add this...

Code: Select all


...

if (!%errors) {
$data{'success'}=0;
$data{'errors'}=\%errors;
} else {
$data{'success'}=1;
$data{'message'}='Success';
}

...


it outputs this...

Code: Select all


root@nimmsag: /srv/www/nmonchart/ondemand # ./process.pl
Content-Type: application/json;charset=UTF-8

{"success":1,"message":"Success"}



also ok, but this all should be dynamic according to input paramaters. any hints?
no plan
You're inverting the errors test, remove the !.
:Octane: halo , oct ane Image knightrider , d i g i t a l AlphaPC164, pond , soekris net6501, misc cool stuff in a rack
N.B.: I tend to talk out of my ass. Do not take it too seriously.
Better might be explicitly saying if(scalar(keys(%errors)))

That's kind of belt-and-suspenders, but it's guaranteed to Do What You Expect.
smit happens.

:Fuel: bigred , 900MHz R16K, 4GB RAM, V12 DCD, 6.5.30
:Indy: indy , 150MHz R4400SC, 256MB RAM, XL24, 6.5.10
:Indigo2IMP: purplehaze , R10000, Solid IMPACT
probably posted from Image bruce , Quad 2.5GHz PowerPC 970MP, 16GB RAM, Mac OS X 10.4.11
plus IBM POWER6 p520 * Apple Network Server 500 * HP C8000 * BeBox * Solbourne S3000 * Commodore 128 * many more...
ok fixed it, there was a typo in an variable name.

Code: Select all


my $lparnmame = $cgi->param('lparname'); <-----spot the redundant  "m" in lparname



thanks for helping me! :oops:
no plan

Code: Select all

if( my $lparname )


the "my" in that line is a bit weird, it declares a new variable in the scope. Unsure what scope it is in though... it's been a looong time since I did perl.
:Onyx2R: :IRIS3130: :Onyx2: :O2000: :O200: :PI: :Fuel: :Indigo: :Octane: :O2: :Indigo2IMP: :Indigo2: :Indy: :1600SW: :pdp8e:
:BA213: <- MicroVAX 3500 :BA213: <- DECsystem 5500 :BA215: <- MicroVAX 3300
Pictures of my collection: www.pdp8.se