Compatible Support Forums: More PERL help

Jump to content

Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

More PERL help

#1 User is offline   Busby 

  • member
  • Group: Members
  • Posts: 143
  • Joined: 18-April 00

Posted 03 July 2001 - 09:22 PM

I need some more help w/ PERL. This time I don't know exactly what to do (I'm a newbie in PERL). What I want to do is search through a file and if the content of a variable is present then redirect to a page saying yuo've already registered, and then if it doesn't exist it writes info to the file and redirects to a different page.
0

#2 User is offline   Philipp 

  • Pooh-Bah
  • Group: Administrators
  • Posts: 1964
  • Joined: 15-July 99

Posted 03 July 2001 - 10:32 PM

Just wrote a script:
Code:
$NAME = "Username"; # Example Variable

open(FILE, "member.dat"); # File
while&#40;<FILE>&#41; &#123;
$THIS_LINE=$_;
chomp&#40;$THIS_LINE&#41;;
if &#40;$THIS_LINE eq $NAME&#41; &#123; 
print "You are already registered"; 
exit;
&#125;
&#125;
close&#40;FILE&#41;;
open&#40;FILE, ">>member.dat"&#41;; # Write file if user is not on the list
print FILE "$NAME\n";
close&#40;FILE&#41;;
print "$NAME added to the list";
exit;
 

I hope that help you smile
0

#3 User is offline   CUViper 

  • old hand
  • Group: Members
  • Posts: 1120
  • Joined: 23-January 00

Posted 04 July 2001 - 04:22 AM

Philipp's script looks good to me, but I see two small things to fix if you use this. For one, that exit; in the middle of the script leaves the file open - you should insert a close(FILE); right before it. Also, whenever opening files, it is good practice to verify that the file was opened properly. To do this, just add a die statement, i.e. open(FILE, "member.dat") or die "Error - Couldn't open member.dat: $!";

I'm sure you know these things Philipp - I just thought that as a PERL newbie he should be aware of them...;)
0

#4 User is offline   Busby 

  • member
  • Group: Members
  • Posts: 143
  • Joined: 18-April 00

Posted 05 July 2001 - 05:04 PM

I used a little different method but it still works

Code:
open&#40;FILE,"../www/roster_info/roster.txt"&#41;;
$exists++ if grep&#40;/$name/i,&#40;<FILE>&#41;&#41;;
close&#40;FILE&#41;;

if &#40;$exists&#41; &#123;

print "Content-type&#58; text/html\n\n"; 
print <<"EOF";

<meta http-equiv="refresh" content="0;url=http&#58;//www.reservoirdogs.net/roster_info/already.shtml">

EOF

&#125; else &#123;


After the else I have the original contents of the script so it checks first, then it decides whether or not to actually register you (this is for a LAN party registration).

*EDIT*
Got the other part I needed done.
0

Share this topic:


Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

1 User(s) are reading this topic
0 members, 1 guests, 0 anonymous users