Gossamer Forum
Home : General : Perl Programming :

Illegal variable name.

Quote Reply
Illegal variable name.
I've got the following script that keeps returning the error "Illegal variable name.". The trouble is, I don't see any variables which might cause any conflicts with reserved names. Can anyone help? Thanks!
Code:
#/usr/local/bin/perl -w

#___________________________________________________________________________
#
# index.cgi
#
# Revision : 16:06 12/11/01
#
# Author : Wiliam Stephens
# E-Mail : wilstephens@hotmail.com
#
#___________________________________________________________________________


$| = 1;

use CGI;
$query = new CGI;

print $query->header;

@listhtml = (index.apricot.html,
index.butter.html,
index.garden.html,
index.greek.html,
index.maple.html,
index.raspberry.html,
index.strawberry.html,
index.natural.html
);

$htmlrand = $listhtml[int rand @listhtml];

open (FILE, "</home/fba/beta.rachelsorganic.co.uk/$htmlrand");
@html = <FILE>;
close FILE;

print "@html";

exit;

- wil
Quote Reply
Re: [Wil] Illegal variable name. In reply to
Try:

@listhtml = qw(
index.apricot.html,
index.butter.html,
index.garden.html,
index.greek.html,
index.maple.html,
index.raspberry.html,
index.strawberry.html,
index.natural.html
);

Last edited by:

RedRum: Nov 12, 2001, 8:49 AM
Quote Reply
Re: [RedRum] Illegal variable name. In reply to
No that doesn't work. The only thing qw does is to define the array as a Word List.

Thanks anyway.

Wil

- wil
Quote Reply
Re: [Wil] Illegal variable name. In reply to
I have it working on my pc.

http://213.106.6.135/cgi-bin/du.cgi


Last edited by:

RedRum: Nov 12, 2001, 9:01 AM
Quote Reply
Re: [RedRum] Illegal variable name. In reply to
Still doesn't work on mine -> http://beta.rachelsorganic.co.uk/index.cgi

Could be something to do with that paticular server running Perl version 5.005_03?

Wil

- wil

Last edited by:

Wil: Nov 12, 2001, 8:59 AM
Quote Reply
Re: [Wil] Illegal variable name. In reply to
Hmm I guess. I'm using the latest version.

Last edited by:

RedRum: Nov 12, 2001, 9:01 AM
Quote Reply
Re: [Wil] Illegal variable name. In reply to
Wil,

Did you check the syntax of the file? perl -c index.cgi

Did you check your Perl error logs?

I would recommend adding the call to FatalsToBrowser so that you can see the error message.
========================================
Buh Bye!

Cheers,
Me
Quote Reply
Re: [Chewbaca] Illegal variable name. In reply to
Im not try to cause an argument but Fatals must have a lowercase f
Quote Reply
Re: [Chewbaca] Illegal variable name. In reply to
Hi Eliot

Thanks for the reply. However I've tried all of these already.

perl -c index.cgi outputs index.cgi syntax OK

Apache error log outputs Illegal variable name.

I'm using use stricts;, use diagnostics; and the -w flag running.

I've changed all variable names a few times to see if that would make any difference, which it doesn't.

?

Wil

- wil
Quote Reply
Re: [Wil] Illegal variable name. In reply to
After illegal variable name does it not give a line number?

Run it from telnet/ssh
Quote Reply
Re: [RedRum] Illegal variable name. In reply to
I am, and no line number is given.

- wil
Quote Reply
Re: [Wil] Illegal variable name. In reply to
Changing:
Code:
@listhtml = ( index.apricot.html,
index.butter.html,
index.garden.html,
index.greek.html,
... );
to:
Code:
@listhtml = ( 'index.apricot.html',
'index.butter.html',
'index.garden.html',
'index.greek.html',
... );
works for me. Have you tried that yet?
Quote Reply
Re: [Wil] Illegal variable name. In reply to
I guess you'll have to upgrade the perl binary or re-write the code then as it seems to be an issue with the version rather than the code itself. You should try enclosing the values with ' '

I did notice that you have:

