perl script writing empty file, sometimes HELP PLEASE

I have a very annoying and troubling problem.

I have a perl script which calls a another cgi script from The script on just sends back a raw html stream which the perl script then parses, and writes out the results to a temp file on my server.

What is happening is about %80 of the time, the temp file is blank. I've tested the script on and it never timed out once or sent back blank results once. So i dont think the problem is with ups.

What i really dont understand is why it works sometimes, and not other times. I would think it would either work, or not work, not both.

I dont think its a permissions problem on my server (NT4, IIS4, Service pack 5, latest version of perl from activestate) since if the permissions were wrong, the script would either not run, or not be able to write a temp file. but the script is defintely running, and its defintely writing a temp file. Its just writing them blank sometimes.

This script was working before. It stopped working when i replaced my server with a faster machine. So i can only assume something, somewhere, on the new machine isnt set correctly. But i have no clue what that might be.

I have a bulletin board system on the same machine thats all perl based and it seems to be working fine.

I REALLY need some help on this one, sine its been a problem for the last 2 days with no progress at all.

if anyone wants to look at the perl script, please let me know and i'd be more then glad to send it. its about 10k
Who is Participating?
exactly!! i have been trying for the last 10 mins. i have stripped down version of your code (157 lines) that goes to their site and gets the info you want and does the processing.

here is the stripped version i am using.

here i have added $|++ and switched on the debug mode.

when i do that and run the script from the command prompt i get socket ok and bind ok.

Also i used a further stripped down version of this script (47 lines) to get yahoo's home page. That worked fine!!

so again i think your script is fine. All the same i will wait for the ups site to come up and try the script again.


# UPS Socket code
$upsAction = '4';
$upsProduct = '1DM';
$OriginPostalCode = '94555';
$DestZipCode = '94423';
$PackageWeight = '4.2';

$AF_INET = 2;
$sockaddr = 'S n a4 x8';
$port = 80;
$them = '';
$function = 'GET';              #Type of HTTP
$workFile = '/using/services/rave/qcostcgi.cgi';
$versionInfo = "HTTP/1.0\n\n";

$workString = '?';
$workString .= 'accept_UPS_license_agreement=yes';
$workString .= '&';
$workString .= "10_action=$upsAction";
$workString .= '&';
$workString .= "13_product=$upsProduct";
$workString .= '&';
$workString .= "15_origPostal=$OriginPostalCode";
$workString .= '&';
$workString .= "22_destCountry=$ShipCountry";
$workString .= '&';
$workString .= "19_destPostal=$DestZipCode";
$workString .= '&';
$workString .= "23_weight=$PackageWeight";
$request = "$function $workFile$workString $versionInfo";


($name, $aliases, $proto) = getprotobyname ('tcp');
($name, $aliases, $port) = getservbyname ($port, 'tcp') unless $port =~ /^\d+$/
($name, $aliases, $type, $len, $remoteaddr) = gethostbyname ($them);

$local = pack ($sockaddr, $AF_INET, 0, $localaddr);
$remote = pack ($sockaddr, $AF_INET, $port, $remoteaddr);

