Re: Showing errors with user input
Adam Jimerson wrote: On Dec 7, 12:43 pm, g...@lazymountain.com (Greg Jetter) wrote: On Sunday 06 December 2009 10:24:31 am Adam Jimerson wrote: I am working on a registration page and there for want it to show the user errors it has found with their input. I have two subroutines in my code, the first one prints out the form, also takes an array with error descriptions that is passed by the other subroutine. The other subroutine takes the user input and verifies it, any errors that it finds it pushes into an array called @errors and passes that back to the first subroutine. The problem is it doesn't work right when I run it from the command line this is what I get: vend...@seserver:~/public_html/AmeriVista perl -cT register.cgi [Sun Dec 6 14:12:12 2009] register.cgi: Illegal character in prototype for main::form_verify : @user at register.cgi line 43. [Sun Dec 6 14:12:12 2009] register.cgi: Scalar found where operator expected at register.cgi line 93, near $user [Sun Dec 6 14:12:12 2009] register.cgi:(Missing semicolon on previous line?) [Sun Dec 6 14:12:12 2009] register.cgi: main::form_verify() called too early to check prototype at register.cgi line 36. Content-type: text/html h1Software error:/h1 presyntax error at register.cgi line 93, near quot;$userquot; Global symbol quot;$GoodMailquot; requires explicit package name at register.cgi line 93. register.cgi had compilation errors. /pre p For help, please send mail to this site's webmaster, giving this error message and the time and date of the error. /p [Sun Dec 6 14:12:12 2009] register.cgi: syntax error at register.cgi line 93, near $user [Sun Dec 6 14:12:12 2009] register.cgi: Global symbol $GoodMail requires explicit package name at register.cgi line 93. [Sun Dec 6 14:12:12 2009] register.cgi: register.cgi had compilation errors. I have attached my code for the script, if someone could look at it and give some ideas as to how to make this work or a better way then please do You are trying to use a local scoped var as a global , line 93 $GoodMail is used out of its scope , if ( $user[5] =~ /^([...@\w.]+)$/ ) { $user[5] = $1; eval { my $GoodMail = Email::Valid-address( -address = $user[5], -mxcheck = 1); return; } #push @errors, pError: Double check your email address/p if $@; $user[5] = $GoodMail; } it should read if ( $user[5] =~ /^([...@\w.]+)$/ ) { my $GoodMail ; $user[5] = $1; eval { $GoodMail = Email::Valid-address( -address = $user[5], -mxcheck = 1); return; } #push @errors, pError: Double check your email address/p if $@; $user[5] = $GoodMail; } or even declare it up with the other globals if you want , but the way you have it now it is out of scope after that eval { } block completes. there may be other errors , fix that one first and try it again and see what else pops up. Ok well I have corrected a couple more errors with the script and it now has no errors during compile and runs until it goes to report problems it has found back to the user: #!/usr/bin/perl -T use warnings; use strict; use diagnostics; use CGI qw(:standard); use DBI; use Email::Valid; BEGIN { $|=1; use CGI::Carp('fatalsToBrowser'); } delete @ENV { 'IFS', 'CDPATH', 'ENV', 'BASH_ENV'}; my @user; #Here @user deals with: first name, last name, username, password, CLSCC Email, and Student Number my @errors; my $dbh; sub db_connect { use constant username = 'secret'; use constant password = 'secret'; my $database = 'database name'; my $server = 'localhost'; my $dsn = DBI:mysql:database=$database;host=$server;port=3306 || die Couldn't Connect to the Database: $!; The string you are assigning to $dsn is *always* true so the die() is superfluous, it will *never* execute. my $dbh = DBI-connect($dsn, username, password, {RaiseError = 1}) || die couldn't authenticate to the Database: $!; } db_connect (); print header; print start_html (-title=AmeriVista Event Logging, -author='vend...@vendion.net'); print h1Registration Form/h1\n; print hr\n; if (param) { form_verify (@user); } else { print start_form; print_form (); print end_form, \n; } sub form_verify { $user[0] = param('FirstName'); if ( $user[0] =~ /^([-\w.]+)$/ ) { $user[0] = $1; } else { push @errors, pFirst Names should only contain letters/p\n; } $user[1] = param('LastName'); if ( $user[1] =~ /^([...@\w.]+)$/ ) { $user[1] = $1; } else { push @errors, pLast Name Should Only Contain Letters/p\n; } $user[2] = param('Username');
Re: Data Conversion ?
Nathan Gibbs wrote: How would I get the length of a string as returned by length () into a 4 byte unsigned integer in network byte order my $length = pack 'N', length $string; John -- The programmer is fighting against the two most destructive forces in the universe: entropy and human stupidity. -- Damian Conway -- To unsubscribe, e-mail: beginners-cgi-unsubscr...@perl.org For additional commands, e-mail: beginners-cgi-h...@perl.org http://learn.perl.org/
Re: Creating a Logon Form
John W. Krahn wrote: PekinSOFT wrote: John W. Krahn wrote: pekins...@gmail.com wrote: I enter the string 'hiyall2008' in the password field and get the following values in my logon script... Click 1: hiyall2008153639492 Click 2: hiyall2008135813700 Click 3: hiyall2008152312388 et cetera... As you can see, there is a different arbitrary string of numbers at the end of the clear text of the password entered. If it was the same each time the password was entered, I would just make it a part of the password and encrypt the whole thing into my database. However, each time it is different. It appears to be only 9 numbers each time, so I decided to try and strip those 9 numbers off the password with the 'substr()' method. So, I created the following sub procedure to do that: sub strip_string { my $ret = ; for (my $i = 0; $i length($_[0]) - 9; $i++) { $ret .= substr(length($_[0]) - $i, 1); Say that you pass the string hiyall2008153639492 to strip_string and the length of that string is 19 characters. At the start of the loop $i is 0 and length($_[0]) - $i is 19 so your expression says: $ret .= substr(19, 1); Or: $ret .= 9; At the next iteration through the loop $i is 1 so you have: $ret .= substr(18, 1); #print $ret; } return $ret; Since the length of hiyall2008153639492 is 19 and the loop starts at 0 and ends at 9 then the length of $ret will be 10. John, I understand what you are saying here, but in my script, I'm not looping $_[0] - $i times. I set my loop up to loop $_[0] - 9 times, No. You set your loop up to loop length($_[0]) - 9 times. Since hiyall2008153639492 has a numeric value of 0 the loop would never end if you did that. Correction, the loop will never start. John -- Those people who think they know everything are a great annoyance to those of us who do.-- Isaac Asimov -- To unsubscribe, e-mail: beginners-cgi-unsubscr...@perl.org For additional commands, e-mail: beginners-cgi-h...@perl.org http://learn.perl.org/
Re: Creating a Logon Form
PekinSOFT wrote: John W. Krahn wrote: pekins...@gmail.com wrote: I enter the string 'hiyall2008' in the password field and get the following values in my logon script... Click 1: hiyall2008153639492 Click 2: hiyall2008135813700 Click 3: hiyall2008152312388 et cetera... As you can see, there is a different arbitrary string of numbers at the end of the clear text of the password entered. If it was the same each time the password was entered, I would just make it a part of the password and encrypt the whole thing into my database. However, each time it is different. It appears to be only 9 numbers each time, so I decided to try and strip those 9 numbers off the password with the 'substr()' method. So, I created the following sub procedure to do that: sub strip_string { my $ret = ; for (my $i = 0; $i length($_[0]) - 9; $i++) { $ret .= substr(length($_[0]) - $i, 1); Say that you pass the string hiyall2008153639492 to strip_string and the length of that string is 19 characters. At the start of the loop $i is 0 and length($_[0]) - $i is 19 so your expression says: $ret .= substr(19, 1); Or: $ret .= 9; At the next iteration through the loop $i is 1 so you have: $ret .= substr(18, 1); #print $ret; } return $ret; Since the length of hiyall2008153639492 is 19 and the loop starts at 0 and ends at 9 then the length of $ret will be 10. John, I understand what you are saying here, but in my script, I'm not looping $_[0] - $i times. I set my loop up to loop $_[0] - 9 times, No. You set your loop up to loop length($_[0]) - 9 times. Since hiyall2008153639492 has a numeric value of 0 the loop would never end if you did that. so what you are describing shouldn't be happening. The logic that I was attempting with this loop was to add each character, one at a time, from the provided string to the $ret variable, stopping 9 characters shy of the end of the string. However substr(length($_[0]) - $i, 1) does not use the provided string, it uses the string created by length($_[0]) - $i, which is the numeric length of the provided string minus the value of $i converted to a string. However, with it set up the way it is, for some reason I'm either getting the 9 characters that I didn't want or a whole other bunch of 9 characters that I have no clue from whence they came. I just explained, again, where they came from. John -- Those people who think they know everything are a great annoyance to those of us who do.-- Isaac Asimov -- To unsubscribe, e-mail: beginners-cgi-unsubscr...@perl.org For additional commands, e-mail: beginners-cgi-h...@perl.org http://learn.perl.org/
Re: Creating a Logon Form
[EMAIL PROTECTED] wrote: Hey All, Hello, I'm new to doing CGI with Perl and so am a little lost here. I'm working on a web-accessible database system for a (rather large) group of area churches and went through the rigmarole of assessing various programming and scripting languages to see which is the best tool for the job and I landed on Perl::CGI. I started working on this project and have created scripts that generate a registration page that emails the registration information to me for processing. This is intentional, by the way, as I don't want it to be a self-register site for certain security reasons. These scripts work fine, so I started working on a logon form to allow users who are already registered to logon. So, on my main page, I have a right-hand pane that looks similar to this (in the HTML code): div id='rightcontent' pa href='http://myserver.domain.org/cgi-bin/ boms.cgi'Register/a/p br / h3Logon/h3 form method='POST' action='http://myserver.domain.org/cgi-bin/ logon.cgi' Username:br / input type='textfield' name='uname' /br / Password:br / input type='password' name='pwd' /br / input type='submit' name='logon' value='Logon' / /form /div ...etc... This form displays pretty well, though I need to work on the width of the fields, but that's not my issue. My issue is when I fill in the data in the fields and submit it to my logon.cgi script, the password value gets an arbitrary string of numbers attached to the end and I am not having any luck figuring out where those numbers come from, nor how to get rid of them back to the clear text of the password. For example: I enter the string 'hiyall2008' in the password field and get the following values in my logon script... Click 1: hiyall2008153639492 Click 2: hiyall2008135813700 Click 3: hiyall2008152312388 et cetera... As you can see, there is a different arbitrary string of numbers at the end of the clear text of the password entered. If it was the same each time the password was entered, I would just make it a part of the password and encrypt the whole thing into my database. However, each time it is different. It appears to be only 9 numbers each time, so I decided to try and strip those 9 numbers off the password with the 'substr()' method. So, I created the following sub procedure to do that: sub strip_string { my $ret = ; for (my $i = 0; $i length($_[0]) - 9; $i++) { $ret .= substr(length($_[0]) - $i, 1); Say that you pass the string hiyall2008153639492 to strip_string and the length of that string is 19 characters. At the start of the loop $i is 0 and length($_[0]) - $i is 19 so your expression says: $ret .= substr(19, 1); Or: $ret .= 9; At the next iteration through the loop $i is 1 so you have: $ret .= substr(18, 1); #print $ret; } return $ret; Since the length of hiyall2008153639492 is 19 and the loop starts at 0 and ends at 9 then the length of $ret will be 10. } Now, when I use this method to strip the arbitrary numbers from the end of the entered password, I get the following: I enter the same password as before, hiyall2008, and get the following: Click 1: 0134588996 Click 2: 0157203012 Click 3: 0138639940 Now, not only do I have arbitrary strings of numbers, I have 10 numbers instead of 9! I know that it is something that I'm not doing correctly, but I cannot figure out what I'm doing wrong. If you just want to strip 9 characters from the end of your string then: $ perl -le' $_ = hiyall2008153639492; print; substr( $_, -9 ) = ; print; ' hiyall2008153639492 hiyall2008 John -- Those people who think they know everything are a great annoyance to those of us who do.-- Isaac Asimov -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] http://learn.perl.org/
Re: awk command in script?
Owen wrote: You need to run something like this. Adapt to your requirements #!/usr/bin/perl -w use strict; while (DATA) { my $line = $_; if ( $line =~ /QQQ/ ) { my @bits = split; print $bits[$#bits -1]\n; } } Or more simply: #!/usr/bin/perl -w use strict; while (DATA) { if ( /QQQ/ ) { print +( split )[ -2 ], \n; } } John -- Perl isn't a toolbox, but a small machine shop where you can special-order certain sorts of tools at low cost and in short order.-- Larry Wall -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] http://learn.perl.org/
Re: awk command in script?
marys wrote: Hello: Hello, Does anyone know how to use ‘awk’ in a script? perl and awk have a lot of similar features so its usually preferable to use perl in a perl program instead of awk. It must have a different syntax than the unix analog, as does the ‘grep’ command. For grep, the syntax in the c-shell is: “grep ‘string’ , It's the same in every shell because grep is a standalone command. man grep but for Perl the delimiters are slashes: $x = grep / string/ line. That's because in perl grep is a built-in function. Maybe the same thing is going on with Perl. I have searched the following sources with no help on awk: perldoc -f ‘awk’ ‘Beginning Perl’ by S. Cozen ‘CGI101’ and the O’Reilly books: ‘Learning Perl’ aka the llama book ‘Intemediate Perl’ ‘Advanced Perl’ ‘CGI Programming with Perl’ man awk I have a file called /tmp/file.txt with one line: field for grepping on , the script is: #!/usr/bin/perl -w use CGI::Carp qw(fatalsToBrowser); use CGI qw(:standard -no_xhtml); #use CGI ':standard'; use strict; use diagnostics; my $q = new CGI; print $q-header; print $q-start_html(-title=mygrep); my @infile; my $q = new CGI; open (FILEIN, /tmp/file.txt) or die Can't open /tmp/file.txt for reading: $!\n!; open (FILEOUT, /tmp/out.txt) or die Can't open /tmp/out.txt for writing: $!\n!; system chmod 755 /tmp/out.txt; perldoc -f chmod chmod 0755 '/tmp/out.txt' or warn Cannot chmod '/tmp/out.txt' $!; while ( defined(my $line=FILEIN) ){ In a while loop conditional defined() is implied for a readline. chomp($line); push (@infile,$line); } Or more simply: chomp( my @infile = FILEIN ); my @zoom = grep(//,@infile); #looks for '' in @infile Why didn't you just test for // in the while loop, then you wouldn't need two arrays? foreach (@zoom){ print $q-center($q-h3(\nNext line containing '' is: \n), $q-h3($_\n), $q-h3(_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _) ); } Or print this as you found // in the while loop and you wouldn't need either array? print $q-center($q-h2( grep program is finished!\n)); grep is a built-in Perl funtion, not an external program. perldoc -f grep The script works as it should for grep, but what if I want to output $NF (=) when a line has the string ‘field’ in it? There must be a way, but I can't find it. What does $NF contain? I would guess that you want the line number where // was found? If so: while ( my $line = FILEIN ) { next unless //; print $q-center( $q-h3( \nNext line containing '' is:\n ), $q-h3( $_ ), $q-h3( At line number: $. ), $q-h3( '_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _' ) ); } John -- Perl isn't a toolbox, but a small machine shop where you can special-order certain sorts of tools at low cost and in short order.-- Larry Wall -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] http://learn.perl.org/
Re: Processing files in a directory
Susheel Koushik wrote: use the PERL system command. Its a wrapper for system call on your host OS. ex: system(rm *.tmp); Why, when you can just do: unlink *.tmp; John -- Perl isn't a toolbox, but a small machine shop where you can special-order certain sorts of tools at low cost and in short order.-- Larry Wall -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] http://learn.perl.org/
Re: parsing a line
thunder wrote: Hello all Hello, I have the following small file that i am parsing one line at a time (each line consists of hex values) line 1: 0d line 2: line 3: 2000 line 4: 0064 line 5: 76d457ed462df78c7cfde9f9e33724c6 line 6: bded7a7b9f6d763e line 7: 0059010081bb300597603b6f90ef4421 line 8: 001608427754e957a0d281bb30059760 line 9: a72f731c3be6 For line 5, for example, i want to break it up into chunks of 8 hex charaters (ie for example 76d457ed, 462df78c etc). $ echo 0d 2000 0064 76d457ed462df78c7cfde9f9e33724c6 bded7a7b9f6d763e 0059010081bb300597603b6f90ef4421 001608427754e957a0d281bb30059760 a72f731c3be6 |\ perl -lpe'$. == 5 and $_ = join , /[[:xdigit:]]{0,8}/g' 0d 2000 0064 76d457ed 462df78c 7cfde9f9 e33724c6 bded7a7b9f6d763e 0059010081bb300597603b6f90ef4421 001608427754e957a0d281bb30059760 a72f731c3be6 John -- Perl isn't a toolbox, but a small machine shop where you can special-order certain sorts of tools at low cost and in short order.-- Larry Wall -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] http://learn.perl.org/
Re: Testing an array for a match
Lou Hernsen wrote: Hallo Hello, I have an array @Treasures and I want to match anywhere in it for /:1:2:3:/ can I if (@Treasures =~ /:1:2:3:/){} or do i have to change (@Treasures to $Treasures and then $Treasures = @Treasures ; if ($Treasures =~ /:1:2:3:/){} if ( grep /:1:2:3:/, @Treasures ) { John -- Perl isn't a toolbox, but a small machine shop where you can special-order certain sorts of tools at low cost and in short order.-- Larry Wall -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] http://learn.perl.org/
Re: Adding a comma to format localtime
Gregg O'Donnell wrote: I use this line of code: my $datetime = join ' ', (split ' ', localtime)[0,2,1,4,3]; To create this result: Mon 9 Apr 2007 09:15:05 How can I add a comma to this result to get: Mon, 9 Apr 2007 09:15:05 ( my $datetime = localtime ) =~ s{(\S+)\s+(\S+)\s+(\S+)\s+(\S+)\s+(\S+)} {$1, $3 $2 $5 $4}; John -- Perl isn't a toolbox, but a small machine shop where you can special-order certain sorts of tools at low cost and in short order. -- Larry Wall -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] http://learn.perl.org/
Re: Can't use string as a subroutine ref while strict refs
Sara wrote: use strict; use warnings; use CGI; my $q = new CGI; my $do = $q-param('do') || 'main''; if ($do) { $do; } sub main { blah blah } = Trying to call the subroutine main from variable $do but I am gettin' error: Can't use string (main) as a subroutine ref while strict refs. But somehow I don't want to remove the 'use Strict;' Any way out? my $q = new CGI; my $do = $q-param( 'do' ) || \main; if ( ref $do eq 'CODE' ) { $do-(); } sub main { blah blah } John -- Perl isn't a toolbox, but a small machine shop where you can special-order certain sorts of tools at low cost and in short order. -- Larry Wall -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] http://learn.perl.org/ http://learn.perl.org/first-response
Re: indexing... in some way
Adriano Allora wrote: hi to all, Hello, this question isn't exactly a cgi-question, but I need to solve this problem before writing the cgi interface. I've got a list of tagged files. I've listed all the couple word+tag. now, for each word+tag I want to write a file containing all the filename in which compare the couple. This is the script: `sort tagged_files/* | uniq word+tag.txt`; perldoc -q backticks in a void context open(IDX, word+tag.txt); You should verify that the file was actually opened. while(IDX) { next if /^\W.+/; open(TMP, indexes/$_.txt); You should verify that the file was actually opened. $where = `grep -L '$_' tagged_files/*`; print TMP $where; close(TMP); } someone would tell me why the line with grep does not work? The string in $_ has a newline at the end. Is this the fastest way? sort -u tagged_files/* word+tag.txt grep -L -f word+tag.txt tagged_files/* Or did you really want to do it in Perl? Maybe if you could explain in more detail exactly what you want to do? John -- use Perl; program fulfillment -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] http://learn.perl.org/ http://learn.perl.org/first-response
Re: Exact matching using GREP.
Sara wrote: - Original Message - From: Ovid --- Sara [EMAIL PROTECTED] wrote: while (my $row = $sth-fetchrow_hashref) { if (grep /$row-{CAT_TITLE}/, @present) { #matching title with @present elements print $row-{CAT_TITLE}; } Question is how to do EXACT matching using GREP? because the above code prints every element from array for 'php' if the $row-{CAT_TITLE} is 'php' it prints php/counters, php/forums and every element containing php. Assuming I understood your question correctly: if (grep { $_ eq $row-{CAT_TITLE} } @present) { # do something } No, it's not working, probably you didnt' get my question. That's the way I read it and that is how you do it in Perl. It looks like you think that Perl's grep is the same as the grep program you use on the command line but it is not. John -- use Perl; program fulfillment -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] http://learn.perl.org/ http://learn.perl.org/first-response
Re: getstore and comments
Adriano Allora wrote: hi to all, Hello, Some questions: 1) how can I write multine comments? something like /** ... **/? Install this module: http://search.cpan.org/~kane/Acme-Comment-1.02/ 2) the following script doesn't find the page requested in getstore (baolian.local = 127.0.0.1). Why? .local is not a valid TLD (AFAIK) and baolian.local is not equal to 127.0.0.1. You need to use localhost like: $status = getstore('http://localhost/~adrianoallora/index.html', $homepage); 3) $homepage must be a real page/file or I can use it like a normal variable or a filehandle to read with whlie()? I don't understand, sorry. John -- use Perl; program fulfillment -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] http://learn.perl.org/ http://learn.perl.org/first-response
Re: Just to know why ?: is not working here
J. Alejandro Ceballos Z. wrote: I made a CGI that must send a piece of code to screen, otherwise, must return a redirect command (is a banner CGI) What it is stange to me is that the construction: # blah, blah above # # Returns code or redirect to the page print ($str_codetoreturn) ? $cgi_this-header().$str_codetoreturn : $cgi_this-redirect(-uri=$str_redirecto); # # end of cgi sends a premature end of headers error; but: # blah, blah above # # Returns code or redirect to the page if ($str_codetoreturn) { print $cgi_this-header().$str_codetoreturn; } else { print $cgi_this-redirect(-uri=$str_redirecto); } # # end of cgi works fine. ($cgi_this is my CGI object) Why is this? You are falling afoul of perl's precedence rules: perldoc perlop [snip] Terms and List Operators (Leftward) A TERM has the highest precedence in Perl. They include variables, quote and quote-like operators, any expression in parentheses, and any function whose arguments are parenthesized. Actually, there aren't really functions in this sense, just list operators and unary operators behaving as functions because you put parentheses around the arguments. These are all documented in perlfunc. If any list operator (print(), etc.) or any unary operator (chdir(), etc.) is followed by a left parenthesis as the next token, the operator and arguments within parentheses are taken to be of highest precedence, just like a normal function call. In the absence of parentheses, the precedence of list operators such as print, sort, or chmod is either very high or very low depending on whether you are looking at the left side or the right side of the operator. So you should write the expression without parentheses like: # Returns code or redirect to the page print $str_codetoreturn ? $cgi_this-header() . $str_codetoreturn : $cgi_this-redirect( -uri = $str_redirecto ); # # end of cgi John -- use Perl; program fulfillment -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] http://learn.perl.org/ http://learn.perl.org/first-response