Web Hosting Forum | Lunarpages

Author Topic: Trouble with passing arguements in perl  (Read 3153 times)

Offline 1hunglo

  • Space Explorer
  • ***
  • Posts: 6
Trouble with passing arguements in perl
« on: May 04, 2013, 07:55:07 PM »
No matter what I do I can't seem to get my perl script to accept any arguments. I'm trying to send arguments from a Delphi app I'm working on but the following perl script on my domain never seems to receive any:

****************************Begin Perl Script************************************
#!/usr/bin/perl

print "Content-type: text/plain\n\n";

open (MYFILE, '>>Data.txt');
$ip_ad = $ENV{'REMOTE_ADDR'};
print MYFILE "$ip_ad\n";
print "Number of arguments is: $#ARGV\n";
print "Your public IP is: $ip_ad\n";
close (MYFILE);
*******************************End Perl Script******************************************
This always returns:
Number of arguments is: -1
Your public IP is: 123.456.789.123

I sniffed the outbound traffic to make sure the Get was properly formatted and it looks fine:

*****************************Begin Sniff*********************************
GET /cgi-bin/R.pl A0000001 2ndVariable HTTP/1.0
Accept: image/gif, image/x-xbitmap, image/jpeg, image/pjpeg, */*
User-Agent: Mozilla/4.0 (compatible; ICS)
Host: mydomain.com

HTTP/1.1 200 OK
Date: Sun, 05 May 2013 03:38:07 GMT
Server: Apache/2.2.24 (Unix) mod_ssl/2.2.24 OpenSSL/0.9.8e-fips-rhel5 mod_auth_passthrough/2.1 mod_bwlimited/1.4 FrontPage/5.0.2.2635 mod_fcgid/2.3.6
Connection: close
Content-Type: text/plain

Number of arguments is: -1
Your public IP is: 123.456.789.123
*************************************End Sniff**************************************

1st paragraph is the outbound and the rest is the return. I'm sending 2 arguments: "A0000001" and "2ndVariable". I checked the blanks between them to make sure they were truly spaces (h20) and they were.

It's such a simple script, what am I missing?

Offline MrPhil

  • Senior Moderator
  • Berserker Poster
  • *****
  • Posts: 6187
Re: Trouble with passing arguements in perl
« Reply #1 on: May 05, 2013, 06:04:08 AM »
It seems to work in Windows command line. I copied your code into x.pl and ran it: perl ./x.pl ab cd . It returned number of arguments = 1 ($#ARGV is the maximum index, starting at 0, so this is correct for two arguments). Without any arguments, it returns -1. Is this what you get? So, it sounds like you're not passing it any arguments.

How exactly are you invoking this script? I think the normal way of passing arguments through cgi-bin is to use ?var1=val&var2=val (a URL Query String). See http://www.perlmonks.org/?node_id=24065. I don't think a regular command line (values separated by spaces) works in the server environment.
Visit My Site

E-mail Me
  
-= From the ashes shall rise a sooty tern =-

Offline 1hunglo

  • Space Explorer
  • ***
  • Posts: 6
Re: Trouble with passing arguements in perl
« Reply #2 on: May 05, 2013, 11:56:51 AM »
Thank you, MrPhil!

I now realize the error in my understanding of how this works. I too had tested it on my local machine and it had worked like a champ. This led to my mistaken belief that that was the same formatting for queries that was needed on the server. After following the link that you provided and a little trial-and-error I realized that the ARGV array is only available when executed locally and that url requests needed to be in the CGI query format, as you had indicated. A little rewrite on my script and everything is working great!

Thanks! :yey:

 

Share |