Solved

simple - php to perl

Posted on 2006-06-29
9
349 Views
Last Modified: 2008-02-01
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
Comment
Question by:kesea
  • 4
  • 2
  • 2
  • +1
9 Comments
 
LVL 17

Assisted Solution

by:mjcoyne
mjcoyne earned 150 total points
ID: 17015578
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
 
LVL 26

Expert Comment

by:DireOrbAnt
ID: 17015612
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
 
LVL 51

Assisted Solution

by:ahoffmann
ahoffmann earned 150 total points
ID: 17015939
#!/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
Courses: Start Training Online With Pros, Today

Brush up on the basics or master the advanced techniques required to earn essential industry certifications, with Courses. Enroll in a course and start learning today. Training topics range from Android App Dev to the Xen Virtualization Platform.

 
LVL 26

Accepted Solution

by:
DireOrbAnt earned 200 total points
ID: 17019135
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
 
LVL 28

Expert Comment

by:FishMonger
ID: 17021865
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
 
LVL 26

Expert Comment

by:DireOrbAnt
ID: 17022294
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
 
LVL 26

Expert Comment

by:DireOrbAnt
ID: 17022359
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
 
LVL 51

Expert Comment

by:ahoffmann
ID: 17022874
> .. 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
 
LVL 17

Expert Comment

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

Featured Post

Courses: Start Training Online With Pros, Today

Brush up on the basics or master the advanced techniques required to earn essential industry certifications, with Courses. Enroll in a course and start learning today. Training topics range from Android App Dev to the Xen Virtualization Platform.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

I've just discovered very important differences between Windows an Unix formats in Perl,at least 5.xx.. MOST IMPORTANT: Use Unix file format while saving Your script. otherwise it will have ^M s or smth likely weird in the EOL, Then DO NOT use m…
On Microsoft Windows, if  when you click or type the name of a .pl file, you get an error "is not recognized as an internal or external command, operable program or batch file", then this means you do not have the .pl file extension associated with …
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …

776 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question