[Webinar] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 377
  • Last Modified:

simple - php to perl

Hi experts,

Can you help me convert this XML.php file to a XML.pl file?

<?php
      header( "Content-Type: text/xml; charset=utf-8" );
      
      echo( "<?xml version='1.0' encoding='utf-8'?>" );
      echo( "<!-- COPYRIGHT -->" );
      echo( "<!-- CS version 1.8.11 -->" );
      echo( "<communicationsuite>" );

      echo( "<time>" . date("F d, Y h:i:s A") . "</time>" );

      $strDomainID = isset($_GET['domainID']) ? $_GET['domainID'] : null;
      $strFunction = isset($_GET['function']) ? $_GET['function'] : (isset($_GET['action']) ? $_GET['action'] : null);
      $strCallID = isset($_GET['callID']) ? $_GET['callID'] : null;
      
      if( $strFunction != null && $strDomainID != null )
      {
                      .. do stuff
                }

      echo( "</communicationsuite>" );
?>

0
kesea
Asked:
kesea
  • 4
  • 2
  • 2
  • +1
3 Solutions
 
mjcoyneCommented:
Here's some of it:

#!/usr/bin/perl -w

use POSIX qw(strftime);

print "Content-Type: text/xml; charset=utf-8\n";
print "<?xml version='1.0' encoding='utf-8'?>\n";
print "<!-- COPYRIGHT -->\n";
print "<!-- CS version 1.8.11 -->\n";
print "<communicationsuite>\n";
print "<time>" . strftime ("%B %d, %Y %H:%M:%S %p", localtime) . "</time>\n";


#     $strDomainID = isset($_GET['domainID']) ? $_GET['domainID'] : null;
#     $strFunction = isset($_GET['function']) ? $_GET['function'] : (isset($_GET['action']) ? $_GET['action'] : null);
#     $strCallID = isset($_GET['callID']) ? $_GET['callID'] : null;
   
if ($strFunction && $strDomainID) {
        .. do stuff
}

print "</communicationsuite>\n";

You'll have to tell me a bit more about what you're $_GET'ing -- info from a URL?
0
 
DireOrbAntCommented:
Asside from the fact that null vs '' might cause issues, you can probably use this:

#!/usr/bin/perl -w
use strict;
use CGI;

my $CGIObj = new CGI;
my @MoY = ('January', 'February', 'March', 'April', 'May', 'June', 'July', 'August', 'September', 'October', 'November', 'December');


     print "Content-Type: text/xml; charset=utf-8\n\n";
     
     print "<?xml version='1.0' encoding='utf-8'?>\n";
     print "<!-- COPYRIGHT -->\n";
     print "<!-- CS version 1.8.11 -->\n";
     print "<communicationsuite>\n";

     my ($Sec, $Min, $Hour, $Day, $Month, $Year) = localtime();
     print "<time>".sprintf("%s %02d, %d %02d:%02d:%02d %s", $MoY[$Month], $Day, $Year+1900, $Hour%12, $Min, $Sec, ($Hour < 12) ? 'AM' : 'PM')."</time>\n";

     my $strDomainID = $CGIObj->param('domainID') ? $CGIObj->param('domainID') : '';
     my $strFunction = $CGIObj->param('function') ? $CGIObj->param('function') : ($CGIObj->param('action') ? $CGIObj->param('action') : '');
     my $strCallID = $CGIObj->param('callID') ? $CGIObj->param('callID') : '';
     
     if( $strFunction && $strDomainID )
     {
       #.. do stuff
     }

     print "</communicationsuite>\n";

0
 
ahoffmannCommented:
#!/usr/bin/perl -w
use POSIX qw(strftime);
$\="\n";
print "Content-Type: text/xml; charset=utf-8\n";
print '<?xml version="1.0" encoding="utf-8"?>';
print '<!-- COPYRIGHT -->','<!-- CS version 1.8.11 -->','<communicationsuite>';
print '<time>' . strftime ("%B %d, %Y %H:%M:%S %p", localtime) . '</time>';
use CGI;
my $Q=new CGI;
my $strDomainID = $q->param('domainID') or undef;
my $strFunction = $q->param('function') or undef;
my $strCallID = $q->param('callID') or undef;
 if (defined($strFunction) && defined($strDomainID)) {
       # .. do stuff
}
print '</communicationsuite>';
0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
DireOrbAntCommented:
kesea,

mjcoyne's and ahoffmann's are missing an extra \n for the headers to work.
I like the strftime funstion, but the POSIX.pm module is not the best one to use...

To make ahoffmann's compile and work change:
my $Q=new CGI;
To:
my $q=new CGI;

And:
print "Content-Type: text/xml; charset=utf-8\n";
To
print "Content-Type: text/xml; charset=utf-8\n\n";

The function->action piece is not there either.

I also prefer ahoffmann's null to undef format. I guess I tried to be too literal in the conversion ;)

So, I combined his and mine, I also removed extra \n, I though echo in php was pushing \n. Here it is:
--------------------------------------------
#!/usr/bin/perl -w
use strict;
use CGI;

my $CGIObj = new CGI;
my @MoY = ('January', 'February', 'March', 'April', 'May', 'June', 'July', 'August', 'September', 'October', 'November', 'December');
my ($Sec, $Min, $Hour, $Day, $Month, $Year) = localtime();

