Solved

simple - php to perl

Posted on 2006-06-29
9
331 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
Comment Utility
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
Comment Utility
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
Comment Utility
#!/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
 
LVL 26

Accepted Solution

by:
DireOrbAnt earned 200 total points
Comment Utility
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
Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

 
LVL 28

Expert Comment

by:FishMonger
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
> .. 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
Comment Utility
He might have meant me -- in my (incomplete) code, I left the extra \n out....
0

Featured Post

Enabling OSINT in Activity Based Intelligence

Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

Join & Write a Comment

Many time we need to work with multiple files all together. If its windows system then we can use some GUI based editor to accomplish our task. But what if you are on putty or have only CLI(Command Line Interface) as an option to  edit your files. I…
In the distant past (last year) I hacked together a little toy that would allow a couple of Manager types to query, preview, and extract data from a number of MongoDB instances, to their tool of choice: Excel (http://dilbert.com/strips/comic/2007-08…
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…
Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…

744 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

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now