print "@html"; exit;

You don't need to enclose perl code with " ". Lots of people seem to do:

print "$bla"; and print "@foo"; ....perldoc does mention not to do that.

You should do print @html; and print $var; etc.

I don't see why you have an exit in there either.

Last edited by:

RedRum: Nov 12, 2001, 9:36 AM
Quote Reply
Re: [CP] Illegal variable name. In reply to
Tried that CP. I've also tried quotation marks ("").

Tried removing the quotation marks from my print statement, Paul. That didn't want to know either.

Hmm. I've always put exit; at the end of my scripts since I started authoring Perl. What is the advantage/disadvantages?

Wil

- wil
Quote Reply
Re: [Wil] Illegal variable name. In reply to
Quote:
Tried removing the quotation marks from my print statement, Paul. That didn't want to know either.

That was just a general comment.

Using exit dooesn't matter it just isn't needed.

Last edited by:

RedRum: Nov 12, 2001, 9:50 AM
Quote Reply
Re: [Wil] Illegal variable name. In reply to
Try using strict and my() to see if you can solve the problem.
Quote Reply
Re: [RedRum] Illegal variable name. In reply to
Hmm. I always thought it was needed.

Or maybe I'm mistaken. I've always put it in everywhere. I actualy used to use exit1;

- wil
Quote Reply
Re: [RedRum] Illegal variable name. In reply to
Still no luck.

I am compeltly stumped by this.

If you were to write a similar block of code yourself? You'd go about it the way I have done, right?

I can't see any major flaws in the code? Expect for the obvious security checks, file open check etc. which I don't really need.

- wil
Quote Reply
Re: [Wil] Illegal variable name. In reply to
No the script should exit itself after all code is executed. You only need to add an exit if you need to stop the execution for some reason like in error subs.
Quote Reply
Re: [Wil] Illegal variable name. In reply to
It works for CP and myself (with a little alteration) so it must be a perl version issue.

Last edited by:

RedRum: Nov 12, 2001, 9:56 AM
Quote Reply
Re: [RedRum] Illegal variable name. In reply to
Now I've got this code. Does this exact code work for you guys?

Code:
$| = 1;

use CGI;
$query = CGI::new();

print $query->header();

@allhtml = ('index.apricot.html',
'index.butter.html',
'index.garden.html',
'index.greek.html',
'index.maple.html',
'index.raspberry.html',
'index.strawberry.html',
'index.natural.html'
);

$randhtml = $allhtml[int rand @allhtml];

open (HTMLDAT, "</home/fba/beta.rachelsorganic.co.uk/$randhtml");
@html = <HTMLDAT>;
close HTMLDAT;

print @html;

- wil
Quote Reply
Re: [Wil] Illegal variable name. In reply to
Try this...I just wrote it quickly....

Code:
#!/usr/bin/perl

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

my %list = ( 0 => 'index.apricot.html',
1 => 'index.butter.html',
2 => 'index.garden.html',
3 => 'index.greek.html',
4 => 'index.maple.html',
5 => 'index.raspberry.html',
6 => 'index.strawberry.html',
7 => 'index.natural.html'
);

my $htmlrand = $list{int(rand(7))};

print $htmlrand;

Last edited by:

RedRum: Nov 12, 2001, 10:08 AM
Quote Reply
Re: [Wil] Illegal variable name. In reply to
When printing the file data use (it is quicker):

open (H, "</home/fba/beta.rachelsorganic.co.uk/$htmlrand");
read (H, my $html, -s H);
close H;

print $html;

Last edited by:

RedRum: Nov 12, 2001, 10:12 AM
Quote Reply
Re: [RedRum] Illegal variable name. In reply to
Hashes are slower than Arrays?

And I want the number of elements in my hash/array worked out by the script, because I want to make it as easy as possible for a non-programmer to add elements to it.

Cheers

Wil

- wil
Quote Reply
Re: [Wil] Illegal variable name. In reply to
>>Hashes are slower than Arrays?<<

....not when we are talking about 7 keys vs 7 elements