print "Content-Type: text/xml; charset=utf-8\n\n";
     
print "<?xml version='1.0' encoding='utf-8'?>";
print "<!-- COPYRIGHT -->";
print "<!-- CS version 1.8.11 -->";
print "<communicationsuite>";

print "<time>".sprintf("%s %02d, %d %02d:%02d:%02d %s", $MoY[$Month], $Day, $Year+1900, $Hour%12, $Min, $Sec, ($Hour < 12) ? 'AM' : 'PM')."</time>";

my $strDomainID = $CGIObj->param('domainID') || undef;
my $strFunction = $CGIObj->param('function') || $CGIObj->param('action') || undef;
my $strCallID = $CGIObj->param('callID') || undef;
     
if( defined($strFunction) && defined($strDomainID) )
{
  #.. do stuff
}

print "</communicationsuite>";

--------------------------------------------

Feel free to use the POSIX method instead.
0
 
FishMongerCommented:
DireOrbAnt,

I'm curious, why do you think “the POSIX.pm module is not the best one to use... “?

It's cleaner, and with some benchmark tests, I'm willing to bet that we'll see that it's faster as well as being more versatile than the 3 step approach that you're advocating.
0
 
DireOrbAntCommented:
This link:
http://www.perl.com/lpt/a/2003/03/13/datetime.html
Says:
The POSIX module included with Perl provides interfaces to several common C library functions for datetimes, such as strftime(). I consider this the last refuge for the desperate, because the POSIX.pm module is a memory hog, and the C library interface is rather un-Perlish.

That was written by Dave Rolsky

http://perldoc.perl.org/POSIX.html
Says in the Note section:
The POSIX module is probably the most complex Perl module supplied with the standard distribution.

My main issue, is I'm not familiar with it and I saw those two notes. So I guess ignorance might be my main reason :)
Like I said in my previous post, I like the strftime function a lot, much simpler and cleaner, but I didn't feel like recommending it's use since I had no other clue than the two quotes above.

FishMonger, if you run benchmarking tests of the two options (loading the whole perl script each time to include module overhead), please share with us :)
0
 
DireOrbAntCommented:
Well, I guess I had some time to spare...

Perl1.pl is using my proposed script, Perl2.pl is using the POSIX module.

Timing tests (called from IIS 6):
Perl1.pl
Average Request Processing Time after 1000 runs: 78 msec (equals ~46125 requests/h

Perl2.pl
Average Request Processing Time after 1000 runs: 96 msec (equals ~37372 requests/h)

Perl1.pl is the script I proposed above.
Perl2.pl is this scriot (using POSIX):
---------------------------------------
#!/usr/bin/perl -w
use strict;
use CGI;
use POSIX qw(strftime);

my $CGIObj = new CGI;

print "Content-Type: text/xml; charset=utf-8\n\n";
     
print "<?xml version='1.0' encoding='utf-8'?>";
print "<!-- COPYRIGHT -->";
print "<!-- CS version 1.8.11 -->";
print "<communicationsuite>";

print "<time>".strftime ("%B %d, %Y %H:%M:%S %p", localtime)."</time>";

my $strDomainID = $CGIObj->param('domainID') || undef;
my $strFunction = $CGIObj->param('function') || $CGIObj->param('action') || undef;
my $strCallID = $CGIObj->param('callID') || undef;
     
if( defined($strFunction) && defined($strDomainID) )
{
  #.. do stuff
}

print "</communicationsuite>";
---------------------------------------

To be fair I ran this next:
---------------------------------------
#!/usr/bin/perl -w
use strict;
use POSIX qw(strftime);
use Benchmark;
my @MoY = ('January', 'February', 'March', 'April', 'May', 'June', 'July', 'August', 'September', 'October', 'November', 'December');

my $WithPOSIX = sub {
  return strftime ("%B %d, %Y %H:%M:%S %p", localtime);
};

my $WithoutPOSIX = sub {
  my ($Sec, $Min, $Hour, $Day, $Month, $Year) = localtime();
  return sprintf("%s %02d, %d %02d:%02d:%02d %s", $MoY[$Month], $Day, $Year+1900, $Hour%12, $Min, $Sec, ($Hour < 12) ? 'AM' : 'PM');
};

timethese(1000000, {'WithPOSIX' => $WithPOSIX, 'WithoutPOSIX' => $WithoutPOSIX});
---------------------------------------

And got those results:
Benchmark: timing 1000000 iterations of WithPOSIX, WithoutPOSIX...\
WithPOSIX: 3 wallclock secs ( 2.44 usr +  0.00 sys =  2.44 CPU) @ 410340.58/s
WithoutPOSIX: 3 wallclock secs ( 3.69 usr +  0.00 sys =  3.69 CPU) @ 271223.22/s

So there you go. It's a module with a good overhead, but the actual function is quite fast :)
0
 
ahoffmannCommented:
> .. ahoffmann's are missing an extra \n for the headers to work.
no, see my \n and $\="\n"

> my $Q=new CGI;
thanks for the correction

0
 
mjcoyneCommented:
He might have meant me -- in my (incomplete) code, I left the extra \n out....
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

  • 4
  • 2
  • 2
  • +1
Tackle projects and never again get stuck behind a technical roadblock.
Join Now