CGI/Perl Guide | Learning Center | Forums | Advertise | Login
Site Search: in

  Main Index MAIN
INDEX
Search Posts SEARCH
POSTS
Who's Online WHO'S
ONLINE
Log in LOG
IN

Home: Perl Programming Help: Beginner:
Error Message: Variable "@ADDNEWS" is not imported at ...

 



Larkdog
User

Jun 25, 2004, 7:35 PM

Post #1 of 24 (3953 views)
Error Message: Variable "@ADDNEWS" is not imported at ... Can't Post

Hello,

I have a small script below that was working until I made a slight change... The script opens and edits a file, formerly called 'home.htm' but now called 'home.php'...when i changed the file extension from .htm to .php, i must have done something else inadvertantely, b/c now it dont work...i even tried to put it back to .htm and it still wont work...could you help find my mistake??? this is the error message i get: Global Variable "@ADDNEWS" is not imported at ... then it gives the location of the file on my pc

here is the script:


Code
    

#!/usr/bin/perl

use warnings;

use strict;

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

$|++;

use CGI::Carp qw(fatalsToBrowser);

$addnews_file = '/home.htm';

if($ENV{'REQUEST_METHOD'} eq "POST"){

$data_length = $ENV{'CONTENT_LENGTH'};

$bytes_read = read(STDIN, $my_data, $data_length);

}

@name_value_array = split(/&/, $my_data);

foreach $name_value_pair (@name_value_array) {

($name, $value) = split(/=/, $name_value_pair);

$name =~ tr/+/ /;

$value =~ tr/+/ /;

$name =~ s/%(..)/pack("C",hex($1))/eg;

$value =~ s/%(..)/pack("C",hex($1))/eg;

if($form_data{$name})

{

$form_data{$name} .= "\t$value";

}

else

{

$form_data{$name} = $value;

}

}

open(ADDNEWS, "<$addnews_file") or

die "Can't open ADDNEWS: $addnews_file\n";

@ADDNEWS =<ADDNEWS>;

close(ADDNEWS);

open(ADDNEWS, ">$addnews_file") or

die "Can't open ADDNEWS: $addnews_file\n";