>>And I want the number of elements in my hash/array worked out by the script, because I want to make it as easy as possible for a non-programmer to add elements to it.
<<

eh?

my $total = scalar keys %list;

Last edited by:

RedRum: Nov 12, 2001, 10:27 AM
Quote Reply
Re: [RedRum] Illegal variable name. In reply to
Ah. Shucks. Still no answer. I reckon it's my local copy of perl. Time for an upgrade I think.

- wil
Quote Reply
Re: [Wil] Illegal variable name. In reply to
Because you haven't enclosed the file names with " " or ' ' perl gives the illegal variable error as it thinks that the file name is supposed to be a variable.

You can't create an array like that - only perl variables can be ($var, $bla), everything else needs to be enclosed by ' ' or " " (or use qw)

So that is the cause. Upgrading perl is a good idea though.

Last edited by:

RedRum: Nov 12, 2001, 1:48 PM
Quote Reply
Re: [RedRum] Illegal variable name. In reply to
even quicker (maybe):
Code:
while (<FV>) { print }

--Philip
Links 2.0 moderator
Quote Reply
Re: [PerlFreak] Illegal variable name. In reply to
I _think_ read is faster.

Where did FV come from? ....have you been copying code from comp.lang.misc.perl Cool

Last edited by:

RedRum: Nov 12, 2001, 3:19 PM
Quote Reply
Re: [RedRum] Illegal variable name. In reply to
Paul, I've tried:

Code:
@allhtml = qw(index.apricot.html
index.butter.html
index.garden.html
index.greek.html
index.maple.html
index.raspberry.html
index.strawberry.html
index.natural.html
);

and

Code:
@allhtml = ('index.apricot.html',
'index.butter.html',
...
);

Still no luck.

Can you run the following *exact* code on your system:

Code:
$| = 1;

use CGI;
$query = CGI::new();

print $query->header();

@allhtml = qw(index.apricot.html
index.butter.html
index.garden.html
index.greek.html
index.maple.html
index.raspberry.html
index.strawberry.html
index.natural.html
);

my $randhtml = $allhtml[int rand @allhtml];

open (HTMLDAT, "</home/fba/beta.rachelsorganic.co.uk/$randhtml");
@html = <HTMLDAT>;
close HTMLDAT;

print @html;

??

Cheers

Wil

- wil
Quote Reply
Re: [Wil] Illegal variable name. In reply to
I have done already and it works.
Quote Reply
Re: [RedRum] Illegal variable name. In reply to
That *exact* code? Hmph.

- wil
Quote Reply
Re: [Wil] Illegal variable name. In reply to
That code is longer and slower than it needs to be. All you need is:

Code:

#!/usr/bin/perl

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

@html = ('index.apricot.html',
'index.butter.html',
'index.garden.html',
'index.greek.html',
'index.maple.html',
'index.raspberry.html',
'index.strawberry.html',
'index.natural.html');


my $rand = $html[rand @html];

open (H, "</home/fba/beta.rachelsorganic.co.uk/$rand");
while (<H>) { print; }
close H;

That will work with any version of perl. If it doesn't you screwed up your perl installation.

Also you don't need the int in [int rand @html] seeing as you aren't dealing with an integer Tongue

Last edited by:

RedRum: Nov 13, 2001, 5:35 AM
Quote Reply
Re: [RedRum] Illegal variable name. In reply to
You don't need int but it's good practice to include it.

I'll try your code.

The perl installation is fine, btw.

- wil
Quote Reply
Re: [RedRum] Illegal variable name. In reply to
Although, I don't see any difference with your code except for the file handling? Why would that work with "any version of perl" and mine not?

- wil
Quote Reply
Re: [Wil] Illegal variable name. In reply to
>>You don't need int but it's good practice to include it.
<<

Really? Not when you are using words and int() is for numbers.

>>Why would that work with "any version of perl" and mine not?
<<

Because my version is correctly formatted

Did you try it?


Last edited by:

RedRum: Nov 13, 2001, 5:50 AM
Quote Reply
Re: [RedRum] Illegal variable name. In reply to
Yep, doesn't work on two different machines. The machines are running:

