RE: using strict
I am sorry David, it just seems like I am having trouble communicating clearly. We are apparently in a disagreement, but your points are not at all what I wanted to explain... I'll try again. you should test it yourself. i am not sure how good you are with Perl and i don't want to sound like i-know-Perl-more-than-you guy but your example won't even compile. 'perl -e' is exactly the same as if you put it in a script. why do you think there is a different? As you said, don't go there. I still consider myself a beginner, but I think you presume too much, as you might find out in what follows. I agree, they are not different in a strict sense, but if you have a long script, and try to run one line from that script in 'perl -e', it will obviously not work. Just consider this: In my first response to the OP, I took a few lines from his script and changed them, just to demonstrate a point. I never intended those few lines to be usable alone. I would have thought that obvious from the context. I'll just need to be clearer next time, sorry about that. unfortunately, other than coding style, the following is EXACTLY the same: [panda]# perl -e 'open(A,F) || die' [panda]# perl -e 'open A,F or die' True, but these are not. [panda]# perl -e 'open(A,F) || $_ += 2' [panda]# perl -e 'open A,F or $_ += 2' which when deparsed, respectively give: open(A, 'F') || $_ += 2; $_ += 2 unless open A, 'F'; One gives a syntax error ( Can't modify logical or (||) in addition (+) at -e line 1, near 2; ), the other works. So if you get used to using 'or' to check error status, no matter what you use on the left or right side of the 'or', it will work. That is what I meant. Getting used to some good practices early on is useful to prevent getting bitten by obscure bugs (yes, I refer to precedence as obscure, because not everyone knows their precedence table by heart, and it may take a little time to figure out that precedence is the problem). Of course, in all these cases, using parentheses to fix precedence is an alternative. But using or also makes the code read more like English, which (in my humble opinion) makes it easier to understand, and self-documenting. But that's very subjective... you also failed to tell the OP that they are the same and now the OP will never use '||' again and he will go around and spread that word that 'open(...) || die' is wrong which is not! I don't remember ever saying it's wrong, or to never use '||'. Everything I said was personal preference, but I still think it's a good practice. If what I said was interpreted otherwise, I am sorry. Natural language is so ambiguous! :-) It's obviously all about context. In the context of checking error status, I believe it's better to use 'or', simply because of the precedence issue I noted above. But both work, obviously. And if you never use anything other than a simple die in the right-hand side of the '||', you'll never have a problem. But I prefer using 'or' even in those cases, simply because in 2 years, if I haven't done Perl for a few months, and I want to modify a program to use more complex error-handling, I might not remember to wrap it in parentheses. I hope I made my points clear. I am sorry if my first posts mislead anyone, or if the explanations left to be desired. I hope this message sets all of that straight. -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] http://learn.perl.org/ http://learn.perl.org/first-response
RE: using strict
Hello Joseph, One thing I would request is that you trim off any material to which you are not responding from old posts. This helps keep bandwidth and storage needs down. I'll keep that in mind. I usually don't keep all the thread of replies that Outlook ( grrr) keeps at the bottom, seems like I forgot that time. J-S -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] http://learn.perl.org/ http://learn.perl.org/first-response
RE: using strict
Hello Joseph, David, A stronger argument has to do with mindset. The || operator is an expression evaluation operator, appropriate to mathematical or paramathematical expressions. The context really calls for a flow-control operator, or. Thanks for making my point better than I could, Joseph. Yes, it's coding style, but I think clarity is an important part of coding standards (not just official ones, but personal ones too). Thanks again, J-S -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] http://learn.perl.org/ http://learn.perl.org/first-response
RE: using strict
Guay Jean-Sbastien wrote: I am sorry David, it just seems like I am having trouble communicating clearly. We are apparently in a disagreement, but your points are not at all what I wanted to explain... I'll try again. i totally understand what you are saying and like i said before, your point is well taken but you failed to explain the reasons behind it and you use a couple of examples that have syntax error which doesn't serve that well. unfortunately, other than coding style, the following is EXACTLY the same: [panda]# perl -e 'open(A,F) || die' [panda]# perl -e 'open A,F or die' True, but these are not. [panda]# perl -e 'open(A,F) || $_ += 2' [panda]# perl -e 'open A,F or $_ += 2' well, they are not because one has a syntax error which will never equal to anything that's legal Perl. which when deparsed, respectively give: open(A, 'F') || $_ += 2; $_ += 2 unless open A, 'F'; One gives a syntax error ( Can't modify logical or (||) in addition (+) at -e line 1, near 2; ) right. so please let's not use it as an example because it simply wont' run. Of course, in all these cases, using parentheses to fix precedence is an alternative. But using or also makes the code read more like English, which (in my humble opinion) makes it easier to understand, and self-documenting. But that's very subjective... right. you should state that OP's version is correct as well and that you want OP to change it because you feel your version is better for personal perference and because of possible precedence problem with '||' in other cases. you also failed to tell the OP that they are the same and now the OP will never use '||' again and he will go around and spread that word that 'open(...) || die' is wrong which is not! I don't remember ever saying it's wrong, or to never use '||'. Everything I said was personal preference, but I still think it's a good practice. If what I said was interpreted otherwise, I am sorry. Natural language is so ambiguous! :-) right. i could have interpreted it the other way as well. :-) I hope I made my points clear. I am sorry if my first posts mislead anyone, or if the explanations left to be desired. I hope this message sets all of that straight. it does. even if it doesn't and OP end up using 'open H,F or die' it's no big deal. all i don't want OP (or other beginners) to think is that 'open(H,F) || die' is not correct. david -- s$s*$+/tgmecJntgRtgjvqpCvuwL$;$;=qq$ \x24\x5f\x3d\x72\x65\x76\x65\x72\x73\x65 \x24\x5f\x3b\x73\x2f\x2e\x2f\x63\x68\x72 \x28\x6f\x72\x64\x28\x24\x26\x29\x2d\x32 \x29\x2f\x67\x65\x3b\x70\x72\x69\x6e\x74 \x22\x24\x5f\x5c\x6e\x22\x3b\x3b$;eval$; -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] http://learn.perl.org/ http://learn.perl.org/first-response
Re: using strict
[EMAIL PROTECTED] wrote: Yet another great explanation.. What explanation? I see niothing above this. Please do not top-post when posting to this list. Instead, follow the material to which you are directly responding with your response, then trim any extraneous material. Most of us keep records of recent posts, and newsreaders will also thread them appropriately, so if people need the entire text of an original post, they can refer to it. . thank you! But I still need to know how to print each specific element # along with its data? Is this the right way to go for storing each line in its own element??? while $line FILEHANDLE my @tsm = FILEHANDLE Which do you want? This is a repeat of the same problem in your earlier post. Do each task once, and only once foreach $_ (@tsm) If you explicitly assign a variable out of a for loop, make it a meaningful and descriptive variable name. The default variable is assigned automatically if you do not provide a variable to receive the values produced. Either: foraeach (@tsm) { # pronounce the word tsm aloud. Now say it ten time, fast. do something($_) } or foreach $thing_going_bump (@things_that_go_bump_in_the_night) { if (is_significant($thing_going_bump) { investigate_and_confront($thing_going_bump) } else { go_back_to_sleep(); } } Joseph -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] http://learn.perl.org/ http://learn.perl.org/first-response
Re: using strict
Guay Jean-Sébastien wrote: Hello Derek, Guay, Err, my first name is Jean-Sebastien. Hi Jean-Sebastien, My last name is Guay. French-language people have a bad habit to put the last name first, as in Guay, Jean-Sebastien... So I understand why this is a bit confusing. Thanks for the clarification. It is nice to know how to address each other. One thing I would request is that you trim off any material to which you are not responding from old posts. This helps keep bandwidth and storage needs down. Joseph -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] http://learn.perl.org/ http://learn.perl.org/first-response
Re: using strict
david wrote: Guay Jean-Sébastien wrote: open (CRITICALSERVERS, $crout) || die can't open file \n: $!; As I said, you should replace || by or in the above line. See the precedence rules in perldoc perlop for details. why do you think so? is there any problem in the above line? Good point. The explcit parentheses do indeed avoid the precedence problems that would come with: open CRITICALSERVERS, $crout || die can't open file \n: $!; Where the open function would take the second line as an alternative argument. For instance, I have no file named 'crout' in my filesystem, yet: Greetings! E:\d_drive\perlStuffperl -w open CRITICALSERVERS, 'crout' || die can't open file \n: $!; close CRITICALSERVERS; ^Z does not produce an error message, because the die clause gets taken as an alternative. A stronger argument has to do with mindset. The || operator is an expression evaluation operator, appropriate to mathematical or paramathematical expressions. The context really calls for a flow-control operator, or. Joseph -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] http://learn.perl.org/ http://learn.perl.org/first-response
Re: using strict
Guay Jean-Sébastien wrote: There is no problem syntactically. But there is a problem with the precdence. If you ever have another operation on either the left or right side of the || operator, the || operator will bind tighter than the other operation. So for example, if you do: my $errors = 0; open (CRITICALSERVERS, $crout) || $errors += 2; that will translate to: my $errors = 0; ( open (CRITICALSERVERS, $crout) || $errors ) += 2; Nope. Please credit the Perl interpreter with some common sense. There are few, if any, instances where Perl would override explicit parentheses, and does not in this case. To illustrate, I restore the parens to the example I used in a parallel response: Greetings! E:\d_drive\perlStuffperl -w open (CRITICALSERVERS, 'crout') || die can't open file \n: $!; close CRITICALSERVERS; ^Z can't open file : No such file or directory at - line 1. In most cases, Perl will do the right thing with statements expressed in natural form. Joseph -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] http://learn.perl.org/ http://learn.perl.org/first-response
Re: using strict
On Sun, Apr 04, 2004 at 01:28:42PM -0700, R. Joseph Newton wrote: Guay Jean-Sébastien wrote: my $errors = 0; open (CRITICALSERVERS, $crout) || $errors += 2; that will translate to: my $errors = 0; ( open (CRITICALSERVERS, $crout) || $errors ) += 2; Nope. $ perl -MO=Deparse,-p -e 'open (CRITICALSERVERS, $crout) || $errors += 2' Can't modify logical or (||) in addition (+) at -e line 1, at EOF -e had compilation errors. ((open(CRITICALSERVERS, $crout) || $errors) += 2); -- Paul Johnson - [EMAIL PROTECTED] http://www.pjcj.net -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] http://learn.perl.org/ http://learn.perl.org/first-response
Re: using strict
Paul Johnson wrote: On Sun, Apr 04, 2004 at 01:28:42PM -0700, R. Joseph Newton wrote: Guay Jean-Sébastien wrote: my $errors = 0; open (CRITICALSERVERS, $crout) || $errors += 2; that will translate to: my $errors = 0; ( open (CRITICALSERVERS, $crout) || $errors ) += 2; Nope. $ perl -MO=Deparse,-p -e 'open (CRITICALSERVERS, $crout) || $errors += 2' Can't modify logical or (||) in addition (+) at -e line 1, at EOF -e had compilation errors. ((open(CRITICALSERVERS, $crout) || $errors) += 2); Thanks. I stand corrected. I'm afraid I am not very familiar with these pitfalls, because my coding style doesn't often bring me up against them. Usually, if I felt a need to consult precedence tables to be sure, I would just use explicit parentheses. Joseph -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] http://learn.perl.org/ http://learn.perl.org/first-response
RE: using strict
Guay, ooops. that would be it! someone's else's eyes are always better! Thanks for the nice explanation! My code is now compiling smoothly! What do you mean when you say Just use $line inside the while, and you'll read one line at a time. ??? I am doing this, aren't I? FROM GUAY: [[ By the way, why are you reading one line in the whiles parentheses, and then slurping the rest of the file into an array inside the while? ]] RESPONSE FROM DEREK : I did notice this after you pointed it out, so my goal is to read in all the lines and put them in their own element, then print out those specific elements. Do you have any thoughts? thanks! Derek B. Smith OhioHealth IT UNIX / TSM / EDM Teams 614-566-4145 Guay Jean-Sébastien [EMAIL PROTECTED] 04/01/2004 05:10 PM To: Perl Beginners [EMAIL PROTECTED] cc: Subject:RE: using strict Hello Derek, When using strict, the error message should point you to the line where the error is. It's usually pretty darn good at pointing the right line. In this case, I bet it's this one: while ( defined($line = CRITICALSERVERS) ) { There is no declaration of the $line variable. Try declaring it first, like this: my $line; while ( defined($line = CRITICALSERVERS) ) { I think that's the only variable that wasn't declared. If you get any more error messages, you can check them out and figure out what's wrong. Just a suggestion: You should use or instead of || when checking if a file opened correctly, because if you ever want to do some operations on the right side, or will bind less tightly and allow your operations to work correctly without needing parentheses to fix the precedence. By the way, why are you reading one line in the while's parentheses, and and then slurping the rest of the file into an array inside the while? That means your while will only be run once, because at the next iteration you'll already be at the end of the file... Just use $line inside the while, and you'll read one line at a time. You also chomped your line, which is good. Just remember to re-add the line ending (\n) when printing the lines as you're doing, or else all the lines will be printed on a single, very long line. I'll let the experts explain what soft references and barewords are. I've never used either, as I come from the relatively new school of programming. As I understand, they are features of the language that are dangerous to use in some contexts, so use strict disallows them knowing that if you really want to use them, you'll disable strict for the block of code where you use it, and then re-enable it. Hope this helps, Jean-Sébastien Guay -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] http://learn.perl.org/ http://learn.perl.org/first-response
Re: using strict
Yet another great explanation... thank you! But I still need to know how to print each specific element # along with its data? Is this the right way to go for storing each line in its own element??? while $line FILEHANDLE my @tsm = FILEHANDLE foreach $_ (@tsm) print $_ , \n; Derek B. Smith OhioHealth IT UNIX / TSM / EDM Teams [EMAIL PROTECTED] 04/01/2004 06:02 PM To: [EMAIL PROTECTED], [EMAIL PROTECTED] cc: Subject:Re: using strict In a message dated 4/1/2004 5:03:40 PM Eastern Standard Time, [EMAIL PROTECTED] writes: People of the Perl, from my understanding strict disallows soft references, ensures that all variables are declared before usage and disallows barewords except for subroutines. what is a soft reference? A soft reference is a symbolic reference. For example: $hello = Hey, what's up?; $var = hello; print $$var; #or, if it helps you see it better 'print ${$var}' This may be what you want, but may not be (usually you want hard references), hence use strict 'refs' what is a bareword? A bareword is...a bare...word? Really its just a word not expicitly denoted to be something. For example: sub foo { 1 } foo; foo is a barewords. In the above case, strict doesn't freak out because the subroutine is predeclared. But if you just throw around words whenever you want, it will freak out. For example, @array = (String, String, String, String); String is a bareword. strict will throw up an exception so fast. 2 exceptions to the bareword throw-up: left side of '=' and inside curly braces. For Example: %hash = ( left = 'right' ) print $hash{left}; why is strict disallowing a compile here When I comment out strict the syntax checks outs as ok!??? how do I display each element # with its corresponding data line, b/c I only want certain elements printed out? People already answered these. On an aside, Your code may compile dandy, that's not what strict is for. If perl could catch the errors like the ones you stated, there would be no need for strict. Strict is basically just so you don't screw up. You don't want barewords because you might think it means something else. You don't want soft references when you think you're using hard references. You don't want your code to screw up and you not to know why cause you used $var on 10,000 of the 20,000 lines and $Var on the other 10,000. -will (the above message is double rot13 encoded for security reasons) Most Useful Perl Modules -strict -warnings -Devel::DProf -Benchmark -B::Deparse -Data::Dumper -Clone (a Godsend) -Perl::Tidy -Beautifier
Re: using strict
[EMAIL PROTECTED] wrote: Yet another great explanation... thank you! But I still need to know how to print each specific element # along with its data? Is this the right way to go for storing each line in its own element??? while $line FILEHANDLE my @tsm = FILEHANDLE foreach $_ (@tsm) print $_ , \n; my @tsm; while (my $line = FILEHANDLE) { chomp($line); push @tsm, $line; print $line\n; } print '-' x 60, \n; use Data::Dumper; print Dumper([EMAIL PROTECTED]); -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] http://learn.perl.org/ http://learn.perl.org/first-response
RE: using strict
Hello Derek, Guay, Err, my first name is Jean-Sebastien. My last name is Guay. French-language people have a bad habit to put the last name first, as in Guay, Jean-Sebastien... So I understand why this is a bit confusing. What do you mean when you say Just use $line inside the while, and you'll read one line at a time. ??? I am doing this, aren't I? No. I will explain step by step what your code does: open (CRITICALSERVERS, $crout) || die can't open file \n: $!; As I said, you should replace || by or in the above line. See the precedence rules in perldoc perlop for details. while ( defined($line = CRITICALSERVERS) ) { Here, you read one line into $line, and the current line pointer advances to the next line. The while is essentially there to check that you don't read past the end of file. So in theory, the above construct will read one line at a time, until the end of file. chomp ($line); Here, you remove the end of line delimiter (\n, \r\n or \r depending on your platform). This is good, before working with the text inside a variable you should make sure there is no end of line. my @tsm = CRITICALSERVERS; Here, you slurp the whole file, minus the first line which you read into $line above, into an array. After this, the whole file will be in your @tsm array, one line per array element, and the current line pointer will be at the end of the file. foreach $_ (@tsm) { print $_; } This will print every line that's in the array. Note that as I said above, the first line will not be in the array, so you will not see all the lines of your file printed onscreen... } close (CRITICALSERVERS); Perhaps you are not familiar with the term slurp. What that means, is that you are reading the whole file into memory. This is wasteful, and can lead to problems later, if you try to do it with a very large file (not enough memory, machine starts swapping, etc). But it is also sometimes faster, if you need to do a very simple step on every line of your file. In most cases, you should not slurp a file. Instead, read one line at a time. Here is the code I would suggest. open (CRITICALSERVERS, $crout) or die can't open file \n: $!; my $line; while ( $line = CRITICALSERVERS ) { chomp ($line); print $line . \n; } close (CRITICALSERVERS); Essentially, the changes are the following: 1) Changed || for or on the open line 2) No need to check if $line is defined after reading, since $line will at least contain a \n when the line is empty and will contain nothing at end of file. 3) The while will read one line at a time, and print it. Nowhere do I read the whole file at once, as you did in your my @tsm = CRITICALSERVERS; line. So that's what I meant. I hope it is all clear. If you really wanted to slurp the file, here is the corresponding code you should use, so that you are not missing one line... open (CRITICALSERVERS, $crout) or die can't open file \n: $!; my @tsm = CRITICALSERVERS close (CRITICALSERVERS); foreach my $line (@tsm) { chomp ($line); print $line . \n; } As you can see, it is very similar, but since we get the whole file into the array, we can close it right after, and also we loop over the array instead of looping and getting one line at a time until we get to the end of the file. Hope this helps, Jean-Sébastien -Message d'origine- De: [EMAIL PROTECTED] [mailto:[EMAIL PROTECTED] Date: 2 avril, 2004 10:12 À: Guay Jean -Sébastien Cc: Perl Beginners Objet: RE: using strict Guay, ooops. that would be it! someone's else's eyes are always better! Thanks for the nice explanation! My code is now compiling smoothly! What do you mean when you say Just use $line inside the while, and you'll read one line at a time. ??? I am doing this, aren't I? FROM GUAY: [[ By the way, why are you reading one line in the whiles parentheses, and then slurping the rest of the file into an array inside the while? ]] RESPONSE FROM DEREK : I did notice this after you pointed it out, so my goal is to read in all the lines and put them in their own element, then print out those specific elements. Do you have any thoughts? thanks! Derek B. Smith OhioHealth IT UNIX / TSM / EDM Teams 614-566-4145 Guay Jean-Sébastien [EMAIL PROTECTED] 04/01/2004 05:10 PM To:Perl Beginners [EMAIL PROTECTED] cc: Subject:RE: using strict Hello Derek, When using strict, the error message should point you to the line where the error is. It's usually pretty darn good at pointing the right line. In this case, I bet it's this one: while ( defined($line = CRITICALSERVERS) ) { There is no declaration of the $line variable. Try declaring it first, like this: my $line; while ( defined($line = CRITICALSERVERS) ) { I think that's the only variable that wasn't declared. If you get any more error messages, you can check
RE: using strict
Guay Jean-Sbastien wrote: open (CRITICALSERVERS, $crout) || die can't open file \n: $!; As I said, you should replace || by or in the above line. See the precedence rules in perldoc perlop for details. why do you think so? is there any problem in the above line? david -- s$s*$+/tgmecJntgRtgjvqpCvuwL$;$;=qq$ \x24\x5f\x3d\x72\x65\x76\x65\x72\x73\x65 \x24\x5f\x3b\x73\x2f\x2e\x2f\x63\x68\x72 \x28\x6f\x72\x64\x28\x24\x26\x29\x2d\x32 \x29\x2f\x67\x65\x3b\x70\x72\x69\x6e\x74 \x22\x24\x5f\x5c\x6e\x22\x3b\x3b$;eval$; -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] http://learn.perl.org/ http://learn.perl.org/first-response
RE: using strict
Guay Jean-Sbastien wrote: open (CRITICALSERVERS, $crout) || die can't open file \n: $!; As I said, you should replace || by or in the above line. See the precedence rules in perldoc perlop for details. why do you think so? is there any problem in the above line? There is no problem syntactically. But there is a problem with the precdence. If you ever have another operation on either the left or right side of the || operator, the || operator will bind tighter than the other operation. So for example, if you do: my $errors = 0; open (CRITICALSERVERS, $crout) || $errors += 2; that will translate to: my $errors = 0; ( open (CRITICALSERVERS, $crout) || $errors ) += 2; because the += operator has lower precedence than the || operator. (I admit, this is a contrived example, but I have been bitten by this before!) So whenever using || for an error condition, it is suggested that you use 'or' instead, since 'or' is lowest precedence of all (xor is the same precedence). So when using or, anything you do will be like this: ( some operation that returns a false value on error ) or ( something to do in case of error ); without explicitly requiring the parentheses, because of the precedence. It was just a suggestion, but if you ever see a line with an || not being executed in the right order, think about that and either use parentheses to explicitly state what you mean, or use 'or'. Hope this explains this suggestion that might seem, at first, to come out of the blue! J-S -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] http://learn.perl.org/ http://learn.perl.org/first-response
RE: using strict
Jean, can I email you from know on??? : ) You are so through! Thank you! I now understand the one line code and slurping coding within the array! My reasoning for the array was so that I can only and easily print out specific elements. So do you still suggest just using the while construct instead of the array assuming memory is not an issue? Derek B. Smith OhioHealth IT UNIX / TSM / EDM Teams Guay Jean-Sébastien [EMAIL PROTECTED] 04/02/2004 02:20 PM To: Perl Beginners [EMAIL PROTECTED] cc: Subject:RE: using strict Hello Derek, Guay, Err, my first name is Jean-Sebastien. My last name is Guay. French-language people have a bad habit to put the last name first, as in Guay, Jean-Sebastien... So I understand why this is a bit confusing. What do you mean when you say Just use $line inside the while, and you'll read one line at a time. ??? I am doing this, aren't I? No. I will explain step by step what your code does: open (CRITICALSERVERS, $crout) || die can't open file \n: $!; As I said, you should replace || by or in the above line. See the precedence rules in perldoc perlop for details. while ( defined($line = CRITICALSERVERS) ) { Here, you read one line into $line, and the current line pointer advances to the next line. The while is essentially there to check that you don't read past the end of file. So in theory, the above construct will read one line at a time, until the end of file. chomp ($line); Here, you remove the end of line delimiter (\n, \r\n or \r depending on your platform). This is good, before working with the text inside a variable you should make sure there is no end of line. my @tsm = CRITICALSERVERS; Here, you slurp the whole file, minus the first line which you read into $line above, into an array. After this, the whole file will be in your @tsm array, one line per array element, and the current line pointer will be at the end of the file. foreach $_ (@tsm) { print $_; } This will print every line that's in the array. Note that as I said above, the first line will not be in the array, so you will not see all the lines of your file printed onscreen... } close (CRITICALSERVERS); Perhaps you are not familiar with the term slurp. What that means, is that you are reading the whole file into memory. This is wasteful, and can lead to problems later, if you try to do it with a very large file (not enough memory, machine starts swapping, etc). But it is also sometimes faster, if you need to do a very simple step on every line of your file. In most cases, you should not slurp a file. Instead, read one line at a time. Here is the code I would suggest. open (CRITICALSERVERS, $crout) or die can't open file \n: $!; my $line; while ( $line = CRITICALSERVERS ) { chomp ($line); print $line . \n; } close (CRITICALSERVERS); Essentially, the changes are the following: 1) Changed || for or on the open line 2) No need to check if $line is defined after reading, since $line will at least contain a \n when the line is empty and will contain nothing at end of file. 3) The while will read one line at a time, and print it. Nowhere do I read the whole file at once, as you did in your my @tsm = CRITICALSERVERS; line. So that's what I meant. I hope it is all clear. If you really wanted to slurp the file, here is the corresponding code you should use, so that you are not missing one line... open (CRITICALSERVERS, $crout) or die can't open file \n: $!; my @tsm = CRITICALSERVERS close (CRITICALSERVERS); foreach my $line (@tsm) { chomp ($line); print $line . \n; } As you can see, it is very similar, but since we get the whole file into the array, we can close it right after, and also we loop over the array instead of looping and getting one line at a time until we get to the end of the file. Hope this helps, Jean-Sébastien -Message d'origine- De: [EMAIL PROTECTED] [mailto:[EMAIL PROTECTED] Date: 2 avril, 2004 10:12 À: Guay Jean -Sébastien Cc: Perl Beginners Objet: RE: using strict Guay, ooops. that would be it! someone's else's eyes are always better! Thanks for the nice explanation! My code is now compiling smoothly! What do you mean when you say Just use $line inside the while, and you'll read one line at a time. ??? I am doing this, aren't I? FROM GUAY: [[ By the way, why are you reading one line in the whiles parentheses, and then slurping the rest of the file into an array inside the while? ]] RESPONSE FROM DEREK : I did notice this after you pointed it out, so my goal is to read in all the lines and put them in their own element, then print out those specific elements. Do you have any thoughts? thanks! Derek B. Smith OhioHealth IT UNIX / TSM / EDM Teams 614-566-4145 Guay Jean-Sébastien [EMAIL PROTECTED] 04/01/2004 05:10 PM To:Perl Beginners
RE: using strict
Hello Derek, can I email you from know on??? : ) You are so through! Thank you! No, I would prefer that you always reply to the list. First reason is that other beginners can benefit from our exchange (and the mails end up in a searchable archive), second is that if I'm not there (or for the many subjects which I don't know that well, I still think of myself as a beginner too), others can reply to you. I just saw that your message contained some questions which I could answer, and I figured I would give the others who are always answering questions (you know who you are!) a short break. :-) My reasoning for the array was so that I can only and easily print out specific elements. So do you still suggest just using the while construct instead of the array assuming memory is not an issue? Well, if you're going to be using those elements only once (i.e. your processing of the file is sequential) I would still suggest reading one line at a time. You never know when you might start using very large files instead of small ones... But of course, if you're going to read only one line of the file, and you already know the line number, you might want to slurp into an array and get the specific line right away. I think there was a discussion about this (how to get to a specific line number directly without looping) on this list not long ago. Search the archives to find it. Also, if you're going to do something to one line, then go to another one, and then might come back to the first one, using an array will be easier. If you don't, you'll have to seek to the beginning of the file and restart your loop to go back to the first line you processed. Such non-sequential processing on a file is usually done with the whole file in memory, i.e. with slurping. It all depends. Both are valid methods, but in general reading one line at a time leads to fewer problems if what you're doing can be done with that method. Hope this helps. Just remember, in programming nothing is black and white. Keep an open mind, and remember that techniques you dismissed for solving one problem might turn out to be the best way to solve another one. J-S -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] http://learn.perl.org/ http://learn.perl.org/first-response
RE: using strict
Guay Jean-Sbastien wrote: Guay Jean-Sbastien wrote: open (CRITICALSERVERS, $crout) || die can't open file \n: $!; As I said, you should replace || by or in the above line. See the precedence rules in perldoc perlop for details. why do you think so? is there any problem in the above line? There is no problem syntactically. But there is a problem with the precdence. If you ever have another operation on either the left or right side of the || operator, the || operator will bind tighter than the other operation. So for example, if you do: sure. i just don't want beginners to think: 'open(H,F) || die' is wrong. my $errors = 0; open (CRITICALSERVERS, $crout) || $errors += 2; that will translate to: my $errors = 0; ( open (CRITICALSERVERS, $crout) || $errors ) += 2; both of your examples above have syntax error so it's not *that* good of an example, perhaps you mean: [panda]# perl -e 'open A,f or die' Died at -e line 1. [panda]# instead of: [panda]# perl -e 'open A,f || die' [panda]# Hope this explains this suggestion that might seem, at first, to come out of the blue! it does. my main concern is not to confuse beginners who might think a certain construct should never be used. in this example, you sound like 'or' should always be used instead of '||' when checking whether an open success or not. david -- s$s*$+/tgmecJntgRtgjvqpCvuwL$;$;=qq$ \x24\x5f\x3d\x72\x65\x76\x65\x72\x73\x65 \x24\x5f\x3b\x73\x2f\x2e\x2f\x63\x68\x72 \x28\x6f\x72\x64\x28\x24\x26\x29\x2d\x32 \x29\x2f\x67\x65\x3b\x70\x72\x69\x6e\x74 \x22\x24\x5f\x5c\x6e\x22\x3b\x3b$;eval$; -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] http://learn.perl.org/ http://learn.perl.org/first-response
RE: using strict
Hello Derek, When using strict, the error message should point you to the line where the error is. It's usually pretty darn good at pointing the right line. In this case, I bet it's this one: while ( defined($line = CRITICALSERVERS) ) { There is no declaration of the $line variable. Try declaring it first, like this: my $line; while ( defined($line = CRITICALSERVERS) ) { I think that's the only variable that wasn't declared. If you get any more error messages, you can check them out and figure out what's wrong. Just a suggestion: You should use or instead of || when checking if a file opened correctly, because if you ever want to do some operations on the right side, or will bind less tightly and allow your operations to work correctly without needing parentheses to fix the precedence. By the way, why are you reading one line in the while's parentheses, and and then slurping the rest of the file into an array inside the while? That means your while will only be run once, because at the next iteration you'll already be at the end of the file... Just use $line inside the while, and you'll read one line at a time. You also chomped your line, which is good. Just remember to re-add the line ending (\n) when printing the lines as you're doing, or else all the lines will be printed on a single, very long line. I'll let the experts explain what soft references and barewords are. I've never used either, as I come from the relatively new school of programming. As I understand, they are features of the language that are dangerous to use in some contexts, so use strict disallows them knowing that if you really want to use them, you'll disable strict for the block of code where you use it, and then re-enable it. Hope this helps, Jean-Sébastien Guay -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] http://learn.perl.org/ http://learn.perl.org/first-response
Re: using strict
--As of Thursday, April 1, 2004 5:01 PM -0500, [EMAIL PROTECTED] is alleged to have said: what is a soft reference? what is a bareword? why is strict disallowing a compile here When I comment out strict the syntax checks outs as ok!??? how do I display each element # with its corresponding data line, b/c I only want certain elements printed out? I'll leave these to some Guru who knows what he is talking about, but I think I can say why strict is disallowing a compile... thank you! # Set pragma use strict; use warnings; #It just makes things easier... #Use always, or nearly. use diagnostics;#It answers questions... #Use until comfortable that you know #what it will tell you. tsm_critical_servers; # Declare sub sub tsm_critical_servers { my $crout=/tmp/critical_servers.out; # Make system call for data gathering system (dsmadmc -id=menu -password=xx 'q event * * begind=-1 begint=15:30 endd=today endtime=now' $crout); # Create array and read in each line as a seperate element open (CRITICALSERVERS, $crout) || die can't open file \n: $!; while ( defined($line = CRITICALSERVERS) ) { Right here. You've never told the compiler what $line is before. 'strict' doesn't like that, though it is technically legal Perl. Make that: my $line; while ( defined($line = CRITICALSERVERS) ) { chomp ($line); my @tsm = CRITICALSERVERS; foreach $_ (@tsm) { print $_; } } close (CRITICALSERVERS); } I think that will get you going. Daniel T. Staal --- This email copyright the author. Unless otherwise noted, you are expressly allowed to retransmit, quote, or otherwise use the contents for non-commercial purposes. This copyright will expire 5 years after the author's death, or in 30 years, whichever is longer, unless such a period is in excess of local copyright law. --- -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] http://learn.perl.org/ http://learn.perl.org/first-response
Re: using strict
On 4/1/2004 5:01 PM, [EMAIL PROTECTED] wrote: People of the Perl, from my understanding strict disallows soft references, ensures that all variables are declared before usage and disallows barewords except for subroutines. what is a soft reference? what is a bareword? why is strict disallowing a compile here When I comment out strict the syntax checks outs as ok!??? perldoc strict answers all of the above questions. Those below have already been answered. how do I display each element # with its corresponding data line, b/c I only want certain elements printed out? thank you! # Set pragma use strict; tsm_critical_servers; # Declare sub sub tsm_critical_servers { my $crout=/tmp/critical_servers.out; # Make system call for data gathering system (dsmadmc -id=menu -password=xx 'q event * * begind=-1 begint=15:30 endd=today endtime=now' $crout); # Create array and read in each line as a seperate element open (CRITICALSERVERS, $crout) || die can't open file \n: $!; while ( defined($line = CRITICALSERVERS) ) { chomp ($line); my @tsm = CRITICALSERVERS; foreach $_ (@tsm) { print $_; } } close (CRITICALSERVERS); } Derek B. Smith OhioHealth IT UNIX / TSM / EDM Teams -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] http://learn.perl.org/ http://learn.perl.org/first-response
Re: using strict
Does the following turn off strict for a vars? no strict vars; Could you also turn off strict for other things besides vars, refs and subs? Say for a subroutine (for example). Just curious. I've run into situations where I've come across badly maintained code and would like to do this for pieces instead of the whole deal. Thx On 4/1/2004 5:01 PM, [EMAIL PROTECTED] wrote: People of the Perl, from my understanding strict disallows soft references, ensures that all variables are declared before usage and disallows barewords except for subroutines. what is a soft reference? what is a bareword? why is strict disallowing a compile here When I comment out strict the syntax checks outs as ok!??? perldoc strict answers all of the above questions. Those below have already been answered. how do I display each element # with its corresponding data line, b/c I only want certain elements printed out? thank you! # Set pragma use strict; tsm_critical_servers; # Declare sub sub tsm_critical_servers { my $crout=/tmp/critical_servers.out; # Make system call for data gathering system (dsmadmc -id=menu -password=xx 'q event * * begind=-1 begint=15:30 endd=today endtime=now' $crout); # Create array and read in each line as a seperate element open (CRITICALSERVERS, $crout) || die can't open file \n: $!; while ( defined($line = CRITICALSERVERS) ) { chomp ($line); my @tsm = CRITICALSERVERS; foreach $_ (@tsm) { print $_; } } close (CRITICALSERVERS); } Derek B. Smith OhioHealth IT UNIX / TSM / EDM Teams -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] http://learn.perl.org/ http://learn.perl.org/first-response -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] http://learn.perl.org/ http://learn.perl.org/first-response
Re: using strict
In a message dated 4/1/2004 5:03:40 PM Eastern Standard Time, [EMAIL PROTECTED] writes: People of the Perl, from my understanding strict disallows soft references, ensures that all variables are declared before usage and disallows barewords except for subroutines. what is a soft reference? A soft reference is a symbolic reference. For example: $hello = Hey, what's up?; $var = hello; print $$var; #or, if it helps you see it better 'print ${$var}' This may be what you want, but may not be (usually you want hard references), hence use strict 'refs' what is a bareword? A bareword is...a bare...word? Really its just a word not expicitly denoted to be something. For example: sub foo { 1 } foo; foo is a barewords. In the above case, strict doesn't freak out because the subroutine is predeclared. But if you just throw around words whenever you want, it will freak out. For example, @array = (String, String, String, String); String is a bareword. strict will throw up an exception so fast. 2 exceptions to the bareword throw-up: left side of '=' and inside curly braces. For Example: %hash = ( left = 'right' ) print $hash{left}; why is strict disallowing a compile here When I comment out strict the syntax checks outs as ok!??? how do I display each element # with its corresponding data line, b/c I only want certain elements printed out? People already answered these. On an aside, Your code may compile dandy, that's not what strict is for. If perl could catch the errors like the ones you stated, there would be no need for strict. Strict is basically just so you don't screw up. You don't want barewords because you might think it means something else. You don't want soft references when you think you're using hard references. You don't want your code to screw up and you not to know why cause you used $var on 10,000 of the 20,000 lines and $Var on the other 10,000. -will (the above message is double rot13 encoded for security reasons) Most Useful Perl Modules -strict -warnings -Devel::DProf -Benchmark -B::Deparse -Data::Dumper -Clone (a Godsend) -Perl::Tidy -Beautifier
Re: using strict
[EMAIL PROTECTED] wrote: Does the following turn off strict for a vars? no strict vars; Could you also turn off strict for other things besides vars, refs and subs? Say for a subroutine (for example). Pragma are block/file scoped similar to a lexical. To answer your question below, yes you could turn off all or a subset of the strict pragma for a given block. Suggested reading: perldoc strict perldoc -f use perldoc perllexwarn Just curious. I've run into situations where I've come across badly maintained code and would like to do this for pieces instead of the whole deal. That is probably the best use of them, at least until you cleanup the code sufficiently that they can go back to being file scoped. http://danconia.org -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] http://learn.perl.org/ http://learn.perl.org/first-response
Re: Using strict and a configuration file?
just define all vars in ur conf file with a scope reslution eg $global::test = 'hello'; Octavian Rasnita wrote: Hi all, Is it possible to use use strict; if I get the variables from a configuration file? I've tried: use strict; require f:/xxx/config.txt; #In the configuration file I have a line like my $test = test test test; print Content-type: text/html\n\n; print $test; This gives me an error that I should define the variable $test. I don't want to define it in the script but in the configuration file. However, if I define it in the script with my $test; the script prints an empty string and doesn't take the variable from the configuration file. Is it possible to use strict, or it is necessary to use no strict;? Another problem, maybe bigger is that even though the script is running fine, it give me errors in the log file telling me that the variable $xxx and $yyy, ... is used only once. Is there a good method to define variables for more scripts in a single configuration file? I don't want to store my email address and other settings in each script because I may change that address, and then I will need to modify a lot. Thank you. or I have another solution for defining all the variables in a configuration file? Teddy, [EMAIL PROTECTED] -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]
RE: Using strict and configuration files
Bob, Exactly what does our do? I understand my and even local but have yet to grasp the our concept. -Original Message- From: Bob Showalter [mailto:[EMAIL PROTECTED]] Sent: Tuesday, June 11, 2002 9:12 AM To: 'Octavian Rasnita'; [EMAIL PROTECTED] Subject: RE: Using strict and configuration files -Original Message- From: Octavian Rasnita [mailto:[EMAIL PROTECTED]] Sent: Sunday, May 28, 2000 4:32 AM To: [EMAIL PROTECTED] Subject: Using strict and configuration files Hi all, I want to use: use strict; And I want to use a configuration file in a Perl script. The configuration file uses: %page1=( ); %page2=( ); This way I get errors if I run the script because the variables are not defined with my. I've tried putting in the configuration file: my %page1=( ); But this method doesn't work. I use a configuration file and I would like to put the settings only in this file without modifying the script. Is it possible? Yes. In your main program: use strict; require config.pl; our ($foo); print foo is $foo\n; In your config file: $foo = Hello, world; use strict applies only to the current file, so you don't need to change your config file. You need to add the our declaration to your main program to make use strict happy. A more formal way to handle this would be to use a module and import symbols: File MyConfig.pm: package MyConfig; use strict; require Exporter; our @ISA = qw(Exporter); our @EXPORT = qw($foo); our $foo = Hello, world; 1; Main program: use strict; use MyConfig; print foo is $foo\n; If you import a symbol with use, you don't need the our declaration. -- 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]
RE: Using strict and configuration files
-Original Message- From: Camilo Gonzalez [mailto:[EMAIL PROTECTED]] Sent: Tuesday, June 11, 2002 10:16 AM To: 'Bob Showalter'; 'Octavian Rasnita'; [EMAIL PROTECTED] Subject: RE: Using strict and configuration files Bob, Exactly what does our do? I understand my and even local but have yet to grasp the our concept. It just declares that a global variable will be used without a qualifying package name. Basically, it makes use strict happy. perldoc -f our -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]
RE: Using strict and configuration files
So the following are equivalent: use vars qw(foo) our $foo = -Original Message- From: Bob Showalter [mailto:[EMAIL PROTECTED]] Sent: Tuesday, June 11, 2002 9:17 AM To: 'Camilo Gonzalez'; [EMAIL PROTECTED] Subject: RE: Using strict and configuration files -Original Message- From: Camilo Gonzalez [mailto:[EMAIL PROTECTED]] Sent: Tuesday, June 11, 2002 10:16 AM To: 'Bob Showalter'; 'Octavian Rasnita'; [EMAIL PROTECTED] Subject: RE: Using strict and configuration files Bob, Exactly what does our do? I understand my and even local but have yet to grasp the our concept. It just declares that a global variable will be used without a qualifying package name. Basically, it makes use strict happy. perldoc -f our -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]
RE: Using strict and configuration files
-Original Message- From: Camilo Gonzalez [mailto:[EMAIL PROTECTED]] Sent: Tuesday, June 11, 2002 10:21 AM To: 'Bob Showalter'; Camilo Gonzalez; [EMAIL PROTECTED] Subject: RE: Using strict and configuration files So the following are equivalent: use vars qw(foo) our $foo = Pretty much. The latter has an initializer, which the first doesn't. The scoping rules are also slightly different. But yes, our is basically a replacement for use vars. -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]
Re: Using strict and a configuration file?
Teddy -- ...and then Octavian Rasnita said... % % Hi all, Hello! % % Is it possible to use use strict; if I get the variables from a % configuration file? % % I've tried: % % use strict; % require f:/xxx/config.txt; Have you tried use f:/xxx/config.txt; instead? From my reading of the camel book, require happens at run time and thus anything it defines won't be defined at compile time and strict will puke, whereas use essentially performs a require at compile time and then lets yuou import declarations into your own namespace, the latter part of which may also be useful (no put intended ;-) in light of some of the scoping concerns brought up in other replies. HTH HAND :-D -- David T-G * It's easier to fight for one's principles (play) [EMAIL PROTECTED] * than to live up to them. -- fortune cookie (work) [EMAIL PROTECTED] http://www.justpickone.org/davidtg/Shpx gur Pbzzhavpngvbaf Qrprapl Npg! msg05345/pgp0.pgp Description: PGP signature
RE: Using strict and a configuration file?
I had this same question answered on the perl beginners list. Object oriented programming was recommended. Someday I want to learn about that But for now what I did was give the configuration file and the scripts that use it the same package name and then I defined these variables as global Variables with the use vars. It works ok with the use strict, but it may Violate some standard perl coding practices against global variables. Maureen -Original Message- From: Octavian Rasnita [mailto:[EMAIL PROTECTED]] Sent: Friday, June 07, 2002 12:12 AM To: [EMAIL PROTECTED] Subject: Using strict and a configuration file? Hi all, Is it possible to use use strict; if I get the variables from a configuration file? I've tried: use strict; require f:/xxx/config.txt; #In the configuration file I have a line like my $test = test test test; print Content-type: text/html\n\n; print $test; This gives me an error that I should define the variable $test. I don't want to define it in the script but in the configuration file. However, if I define it in the script with my $test; the script prints an empty string and doesn't take the variable from the configuration file. Is it possible to use strict, or it is necessary to use no strict;? Another problem, maybe bigger is that even though the script is running fine, it give me errors in the log file telling me that the variable $xxx and $yyy, ... is used only once. Is there a good method to define variables for more scripts in a single configuration file? I don't want to store my email address and other settings in each script because I may change that address, and then I will need to modify a lot. Thank you. . or I have another solution for defining all the variables in a configuration file? Teddy, [EMAIL PROTECTED] -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]
Re: Using strict and configuration files
Hi, This is how I do it. #!/usr/bin/perl -wT use strict; my $conf; unless ($conf = do ('/path/to/config.pl')) { die (Could not open file); } print $conf-{'var1'}, \n; - Then in a file called config.pl { var1 = one, var2 = two } - The unless part is just to check that the file was opened successfully. The do actually opens the file and assigns the hash structure to $conf. Hope this helps! Carl Hi all, I want to use: use strict; And I want to use a configuration file in a Perl script. The configuration file uses: %page1=( ); %page2=( ); This way I get errors if I run the script because the variables are not defined with my. I've tried putting in the configuration file: my %page1=( ); But this method doesn't work. I use a configuration file and I would like to put the settings only in this file without modifying the script. Is it possible? Thanks. Teddy, [EMAIL PROTECTED] -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]
Re: Using strict with DBI
$sth = $dbh-prepare(select ID from maintenance); this should read: my($sth) = $dbh-prepare(select ID from maintenance); That didn't seem to work for me for some reason. Another question - how do I apply strict to the lines below. And how do I just avoid the private or my declaration by specifically declaring a variable as a global variable (using vars?). foreach $var (@email){ $var =~ /(^.*)\\@.*/; @names = (@names, $1 );} @names = map (uc($_), @names); Todd Wade [EMAIL PROTECTED] wrote: Rob Roudebush wrote in message [EMAIL PROTECTED]">news:[EMAIL PROTECTED]... I've seemed to have narrowed down the problem to $sth variable, I guess I can't declare this as a private variable because it prepares it and needs to have it at least local or global to access it? Rob Roudebush wrote: I think pulling the ID field may be causing the problem with use strict; ? $dbh = DBI-connect( DBI:mysql:turnover, root, , {RaiseError = 1, AutoCommit = 0}); $sth = $dbh-do( insert into maintenance (owner, email, maintype, title, requested, engineer, ticket, impact, comm . the do() method dosent return a statement handle. The transaction is complete after do() executes sucessfully. remove: ``$sth = '' from above, and then wrap $sth in my() in the line below, as in: $sth = $dbh-prepare(select ID from maintenance); this should read: my($sth) = $dbh-prepare(select ID from maintenance); $sth-execute(); while ($ref = $sth-fetchrow_hashref()){ $ID = $ref-{'ID'};} $dbh-disconnect; Mark Bergeron wrote: Rob, use strict; shouldn't really affect the syntax of any DBI handle or statement. ... When strict is giving you problems, its because you are doing something wrong, even though you swear you arent. Ive learned to trust perl before I trust my own code, its a good policy. Todd W. [EMAIL PROTECTED] -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] - Do You Yahoo!? LAUNCH - Your Yahoo! Music Experience
RE: using strict and -w
-Original Message- From: Robert Beau Link [mailto:[EMAIL PROTECTED]] Sent: Tuesday, April 30, 2002 8:27 PM To: [EMAIL PROTECTED] Subject: using strict and -w ... And where might I have found that answer within perldoc? I tried perldoc -q strict and got No documentation for perl FAQ keyword 'strict' found. All pointers appreciated. perldoc strict In general, if you see use foo, or no foo, the docs can be found with perldoc foo: use strict;perldoc strict no warnings 'undefined'; perldoc warnings use constant PI = 3.14; perldoc constanct Same for modules: use CGI qw(:standard); perldoc CGI use IO::Socket;perldoc IO::Socket -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]
Re: Using strict with DBI
Rob, use strict; shouldn't really affect the syntax of any DBI handle or statement. I would help if you included an example for us to have look at here. -Original Message- From: Rob Roudebush[EMAIL PROTECTED] To: [EMAIL PROTECTED] Date: Mon Apr 29 18:15:23 PDT 2002 Subject: Using strict with DBI For some reason the DBI part of my code doesn't work with strict - is this a limitation or am I doing something wrong - I declare my variables as my or private - my $sth = DBI-... Another question how can I test my script at the command line with -w if it is interactive? Or how do I push warnings to browser? -Rob - Do You Yahoo!? Yahoo! Health - your guide to health and wellness ___ GO.com Mail Get Your Free, Private E-mail at http://mail.go.com -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]
Re: Using strict with DBI
Rob Roudebush [EMAIL PROTECTED] wrote in message [EMAIL PROTECTED]">news:[EMAIL PROTECTED]... I've seemed to have narrowed down the problem to $sth variable, I guess I can't declare this as a private variable because it prepares it and needs to have it at least local or global to access it? Rob Roudebush [EMAIL PROTECTED] wrote: I think pulling the ID field may be causing the problem with use strict; ? $dbh = DBI-connect( DBI:mysql:turnover, root, , {RaiseError = 1, AutoCommit = 0}); $sth = $dbh-do( insert into maintenance (owner, email, maintype, title, requested, engineer, ticket, impact, comm . the do() method dosent return a statement handle. The transaction is complete after do() executes sucessfully. remove: ``$sth = '' from above, and then wrap $sth in my() in the line below, as in: $sth = $dbh-prepare(select ID from maintenance); this should read: my($sth) = $dbh-prepare(select ID from maintenance); $sth-execute(); while ($ref = $sth-fetchrow_hashref()){ $ID = $ref-{'ID'};} $dbh-disconnect; Mark Bergeron wrote: Rob, use strict; shouldn't really affect the syntax of any DBI handle or statement. ... When strict is giving you problems, its because you are doing something wrong, even though you swear you arent. Ive learned to trust perl before I trust my own code, its a good policy. Todd W. [EMAIL PROTECTED] -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]
Re: using strict and -w
On Tuesday, April 30, 2002, at 05:27 , Robert Beau Link wrote: [..] #!/usr/bin/perl -w use strict; print Number, please...; $alpha = STDIN; print Another, please...; $beta = STDIN; $sum= $alpha + $beta; $diff = $alpha - $beta; $product = $alpha * $beta; $quotient = $alpha / $beta; $remainder = $alpha % $beta; $exponent = $alpha ** $beta; #Show it print Sum: $sum\nDiference: $diff\nProduct: $product\n; print Quotient: $quotient\nRemainder: $remainder\nExponent: $exponent\n; Good point - another place where getting this into the online docs would be a good thing to get done { I always get bit trying to remember how to do the pragma 'no strict refs' } - the canonical source is the 3rd Edition of Programming Perl - the index of which lists this pragma as referenced ppp 15,56,137,263,871 { you will also find that ch 33 is devoted to the error messages. } there are actually a series of things that come with 'use strict' - one of which is guarding variables - as you notice - then there are how subs are used/pre-declared/ and the third section has to do with references - what is a legitimate way to reference or dereference memory items. the principle reason that I go with it is to make sure that folks can start avoiding some of the basic 'silly starter errors' that we all make - to our dying days - that are called out with strict and point us to where - that 'get passed over' when we do not use it and come back to bit us later... In your case your code above: plan a: each time you declare a new variable in a scope {and yes you want to do this to know scoping} make sure that you prefix them with my $var = thingWeDoWithIt; or use a local() for some previously defined variable that needs to be isolated in this scope... plan b: everytime you throw in a new variable just start up with my $varNameHere - plan c: 'pascal it' and have a section where you predefine them: use vars qw/allYourVariablesBeMine/; plan d: 'pascal it' and have a section where you do things like my ($var, $var2.) = (theInitsHere); cf: http://perl.plover.com/FAQs/Namespaces.html ciao drieux --- -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]
RE: Using strict and getting return values
-Original Message- From: Dermot Paikkos [mailto:[EMAIL PROTECTED]] Subject: Using strict and getting return values Hi Gurus, Well, I'm definitely not a guru, but I think I might be able to help... :) I am trying to get tidy with my scripts and want to use Strict but am having difficulty with return values from subroutines. I have the following snippet: while (defined(my $i =$fh)) { chomp($i); my @a = split(/|/,$i); my $last = $a[1]; my $first = $a[0]; if ( $name =~ /$last/i ) { return($first,$last); } and get the error: Global symbol $last requires explicit package name at /var/www/perl/reply.pl line 16. Where is line 16? Can you post the whole code? How did you call the sub before getting the error? Without knowing that, I'm going to make an assumption that you did not assign values to the return from the sub. When I later: print Hello $first $last\n; I know it is out of the scope but I thought I could pass the results of a sub to main. What am I doing wrong?? Dp. Assuming the name of your subroutine is SomeSub, the piece of your code that calls the sub should look something like: my ($FName, $LName) = SomeSub(); Then you can do: print Hello $FName $LName\n; Hope this helps Jason CONFIDENTIALITY NOTICE: The information contained in this ELECTRONIC MAIL transmission is confidential. It may also be privileged work product or proprietary information. This information is intended for the exclusive use of the addressee(s). If you are not the intended recipient, you are hereby notified that any use, disclosure, dissemination, distribution [other than to the addressee(s)], copying or taking of any action because of this information is strictly prohibited.
Re: using Strict with filehandles
Deborah Strickland wrote: Hi, I can't find the answer to this in any of my many Perl books so I'm asking you. I want to do 'use strict' but when I also use it with a file handle I get an error. How can I declare a file handle variable such that 'use strict' won't generate an error? This fails. use strict; open (FN, $someFile); This works: open(FN, $someFile); In both cases, FN is not defined. When I try to define it with 'my $FN' I still get an error with strict. What's the proper way to define this? This should pass. Even with use strict, you don't need to declare a file handle. Please tell us, what's the content of $someFile (and when you do, show us the rest of code, too) ! Is it really something like 'my_file.txt'. Greetings, Andrea -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]
RE: using strict
Fortunately or unfortunately, you can't print to files just by using $file as a filehandle. You need to open the file first. CODE foreach $k (sort keys (%all_genes)) { for (1..5){ if ($k =~ /[$_]g/){ $file = CHR$_; open(FILE, $file) or die Couldn't open '$file': $!; stopped; print FILE $k\t$all_genes{$k}\n; print $k \t $all_genes{$k}\n; close(FILE); last; } /CODE That's if you want your files to be CHR1, CHR2, etc... -Original Message- From: Hans Holtan [mailto:[EMAIL PROTECTED]] Sent: Wednesday, June 27, 2001 4:36 PM To: [EMAIL PROTECTED] Subject: using strict Hi everyone, I get this error when I run my script, and I don't understand why: Can't use string (CHR3) as a symbol ref while strict refs in use at AtIntergenicTableSort.pl line 53. I'm trying to split my output into 5 seperate files based on what is in the key. Here is a snip of code: #Sort the hash and write to a new file foreach $k (sort keys (%all_genes)) { for (1..5){ if ($k =~ /[$_]g/){ $file = CHR$_; print $file $k\t$all_genes{$k}\n; print $k \t $all_genes{$k}\n; last; }
RE: using strict
--- Stephen Nelson [EMAIL PROTECTED] wrote: Fortunately or unfortunately, you can't print to files just by using $file as a filehandle. You need to open the file first. CODE foreach $k (sort keys (%all_genes)) { for (1..5){ if ($k =~ /[$_]g/){ $file = CHR$_; open(FILE, $file) or die Couldn't open '$file': $!; stopped; print FILE $k\t$all_genes{$k}\n; print $k \t $all_genes{$k}\n; close(FILE); last; } /CODE That's if you want your files to be CHR1, CHR2, etc... Another trick -- use FileHandle and put them into an array. my @FILE; for (1..5) { $FILE[$_] = new FileHandle CHR$_ or die CHR$_: $!; } now you can say: print $FILE[$_] $k\t$all_genes{$k}\n; -Original Message- From: Hans Holtan [mailto:[EMAIL PROTECTED]] Sent: Wednesday, June 27, 2001 4:36 PM To: [EMAIL PROTECTED] Subject: using strict Hi everyone, I get this error when I run my script, and I don't understand why: Can't use string (CHR3) as a symbol ref while strict refs in use at AtIntergenicTableSort.pl line 53. I'm trying to split my output into 5 seperate files based on what is in the key. Here is a snip of code: #Sort the hash and write to a new file foreach $k (sort keys (%all_genes)) { for (1..5){ if ($k =~ /[$_]g/){ $file = CHR$_; print $file $k\t$all_genes{$k}\n; print $k \t $all_genes{$k}\n; last; } = print Just another Perl Hacker\n; # edited for readability =o) = Real friends are those whom, when you inconvenience them, are bothered less by it than you are. -- me. =o) = There are trivial truths and there are great Truths. The opposite of a trival truth is obviously false. The opposite of a great Truth is also true. -- Neils Bohr __ Do You Yahoo!? Get personalized email addresses from Yahoo! Mail http://personal.mail.yahoo.com/
Re: using strict
On Wed, Jun 27, 2001 at 04:54:17PM -0700, Paul wrote: Another trick -- use FileHandle and put them into an array. my @FILE; for (1..5) { $FILE[$_] = new FileHandle CHR$_ or die CHR$_: $!; } now you can say: print $FILE[$_] $k\t$all_genes{$k}\n; This doesn't actually work, for obscure reasons having to do with parsing and such. It's documented thusly in perldoc -f print: Note that if you're storing FILEHANDLES in an array or other expression, you will have to use a block returning its value instead: print { $files[$i] } stuff\n; print { $OK ? STDOUT : STDERR } stuff\n; In other words, print can't handle anything other than a simple scalar value as its filehandle argument, unless you use the above block form. Michael -- Administrator www.shoebox.net Programmer, System Administrator www.gallanttech.com --
Re: using strict - ADDENDUM
On Wed, 27 Jun 2001, Hans Holtan wrote: I forgot to mention that I did open the output files earlier in the program, ie CHR1, CHR3 ... CHR5. But you can't say $file = CHR1, beacuse then you are creating a symbolic reference to a filehandle. Very bad. Do this instead: $file = *CHR1; Then you can properly use your filehandle. What you really want to do is create an array of open filehandles (going by the code you had) and then do: print $file[$_] stuff; http://www.chapelperilous.net/btfwk/ management, n.: The art of getting other people to do all the work.
Re: using strict - ADDENDUM
On Wed, 27 Jun 2001, Brett W. McCoy wrote: But you can't say $file = CHR1, beacuse then you are creating a symbolic reference to a filehandle. Very bad. Do this instead: $file = *CHR1; Then you can properly use your filehandle. What you really want to do is create an array of open filehandles (going by the code you had) and then do: print $file[$_] stuff; Oops, I'm thinking ahead of myself. You will need to enclose the filehandle in a block if you use an array: print { $file[$_] } stuff; or you can store the filehandle in a variable: my $fh = $file[$_]; print $fh stuff; -- Brett http://www.chapelperilous.net/btfwk/ He who has the courage to laugh is almost as much a master of the world as he who is ready to die. -- Giacomo Leopardi