foreach $line (@ADDNEWS){

if($line =~ /<!--Add News:-->/i) {

print ADDNEWS "<!--Add News:-->\n";

print ADDNEWS "<tr><td>\n";

print ADDNEWS "$form_data{'news'}\n";

print ADDNEWS "</td></tr>\n";



} else {

print ADDNEWS "$line";



close(ADDNEWS);







print <<"EOF"

<HTML><TITLE>Entry Submitted</TITLE><HEAD> <META HTTP-EQUIV="refresh" content="4;URL=../home.htm"></HEAD><BODY>



<H1>Your News Entry has been Submitted\!</H1>

Your news entry has been successfully added to the

ICE homepage and in <font size=24>3</font> seconds you will be taken back to view it.

</BODY>

EOF

#===========================



thanks!
www.MrLarkins.com

(This post was
edited by Larkdog on Jun 26, 2004, 6:19 PM)


davorg
Thaumaturge / Moderator

Jun 27, 2004, 5:24 AM

Post #2 of 24 (3949 views)
Re: [Larkdog] Error Message: Variable "@ADDNEWS" is not imported at ... [In reply to] Can't Post

You are using "use strict" which means that you need to declare all of your variables.

--
Dave Cross, Perl Hacker, Trainer and Writer
http://www.dave.org.uk/
Get more help at Perl Monks


Larkdog
User

Jun 27, 2004, 10:37 AM

Post #3 of 24 (3946 views)
Re: [davorg] Error Message: Variable "@ADDNEWS" is not imported at ... [In reply to] Can't Post

thanks, someone told me it would be better perl writing to have that in there,

so i added it (since i am a beginner)...i am still getting an error message when

running it from the command line, but it scrolls by too fast to read, and I can't

hit the 'pause' button fast enough...

i also want to require a password to this...like have the user enter the password

in the web form, have the script test it, execute the script if true, or display html

page is false, i think is causing my error but i cant read it... its too fast

here is the script updated:


Code
    

#!/usr/bin/perl

use warnings;

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

$|++;

use CGI::Carp qw(fatalsToBrowser);

$addnews_file = '../home.php';

if($ENV{'REQUEST_METHOD'} eq "POST"){

$data_length = $ENV{'CONTENT_LENGTH'};

$bytes_read = read(STDIN, $my_data, $data_length);

}

@name_value_array = split(/&/, $my_data);

foreach $name_value_pair (@name_value_array) {

($name, $value) = split(/=/, $name_value_pair);

$name =~ tr/+/ /;

$value =~ tr/+/ /;

$name =~ s/%(..)/pack("C",hex($1))/eg;

$value =~ s/%(..)/pack("C",hex($1))/eg;

if($form_data{$name})

{

$form_data{$name} .= "\t$value";

}

else

{

$form_data{$name} = $value;

}

}

if($form_data{$password} == 31415926518)

{

open(ADDNEWS, "<$addnews_file") or

die "Can't open ADDNEWS: $addnews_file\n";

@ADDNEWS =<ADDNEWS>;

close(ADDNEWS);

open(ADDNEWS, ">$addnews_file") or

die "Can't open ADDNEWS: $addnews_file\n";

foreach $line (@ADDNEWS){

if($line =~ /<!--Add News:-->/i) {

print ADDNEWS "<!--Add News:-->\n";

print ADDNEWS "<tr><td>\n";

print ADDNEWS "$form_data{'news'}\n";

print ADDNEWS "</td></tr>\n";



} else {

print ADDNEWS "$line";



close(ADDNEWS);







print <<"EOF"

<HTML><TITLE>Entry Submitted</TITLE><HEAD> <META HTTP-EQUIV="refresh" content="4;URL=../home.php"></HEAD><BODY>



<H1>Your News Entry has been Submitted\!</H1>

Your news entry has been successfully added to the

ICE homepage and in <font size=24>3</font> seconds you will be taken back to view it.

</BODY>

EOF

}

else{

print <<"EOF"

<HTML><TITLE>Wrong Password</TITLE><HEAD> <META HTTP-EQUIV="refresh" content="10;URL=../home.php"></HEAD><BODY>



<H1>Wrong Password</H1>

I'm sorry, but you have entered an incorrect password. If you have forgotten your password, send a request for reminder to <a href="mailto:mrlarkins@mrlarkins.com?subject=forgot password">Webmaster</a></BODY>

EOF}

}

#===========================

www.MrLarkins.com

(This post was
edited by Larkdog on Jun 27, 2004, 11:15 AM)


davorg
Thaumaturge / Moderator

Jun 27, 2004, 11:09 AM

Post #4 of 24 (3943 views)
Re: [Larkdog] Error Message: Variable "@ADDNEWS" is not imported at ... [In reply to] Can't Post

Looks like you misunderstood me. I didn't mean to imply that you should remove the "use strict". You should always have "use strict" and "use warnings" in your Perl programs.

You should fix the problems by declaring all of your variables.

--
Dave Cross, Perl Hacker, Trainer and Writer
http://www.dave.org.uk/
Get more help at Perl Monks


Larkdog
User

Jun 27, 2004, 11:13 AM

Post #5 of 24 (3941 views)
Re: [davorg] Error Message: Variable "@ADDNEWS" is not imported at ... [In reply to] Can't Post

oh, ok i'll put it back, then maybe i dont understand what i should do...what do you mean 'declare' my variables?? didn't i do that already??
www.MrLarkins.com


davorg
Thaumaturge / Moderator

Jun 27, 2004, 11:25 AM

Post #6 of 24 (3938 views)
Re: [Larkdog] Error Message: Variable "@ADDNEWS" is not imported at ... [In reply to] Can't Post

You use either "my" or "our" to declare variables. I don't see either of those in in your program. In your case I suspect that "my" would be the correct one to choose.

Also you are using an over-complex and error prone method to extract the CGI parameters that are passed to your program. You should look at using the "param" function from CGI.pm instead.

What are you using to learn Perl? You seem to be picking up some rather bad habits.

--
Dave Cross, Perl Hacker, Trainer and Writer
http://www.dave.org.uk/
Get more help at Perl Monks


Larkdog
User

Jun 27, 2004, 11:42 AM

Post #7 of 24 (3936 views)
Re: [davorg] Error Message: Variable "@ADDNEWS" is not imported at ... [In reply to] Can't Post

LOL, an old testbook and perlguru.com !!

ok i added the following directly after the use warnings and use strict part

my $addnews_file;

my $password;

my $data_length;

my $ENV;

my $bytes_read;

my $my_data;

my $name_value_pair;

my $name;

my $value;

my $form_data;

my $line;

is that too much??

could you also tell me how to get the screen not to close after the script attempts (so i can read the error message?)

www.MrLarkins.com


davorg
Thaumaturge / Moderator

Jun 27, 2004, 1:03 PM

Post #8 of 24 (3933 views)
Re: [Larkdog] Error Message: Variable "@ADDNEWS" is not imported at ... [In reply to] Can't Post

I'd write your program like this:


Code
#!/usr/bin/perl 

use strict;
use warnings;

use CGI ':cgi';
use CGI::Carp qw(fatalsToBrowser);

print header;

my $addnews_file = '../home.php';

if (param('password') != 31415926518) {
print <<'EOF';
<HTML>
<HEAD>
<TITLE>Wrong Password</TITLE>
<META HTTP-EQUIV="refresh" content="10;URL=../home.php">
</HEAD>
<BODY>
<H1>Wrong Password</H1>
<P>I'm sorry, but you have entered an incorrect password. If you have
forgotten your password, send a request for reminder to
<a href="mailto:mrlarkins@mrlarkins.com?subject=forgot password">Webmaster</a></P>
</BODY>
</HTML>
EOF

exit;
}

open(ADDNEWS, $addnews_file)
or die "Can't open ADDNEWS: $addnews_file - $!\n";

my @ADDNEWS = <ADDNEWS>;

close(ADDNEWS);

open(ADDNEWS, ">$addnews_file")
or die "Can't open ADDNEWS: $addnews_file - $!\n";

foreach (@ADDNEWS) {
if (/<!--Add News:-->/i) {

print ADDNEWS "<!--Add News:-->\n";
print ADDNEWS "<tr><td>\n";
print ADDNEWS param('news'), "\n";
print ADDNEWS "</td></tr>\n";
} else {
print ADDNEWS $_;
}
}
close(ADDNEWS);

print <<"EOF"

<HTML>
<HEAD>
<TITLE>Entry Submitted</TITLE>
<META HTTP-EQUIV="refresh" content="4;URL=../home.php">
</HEAD>
<BODY>
<H1>Your News Entry has been Submitted!</H1>

<P>Your news entry has been successfully added to the
ICE homepage and in <font size="24">3</font> seconds you will be
taken back to view it.</p>
</BODY>
</HTML>
EOF


--
Dave Cross, Perl Hacker, Trainer and Writer
http://www.dave.org.uk/
Get more help at Perl Monks


Larkdog
User

Jun 27, 2004, 1:22 PM

Post #9 of 24 (3932 views)
Re: [davorg] Error Message: Variable "@ADDNEWS" is not imported at ... [In reply to] Can't Post

that looks good, but it is cuasing anyother error, but i am unable to read it because it flies by so fast!! is there someway to slow it down so i can read it?? i am running the script from the command line
www.MrLarkins.com


davorg
Thaumaturge / Moderator

Jun 27, 2004, 1:30 PM

Post #10 of 24 (3931 views)
Re: [Larkdog] Error Message: Variable "@ADDNEWS" is not imported at ... [In reply to] Can't Post

Have you tried redirecting stderr to a file?

--
Dave Cross, Perl Hacker, Trainer and Writer
http://www.dave.org.uk/
Get more help at Perl Monks


Larkdog
User

Jun 27, 2004, 1:34 PM

Post #11 of 24 (3929 views)
Re: [davorg] Error Message: Variable "@ADDNEWS" is not imported at ... [In reply to] Can't Post

dont know how to
www.MrLarkins.com


davorg
Thaumaturge / Moderator

Jun 27, 2004, 1:48 PM

Post #12 of 24 (3928 views)
Re: [Larkdog] Error Message: Variable "@ADDNEWS" is not imported at ... [In reply to] Can't Post

Which shell are you using?

--
Dave Cross, Perl Hacker, Trainer and Writer
http://www.dave.org.uk/
Get more help at Perl Monks


Larkdog
User

Jun 27, 2004, 1:52 PM

Post #13 of 24 (3927 views)
Re: [davorg] Error Message: Variable "@ADDNEWS" is not imported at ... [In reply to] Can't Post

hmmm, thats seems like such a simple question! ummm, windows XP??? dont laugh, i am seriously trying here! i have Perl 5.8 !! does that answer your question??

in my windows xp, i usually go to start > run > c:\addnews.pl and click ok
www.MrLarkins.com


davorg
Thaumaturge / Moderator

Jun 27, 2004, 2:04 PM

Post #14 of 24 (3926 views)
Re: [Larkdog] Error Message: Variable "@ADDNEWS" is not imported at ... [In reply to] Can't Post

Oh. OK. I assumed you've be on some kind of Unix system. I know nothing about Windows systems.

Can't you open up a DOS window and run your command from there?

--
Dave Cross, Perl Hacker, Trainer and Writer
http://www.dave.org.uk/
Get more help at Perl Monks


Larkdog
User

Jun 27, 2004, 2:11 PM

Post #15 of 24 (3925 views)
Re: [davorg] Error Message: Variable "@ADDNEWS" is not imported at ... [In reply to] Can't Post

and thats why your the genious!!

ok this is the error when i ran you script (scrapping my own b/c lets face it, you ARE smarter than I):

syntax error line 30, near "exit"
www.MrLarkins.com

(This post was
edited by Larkdog on Jun 27, 2004, 3:08 PM)


Larkdog
User

Jun 27, 2004, 6:43 PM

Post #16 of 24 (3920 views)
Re: [davorg] Error Message: Variable "@ADDNEWS" is not imported at ... [In reply to] Can't Post

just another thought...

does the script you wrote take the data from the html form??

i noticed you wrote it without all "REQUEST_METHOD" crap!

this is worrying me. you replaced it with 'param' which i've

never seen before
www.MrLarkins.com


davorg
Thaumaturge / Moderator

Jun 28, 2004, 1:26 AM

Post #17 of 24 (3918 views)
Re: [Larkdog] Error Message: Variable "@ADDNEWS" is not imported at ... [In reply to] Can't Post


In Reply To
ok this is the error when i ran you script

syntax error line 30, near "exit"


It ran ok here.

But cutting and pasting the code from the board seems to give an extra space on the end of the delimiters that mark the end of the here-docs. In particular line 71 has an extra space after the string "EOF". If you remove that then things seem to work. You might also need to check that the last line in the file (which ends another here-doc) has no spaces at the end and ends in a newline (so there is at least one empty line after it).

Lines that end here-docs need to contain _only_ the delimiter text and they also need to end in a newline.

--
Dave Cross, Perl Hacker, Trainer and Writer
http://www.dave.org.uk/
Get more help at Perl Monks


davorg
Thaumaturge / Moderator

Jun 28, 2004, 1:30 AM

Post #18 of 24 (3917 views)
Re: [Larkdog] Error Message: Variable "@ADDNEWS" is not imported at ... [In reply to] Can't Post


In Reply To
does the script you wrote take the data from the html form??

i noticed you wrote it without all "REQUEST_METHOD" crap!

this is worrying me. you replaced it with 'param' which i've never seen before


I mentioned before that you should be using CGI.pm and the "param" function for doing that. Now you see why. It does all of the same work, but a) your code looks simpler and b) it is less buggy.

--
Dave Cross, Perl Hacker, Trainer and Writer
http://www.dave.org.uk/
Get more help at Perl Monks


Larkdog
User

Jun 28, 2004, 7:12 AM

Post #19 of 24 (3912 views)
Re: [davorg] Error Message: Variable "@ADDNEWS" is not imported at ... [In reply to] Can't Post

ok, it 'kinda' runs fine fromt the c prompt, but it does cause a 500 error in the web browser... from the cprompt i get this:


Code
 addnews.pl: Use of uninitialized value in numeric ne (!=) at line 13



but then it also shows the html code for the 'Wrong Password' part...but when i uploaded it to the server and ran it through the browser, it gave a 500 error "premature end of script headers" (blah blah blah the usual)

any ideas why?
www.MrLarkins.com

(This post was
edited by Larkdog on Jun 28, 2004, 8:11 AM)


davorg
Thaumaturge / Moderator

Jun 28, 2004, 8:41 AM

Post #20 of 24 (3908 views)
Re: [Larkdog] Error Message: Variable "@ADDNEWS" is not imported at ... [In reply to] Can't Post

I'm afraid this is where I stop trying to help you. The error message is pretty clear. There is a problem with an uninitialised value on line 13. There is only one value on the line that can possibly be undefined.

I'm guessing that you aren't setting the "password" parameter when running the test from the command line.

As for the 500 error when running it as a CGI program - you will always get the real error from the web server error log.

I enjoy helping people out with Perl problems. But when it comes down to reading the documentation for you, I get a bit bored I'm afraid :)

--
Dave Cross, Perl Hacker, Trainer and Writer
http://www.dave.org.uk/
Get more help at Perl Monks


Larkdog
User

Jun 28, 2004, 9:12 AM

Post #21 of 24 (3906 views)
Re: [davorg] Error Message: Variable "@ADDNEWS" is not imported at ... [In reply to] Can't Post

hmmm, thanks for the help you have given, i realize what the deal was

and at the c prompt it works great by typing "addnews.pl password=*** news=***

it works for good passwords and bad passwords,

but please note,

the server logs only say "premature end of script headers"...

now you may know what that means, but i do not

i do know that i am running Perl 5.8 at home and my server runs 5.6.1,

does this older version maybe not have CGI.pm??
www.MrLarkins.com

(This post was
edited by Larkdog on Jun 28, 2004, 9:34 AM)


Larkdog
User

Jun 28, 2004, 12:54 PM

Post #22 of 24 (3896 views)
Re: [Larkdog] Error Message: Variable "@ADDNEWS" is not imported at ... [In reply to] Can't Post

i uploaded the files to a second server i deal with and it works great, so the problem must be with the server, so i will question them...davorg thanks for letting me pic your brain...last request, could i link my website www.mrlarkins.com to yours? i really like some of the stuff you have on it
www.MrLarkins.com


davorg
Thaumaturge / Moderator

Jun 29, 2004, 4:21 AM

Post #23 of 24 (3892 views)
Re: [Larkdog] Error Message: Variable "@ADDNEWS" is not imported at ... [In reply to] Can't Post


In Reply To
last request, could i link my website www.mrlarkins.com to yours? i really like some of the stuff you have on it


Feel free to link to anything :)

--
Dave Cross, Perl Hacker, Trainer and Writer
http://www.dave.org.uk/
Get more help at Perl Monks


Larkdog
User

Jun 29, 2004, 5:32 AM

Post #24 of 24 (3890 views)
Re: [davorg] Error Message: Variable "@ADDNEWS" is not imported at ... [In reply to] Can't Post

ok, thanks for that, i like to always ask to link to others...

and as a closier, i opened a service ticket with the my host company (ipshosting) and inquired why the script wouldn't work, and the only reply from them was "Try it now" LOL and whatever they did, its working perfectly, thanks for the help, i did some good learning!
www.MrLarkins.com

 
 


Search for (options) Powered by Gossamer Forum v.1.2.0

Web Applications & Managed Hosting Powered by Gossamer Threads
Visit our Mailing List Archives