perl -v 5.005_03
perl -v 5.6.0

Cheers

Wil

- wil
Quote Reply
Re: [Wil] Illegal variable name. In reply to
Hey Wil,

I tried your code and it worked fine for me. The only thing I changed was the path to the files. perl -v: This is perl, v5.6.1 built for MSWin32-x86-multi-thread. Don't know if that really helps or not :/

Regards,
Charlie
Quote Reply
Re: [Wil] Illegal variable name. In reply to
>>Yep, doesn't work on two different machines. The machines are running:

perl -v 5.005_03
perl -v 5.6.0
<<

You must be doing something wrong as it works for me with 5.6.0, 5.6.1 and 5.005

Last edited by:

RedRum: Nov 13, 2001, 6:17 AM
Quote Reply
Re: [RedRum] Illegal variable name. In reply to
Ah well. Thanks for all your help guys!

- wil
I'm going to come back to this one. Leave it for a few hours I think. Might fix itself :-)

Last edited by:

Wil: Nov 13, 2001, 6:30 AM
Quote Reply
Re: Illegal variable name. In reply to
Hahaha!!! I found the problem.... Look at the first line of my original code. Look at the shebang. Missing a !? :-)

I wrote:

#/usr/local/bin/perl

instead of

#!/usr/local/bin/perl

Took me absolute ages to figure that out! In end it becamse obvious that it wasn't a Perl problem but the system couldn't figure out what on earth I was tyring to do :-\

ROFL! Grrrrrrrrr......

Cheers

Wil

- wil
Quote Reply
Re: [Wil] Illegal variable name. In reply to
How annoying. It is always the most obvious things that take the longest to find.
Quote Reply
Re: [RedRum] Illegal variable name. In reply to
Yep. Thanks for your help, though. Much appreciated.

- wil
Quote Reply
Re: [RedRum] Illegal variable name. In reply to
huh? it was a type... I meant to type <FH>. I haven't visited comp.lang.perl in months.

--Philip
Links 2.0 moderator
Quote Reply
Re: [PerlFreak] Illegal variable name. In reply to
Chill Drew, I was pullin ya chain.
Quote Reply
Re: [Wil] Illegal variable name. In reply to
Wil, the only error wasn't just the missing ! so you must have changed what CP and myself suggested too about the missing ' ' or " "

The file names would have caused errors the way you had them

As this shows:

Not enough arguments for index at e:\apache\cgi-bin\du.cgi line 12, near "index."
Not enough arguments for index at e:\apache\cgi-bin\du.cgi line 13, near "index."
Not enough arguments for index at e:\apache\cgi-bin\du.cgi line 14, near "index."
Not enough arguments for index at e:\apache\cgi-bin\du.cgi line 15, near "index."
Not enough arguments for index at e:\apache\cgi-bin\du.cgi line 16, near "index."
Not enough arguments for index at e:\apache\cgi-bin\du.cgi line 17, near "index."
Not enough arguments for index at e:\apache\cgi-bin\du.cgi line 18, near "index."
Not enough arguments for index at e:\apache\cgi-bin\du.cgi line 19, near "index."

Last edited by:

RedRum: Nov 13, 2001, 11:53 AM
Quote Reply
Re: [RedRum] Illegal variable name. In reply to
Yep, I chaned that to include '' straight away after posting. I don't like "" (quotation marks) and I don't like using qw when I want to astheticaly lay out the array on the page. It makes me nervous about Perl not reading them correctly.

In response to an earlier post. You are right about hashes being faster han arrays. The difference is neglibale to the size of my array, nut in a different situation it would make a difference.

The funny thing is I downloaded ActivePerl for Win32 so I had to change the shebang line to #c:/ or whatever, and that's where I noticed the error.

Where would I be without Windows? <g>.

- wil
Quote Reply
Re: [Wil] Illegal variable name. In reply to
Yes, large arrays are much faster than large hashes
Quote Reply
Re: [RedRum] Illegal variable name. In reply to
You mean hashes are faster than arrays.

Look it up in your Camel book.

Wil

- wil