How to resolve properly exception
Hi, How can I write exception with Perl ? Any thing like this: If (Application generate Error) { message_html('Error ') } Thanks KN -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]
Re: How to resolve properly exception
on Fri, 14 Feb 2003 12:38:16 GMT, [EMAIL PROTECTED] (Khalid Naji) wrote: How can I write exception with Perl ? See, e.g., Object Oriented Exception Handling in Perl by Arun Udaya Shankar, available at http://www.perl.com/pub/a/2002/11/14/exception.html -- felix -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]
chatroom
Hello all, I'm in the process of writing a cgi based chatroom, and I need to figure out a way to tell who is currently logged in the chatroom. The web page is a frameset with three frames: the first one has a listing of the chatrooms you can go to, the second is the actual chatroom itself which is just an html page with a meta content refresh tag on it (refreshes every 5 seconds), and the last frame has the form where you type in the text to be displayed in the html page. The form simply rewrites the html file with the new text. The users of this chatroom actually log in via apache. I've thought about making the html file a perl script that can grab the env var that holds the name of the person using the browser. The script could update another flat file stating that the person's browser refreshed the page. I can then come up with some way to tell if a person has refreshed within the last 5 to 10 seconds, and if he/she hasn't, then I can say that the person has logged out. I'm not very experienced with cookies, but I'm kind of guessing that there may be an easier way to do this using cookies. Any help is appreciated. Thanks! -Stephen Spalding __ Do you Yahoo!? Yahoo! Shopping - Send Flowers for Valentine's Day http://shopping.yahoo.com -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]
DBI question
Hi List, Sorry if this is a little offtopic but I'm trying to learn DBI for a CGI I'm writing. Been reading http://www.perldoc.com/cpan/DBI.html but seems a little over complex for what I need...just inserts, updates, and queries really. Portability isn't too important. Probably going to use DBD::ODBC on top of it anyway. Also trying to learn about packages and OOP with perl at the same time...yikes. Basically, i'm trying to write a little abstraction layer so that I can say things like: $dbh = MyPackages::DB-new(); #handles my connection stuff. #and then my @values = $dbh-getvalues(username,users); #or maybe, my @rowsHashRefs = @{$dbh-query(select username,email from users)}; #and then foreach my $hashRef (@rowsHashRefs){ my %row = %{$hashRef}; #some html... print user $row{'username'}'s email is $row{'email'}; #or whatever } Not sure how much code I'm actually saving by doing this...Does anybody have experience using DBI or any tricks for keeping things simple and avoiding excessive prepare, execute, selectall_hashrefs...etc. Secondly, I thought I had something working (see my package below) but it's complaining that I'm only passing one argument to selectall_hashref despite the documentation saying you can use just a simple statement. here's the error: Usage: $h-selectall_hashref($statement, $keyfield [, \%attr [, @bind_params ] ]) at Score/DB.pm line 56. Thanks for any pointers... -peter here's the package that I'm trying to write: (be warned: It's ugly) use strict; use DBI; use DBD::ODBC; package MyPackages::DB; sub new { #my $class=shift; my $self={}; bless $self; #go ahead and connect $self-connect(); return $self; } sub connect{ my $self = shift; $self-{dbh} = DBI-connect(dbi:ODBC:myDSN,username,sekretpassword,{ AutoCommit=0, RaiseError=1}) or die(Can't connect! $!); } sub getValue{ my ($self,$field,$table,$condition) = @_; return if($#_2); my @rowHashRefs; if($condition){ @rowHashRefs = @{$self-query(Select $field from $table where $condition)}; }else{ @rowHashRefs = @{$self-query(select $field from $table)}; } #get at our hash from our arrayref [0] and pull out the value of field:$field #if ($#rowHasheRefs == 0){ # return ${${$arrayRef}[0]}{$field}; #} my @fieldValues; foreach my $rowHashRef (@rowHashRefs){ push(@fieldValues,${$rowHashRef}{$field}); } #give back the array of values ...or do I want a reference to it? return @fieldValues; } sub query{ my ($self,$qry) = @_; return unless($qry); return $self-{dbh}-selectall_hashref($qry); #why does it complain here } 1; -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]
RE: DBI question
Basically, i'm trying to write a little abstraction layer Someone already did the work for you, check out Class::DBI. Here is a good article on it, it might be all you need. http://www.perl.com/pub/a/2002/11/27/classdbi.html Rob -Original Message- From: Peter Kappus [mailto:[EMAIL PROTECTED]] Sent: Friday, February 14, 2003 3:59 PM To: CGI Subject: DBI question Hi List, Sorry if this is a little offtopic but I'm trying to learn DBI for a CGI I'm writing. Been reading http://www.perldoc.com/cpan/DBI.html but seems a little over complex for what I need...just inserts, updates, and queries really. Portability isn't too important. Probably going to use DBD::ODBC on top of it anyway. Also trying to learn about packages and OOP with perl at the same time...yikes. Basically, i'm trying to write a little abstraction layer so that I can say things like: $dbh = MyPackages::DB-new(); #handles my connection stuff. #and then my @values = $dbh-getvalues(username,users); #or maybe, my @rowsHashRefs = @{$dbh-query(select username,email from users)}; #and then foreach my $hashRef (@rowsHashRefs){ my %row = %{$hashRef}; #some html... print user $row{'username'}'s email is $row{'email'}; #or whatever } Not sure how much code I'm actually saving by doing this...Does anybody have experience using DBI or any tricks for keeping things simple and avoiding excessive prepare, execute, selectall_hashrefs...etc. Secondly, I thought I had something working (see my package below) but it's complaining that I'm only passing one argument to selectall_hashref despite the documentation saying you can use just a simple statement. here's the error: Usage: $h-selectall_hashref($statement, $keyfield [, \%attr [, @bind_params ] ]) at Score/DB.pm line 56. Thanks for any pointers... -peter here's the package that I'm trying to write: (be warned: It's ugly) use strict; use DBI; use DBD::ODBC; package MyPackages::DB; sub new { #my $class=shift; my $self={}; bless $self; #go ahead and connect $self-connect(); return $self; } sub connect{ my $self = shift; $self-{dbh} = DBI-connect(dbi:ODBC:myDSN,username,sekretpassword,{ AutoCommit=0, RaiseError=1}) or die(Can't connect! $!); } sub getValue{ my ($self,$field,$table,$condition) = @_; return if($#_2); my @rowHashRefs; if($condition){ @rowHashRefs = @{$self-query(Select $field from $table where $condition)}; }else{ @rowHashRefs = @{$self-query(select $field from $table)}; } #get at our hash from our arrayref [0] and pull out the value of field:$field #if ($#rowHasheRefs == 0){ # return ${${$arrayRef}[0]}{$field}; #} my @fieldValues; foreach my $rowHashRef (@rowHashRefs){ push(@fieldValues,${$rowHashRef}{$field}); } #give back the array of values ...or do I want a reference to it? return @fieldValues; } sub query{ my ($self,$qry) = @_; return unless($qry); return $self-{dbh}-selectall_hashref($qry); #why does it complain here } 1; -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]
Perl problem:(
Hello all:) You have been greaet helps to me in the past, and im hoping you can help now:) A friend of mine is in a class learning perl and her script keeps getting an error that there is a bracket missing on line 190. When i looked at it, all i could see was the nested statement problem i had at one time. Do you think that is the problem? and if so, what would be the best way to fix it? I want her to be able to learn it, not to have it done for her, but i want to give her the best advice possible: Script::: #!/usr/bin/perl # # lab3.pl # my @scorearray; sub getinformation() { local $score = 0; local $total = 0; local $ctr = 0; local $rpt_ctr = 0; open (OUTFILE, info.txt) || die cannot open info.txt $!; local $var = bad; while ($var eq bad) { print enter student's first name: ; $firstname = STDIN; chop ($firstname); $length = length($firstname); if (!$length) { print Not a valid First Name, try again.\n; next; { if($length 20) { print Not a valid First Name, try again. \n; next; } if($length) { $var = good; print (OUTFILE $firstname|); } } local $var1 = bad; while ($var1 eq bad) { print enter student's last name: ; $lastname = STDIN; chop ($lastname); if (!$length1) { print Not a valid last name, try again. \n; next; } if($length1 20) { print Not a valid last name, try again. \n; next; } if($length1) { $var1 = good; print(OUTFILE $lastname|); } } local $var2 = bad; while($var2 = bad) { print enter student's SSN:; $ssn = STDIN; chop($ssn); $length2=length($ssn); if(!$length2) { print Not a valid Social Security Number, try again.\n; next; } $ssn =~ s/[^\d]//g; if($ssn !~ m/^\d{9}$/) { print Not a valid Social Security Number, try again.\n; next; } if($ssn =~ m/^\d{9}$/) { $var2 = good; print(OUTFILE $ssn); } } local $ctr1 = 1; while ($ctr 4) { print enter a test score $ctr1: ; $score = STDIN; chop ($score); if ($score eq q) { last; } local $error = true; testscore($score); if ($error eq false) { next; } else { $total += $score; $ctr++; $ctr1++; $scorearray[$ctr]=$score; } }; while($rpt_ctr = $ctr) { print (OUTFILE $scorearray[$rpt_ctr]); if($rpt_ctr 3) { print(OUTFILE |); } $rpt_ctr++; } print (OUTFILE \n); getscore(); }; sub getscore() { local $display = 4 - $ctr; local $avg = $total / 4; print Score total is $total \n; print number of scores entered is $ctr \n; print Score Average is $avg \n; print $firstname is missing $display tests\n; warnings(); }; sub testscore() { if ($score 100) { print invalid score, please make sure the score is less than or equal to 100\n; $error = false; } if ($score 0) { print invalid score, please make sure the score is greater than or equal to 0\n; $error = false; } }; sub warnings() { if ($avg = 60) { print Grim Warning, $firstname !; } elsif ($avg = 70) { print Uh-oh! You maybe in trouble, $firstname !; } elsif ($avg = 80) { print Good going $firstname \n You have a
RE: Perl problem:(
First if statement. $length = length($firstname); if (!$length) { print Not a valid First Name, try again.\n; next; ---{ Greg Smith -Original Message- From: t [mailto:[EMAIL PROTECTED]] Sent: Friday, February 14, 2003 4:05 PM To: [EMAIL PROTECTED] Subject: Perl problem:( Hello all:) You have been greaet helps to me in the past, and im hoping you can help now:) A friend of mine is in a class learning perl and her script keeps getting an error that there is a bracket missing on line 190. When i looked at it, all i could see was the nested statement problem i had at one time. Do you think that is the problem? and if so, what would be the best way to fix it? I want her to be able to learn it, not to have it done for her, but i want to give her the best advice possible: Script::: #!/usr/bin/perl # # lab3.pl # my @scorearray; sub getinformation() { local $score = 0; local $total = 0; local $ctr = 0; local $rpt_ctr = 0; open (OUTFILE, info.txt) || die cannot open info.txt $!; local $var = bad; while ($var eq bad) { print enter student's first name: ; $firstname = STDIN; chop ($firstname); $length = length($firstname); if (!$length) { print Not a valid First Name, try again.\n; next; { if($length 20) { print Not a valid First Name, try again. \n; next; } if($length) { $var = good; print (OUTFILE $firstname|); } } local $var1 = bad; while ($var1 eq bad) { print enter student's last name: ; $lastname = STDIN; chop ($lastname); if (!$length1) { print Not a valid last name, try again. \n; next; } if($length1 20) { print Not a valid last name, try again. \n; next; } if($length1) { $var1 = good; print(OUTFILE $lastname|); } } local $var2 = bad; while($var2 = bad) { print enter student's SSN:; $ssn = STDIN; chop($ssn); $length2=length($ssn); if(!$length2) { print Not a valid Social Security Number, try again.\n; next; } $ssn =~ s/[^\d]//g; if($ssn !~ m/^\d{9}$/) { print Not a valid Social Security Number, try again.\n; next; } if($ssn =~ m/^\d{9}$/) { $var2 = good; print(OUTFILE $ssn); } } local $ctr1 = 1; while ($ctr 4) { print enter a test score $ctr1: ; $score = STDIN; chop ($score); if ($score eq q) { last; } local $error = true; testscore($score); if ($error eq false) { next; } else { $total += $score; $ctr++; $ctr1++; $scorearray[$ctr]=$score; } }; while($rpt_ctr = $ctr) { print (OUTFILE $scorearray[$rpt_ctr]); if($rpt_ctr 3) { print(OUTFILE |); } $rpt_ctr++; } print (OUTFILE \n); getscore(); }; sub getscore() { local $display = 4 - $ctr; local $avg = $total / 4; print Score total is $total \n; print number of scores entered is $ctr \n; print Score Average is $avg \n; print $firstname is missing $display tests\n; warnings(); }; sub testscore() { if ($score 100) { print invalid score, please make sure the score is less than or equal to 100\n; $error = false; } if ($score 0) { print invalid score, please make sure the score is greater than or equal to 0\n; $error = false; } };
Re: Perl problem:(
t wrote: Hello all:) You have been greaet helps to me in the past, and im hoping you can help now:) A friend of mine is in a class learning perl and her script keeps getting an error that there is a bracket missing on line 190. When i looked at it, all i could see was the nested statement problem i had at one time. Do you think that is the problem? and if so, what would be the best way to fix it? I want her to be able to learn it, not to have it done for her, but i want to give her the best advice possible: Time to start reading ;-) perldoc -f chomp perldoc -f chop perldoc -f my perldoc -f our perldoc -f local 2 rules first off... Don't ever use 'chop'...at least not until you know why you should never use chop, then you can Don't ever use 'local'...at least not until you know why you should never use local, then you can ;-) http://perl.plover.com/FAQs/Namespaces.html Where is the: use strict; use warnings; ??? perldoc perlstyle (wouldn't hurt to have a glance at) 1. Don't need semi-colons after sub routine definitions or other blocks. 2. In general it is considered ok to put the 'main' of a Perl program into the actual 'main' rather than in a subroutine that is the only thing called. 3. In general the main of Perl goes before the sub definitions. 4. Don't want (need) empty parenthesis in a sub definition, I am not sure but this might actually force the sub to not allow args? 5. warnings is a VERY bad name for a subroutine, don't use it. 6. Multiple if statements involving the same parameter(s) can be chained using 'elsif'. 7. Very seldom should you use the same exact error/warning message twice, this will make debugging a nightmare, tell the user what they screwed up, yell it at them if it helps ;-)... 8. Return something from subroutines, test that return code. 9. Don't get frustrated, don't quit Script::: #!/usr/bin/perl # # lab3.pl # my @scorearray; sub getinformation() { local $score = 0; local $total = 0; local $ctr = 0; local $rpt_ctr = 0; open (OUTFILE, info.txt) || die cannot open info.txt $!; local $var = bad; while ($var eq bad) { print enter student's first name: ; $firstname = STDIN; chop ($firstname); $length = length($firstname); if (!$length) { print Not a valid First Name, try again.\n; next; { Right here is your bad brace. if($length 20) There is much much worse code in production environments, so don't get discouraged at the amount I picked on, tough love http://danconia.org -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]
php like behavior for my perl CGI...and DBI followup
Thanks to Rob for his Class::DBI suggestion earlier. Looks like what I needed was simply: my %hash; return $sth-fetchall_arrayref(\%hash); to return an array of hashes... As I'm continuing with my latest project, I'm finding myself wishing I could do some PHP type things...specifically, as I'm formatting the HTML there's often a need to throw in spacer gifs of various dimensions to make things look pretty... In PHP this can be streamlined by creating a function which prints out your image tag for your and accepts the height and width as parameters. like so: function spacer($y,$x){ ?img src=spacer.gif height=?=$y? width=?=$x?? } To call it, you would just say: ?spacer(10,50)? and it would throw in an image tag with a height of 10 and a width of 50...or whatever. My question is this: i'd like to be able to use the printeof; behavior to send large blocks of HTML to the browser. Is there A way that I can reroute that statement to send my string through a sub that basically does the following: sub sendit{ my $txt = shift; $txt =~ s/\?(.+?)\?/(?{eval($1)})/g; #evaluate what's between my ?? delimeters and print the result print $txt; } I know I could say: my $vareof; some HTML with some PHP-type function in it?someSub(50)? haha! eof sendit($var); BUT that seems like a lot to type...I wish I could just say: senditeof; somHTML with some PHP-type function in it?someSub(50)? haha! eof and have sendit() parse and execute my ?? piece and print the result. But, alas, that seems to start a never ending perl child... Thanks once again for any sage wisdom you may have. -peter -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]