if (socket (S, $AF_INET, $SOCK_STREAM, $proto)) {
        print "socket ok\n" if $debug;
} else {
        die "socket: $!\n";

if (bind (S, $local)) {
        print "bind ok\n" if $debug;
} else {
        die "bind: $!\n";

if (connect (S, $remote)) {
        print "connect ok\n" if $debug;
} else {
        die "connect: $!\n";

select (S);
$| = 1;
select (STDOUT);
$| = 1;

print "$request\n" if $debug;
print S "$request\n";

$got_hdr = 0;
while (<S>) {
        print if $debug;
        push @result, $_ if $got_hdr;
        $got_hdr = 1 if /^\s*$/;        # get lines after header
close (S);
# done with socket code

# put the results in simple variables.
foreach $result (@result){
                # get the name/value strings

= split(/%/, $result);
                # list value keyed by numerical index
        if ($time[$j] eq "-1")  { $time[$j]="None"; }
        # add additional profit to ups charges
        if ($add_profit > 0) {$totalchrg[$j]=$totalchrg[$j]+$add_profit; }
        # Translate UPS Product Codes
        if ($ups[$j] eq "1DM")  { $ups[$j]="Next Day Air-AM"; }
        if ($ups[$j] eq "1DA")  { $ups[$j]="Next Day Air"; }
        if ($ups[$j] eq "1DP")  { $ups[$j]="Next Day Air Saver"; }
        if ($ups[$j] eq "2DM")  { $ups[$j]="2nd Day Air-AM"; }
        if ($ups[$j] eq "2DA")  { $ups[$j]="2nd Day Air"; }
        if ($ups[$j] eq "3DS")  { $ups[$j]="3 Day Select"; }
        if ($ups[$j] eq "GNDCOM")       { $ups[$j]="Ground Commercial"; }
        if ($ups[$j] eq "GNDRES")       { $ups[$j]="Ground Residential"; }

        # check for errors

$upsmax = $j;

# build the code snippet that will be exchanged
for ($j=0; $j<$upsmax; $j++){
$htmlcode .= "UPS-$zone[$j]";
$htmlcode .= ",";
$htmlcode .= "$ups[$j]";
$htmlcode .= ",";
$htmlcode .= "$totalchrg[$j]";
$htmlcode .= "\n";

# Write out UPS file
                # get a temporary filename
do {
     $tempfile = $pathstring . int(rand(99999999)) . ".ups";
   } until !(-e $tempfile);

open (OUTPUT, ">$tempfile") || die "Can't open $tempfile for reading ($!) - Che
ck with your ISP";
select (OUTPUT);
print (OUTPUT $htmlcode) || die "Can't write UPS file: $! - Incorrect permissio
ns on cgi-bin
folder \n";
close (OUTPUT);

# redirect to shipping/db connector
# first add the tempfile name to the redirect string

print "content-type: text/html\n\n";
print "<html>\n";
print "<head>\n";
print "<title>HELLO WORLD</title>\n";
print "</head>\n";
print "<body>\n";
print "<h2>UPS RESULTS</h2>\n";
print @result;
print "<h3>tester</h3>\n";
print "$htmlcode ";
print "</body>\n";
print "</html>\n";
can you pl send a copy of the script to

go it. currently looking at it.
Cloud Class® Course: MCSA MCSE Windows Server 2012

This course teaches how to install and configure Windows Server 2012 R2.  It is the first step on your path to becoming a Microsoft Certified Solutions Expert (MCSE).

merkAuthor Commented:
This might turn out to be a problem with ups after all. Their site is down right now so i have no way of testing this script at all. Perhaps they made some changes to the format of their response which caused the script not to capture the results.
merkAuthor Commented:
well the reason i am not %100 sure its a probablem with UPS is because yesterday their site was up. And this script was writing empty temp files, instead of writing the results from the UPS site. When i would hit the UPS url myself, it always worked.

So the only thought i have now, is maybe UPS looked ok to me, but maybe something wasnt entirely ok to the script. Maybe the layout or formatting or something minor changed, which caused the script to ignore the results that UPS sends.

Thats the only thing i can think of right now to explain why it would write empty temp files.

When UPS comes back up i'll try it again.
"So the only thought i have now, is maybe UPS looked ok to me, but maybe something wasnt entirely ok to the script. Maybe the layout or formatting or something minor changed, which caused the script to ignore the results that UPS sends."

i think the most likely reason could be the formatting, since you are expecting the results to be % delimited.

anyway as of now (18:48 EST) its still now responding. :-(
hope it has nothing to do with the "I love you" virus!!
Are you sure $SOCK_STREAM = 1; is correct?
You may want to
use Socket;
to import the proper definitions
merkAuthor Commented:
maneshr...if and when ups comes back up i will post the results of what i normally see when i put the url in the browser. just went over my head. no idea what you mean by using socket. I usually know enough about perl to maybe get a rought idea of what a small script is supposed to do. But thats about it.


sounds good!! meanwhile is still not respodning/slow.
i ran the trimmed down version of your script a few minutes back and it worked fine!!

here are the contents of the .ups file that the script created.

UPS-94423,The Next Day Air Early A.M. service may not guarantee Early AM arriva
l to   94423 United States.,18.25
UPS-102,Next Day Air,18.25
UPS-202,2nd Day Air,8.20
UPS-302,3 Day Select,5.70
UPS-002,Ground Commercial,3.42
UPS-002,Ground Residential,4.42

meanwhile, how is the script behaving at your end??

did it work??

let me know
merkAuthor Commented:
I havent had a chance to test your script out yet. but i had a thought from a problem someone else posted saying he was unable to make a socket call using perl.

I found a way around this problem. I was using the perlis.dll Instead i switched to using the perl.exe and mapped that to the script, and now its working fine.

Does that give you any clue? Why would it work with the exe but not the dll?

Also, the old server, which was using the dll, was perl 5.003. The new server is running perl 5.6
merkAuthor Commented:
Maneshr, i found out the problem was perl itself. There is a glitch within perl 5.6 when making socket connections. I had to install perl 5.22 and now it seems to be working fine.

Considering all the help you gave me i dont want to just delete the question. If you would email me at, i will post another empty question with the points set to 75 and give you credit for that. I hope that seems fair.
merkAuthor Commented:
This turned out to be a bug within perl itself. I had to install perl 5.22 to get it to work. I would like to give Maneshr though 75 points for the help he gave me.
merkAuthor Commented:
This question has a deletion request Pending
This question no longer is pending deletion
Glad to know that your problem was solved.

i would suggest that you post a question with the title "For maneshr".

i will just answer that Q and you can go ahead & accept the answer.

Community Support has reduced points from 150 to 75

I am reducing the points on this question as asked so merk can award it to the expert for 75 points.

Community Support @ Experts Exchange
merkAuthor Commented:
thanks for the help.
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.