On Friday 08 October 2010 01:41:01 John W. Krahn wrote: > Shlomi Fish wrote: > > Hi all, > > Hello,
Hi John, > > > after being tired of telling Perl newcomers about the same problems with > > their code times and times again, I've decided to create this page > > detailing "Perl Elements to avoid": > > > > http://perl-begin.org/tutorials/bad-elements/ > > <QUOTE> > the ultimately wrong, insecure and/or outdated styles are: > > # Bareword filehandle (type glob), two arguments open (insecure) and no > # error handling > open INPUT, "<$filename"; > </QUOTE> > > You say "outdated styles" implying more than one example but you don't > show the other style i.e.: > > use vars qw/ $INPUT /; > our $INPUT = $filename; > open INPUT; You don't need both use vars and our for the same variable. > > You say "wrong" and "insecure" which is incorrect if the file name is > prefixed and postfixed correctly, as described in the documentation. > > $filename = "./$filename\0"; > OK, but many people will avoid doing that (and I was not aware of it now), and furthermore "./" (or "/") may not be very portable, and the three-args open is generally much preferable. Better be safe than sorry. > > <QUOTE> > Make sure you never use bareword here documents <<EOF which are valid > syntax, but people are never sure whether they are <<"EOF" or <<`EOF`. > </QUOTE> > > I am a "people" and I am very sure that <<EOF is exactly the same as > <<"EOF". OK, I've changed "people" to "many people". > And EOF is usually short for End-Of-File so why use it for a > string delimiter? It's just that EOF is customary for that (presumable back to its root from shell). See: http://www.google.com/codesearch?as_q=%3C%3CEOF&btnG=Search+Code&hl=en&as_package=&as_lang=&as_filename=&as_class=&as_function=&as_license=&as_case= (short URL - http://xrl.us/bh33nx ). Don't take it too literarly. > > > <QUOTE> > Slurping a file (i.e: Reading it all into memory) > </QUOTE> > > You are missing some other examples: > > read $fh, my $contents, -s $fh; > > sysread $fh, my $contents, -s $fh; I didn't see these examples in the wild a lot, but I'll add them. > > > <QUOTE> > sub my_func > { > my ($prefix, $array_ref) = @_; > > foreach my $item (@{$array_ref}) > { > print $prefix, ": ", $item, "\n"; > } > > return; > } > </QUOTE> > > Why use a loop to print a list? > > sub my_func > { > my ($prefix, $array_ref) = @_; > > print $prefix, join( ": ", @$array_ref ), "\n"; > > return; > } > Your rewrite of my example has a few bugs - it will print all the items in @$array_ref on one big line joined by ": " instead of in separate lines, and would include the prefix only once. You can do something a bit better using perldoc -f map , but a loop is good enough. In any case, based on your input, I changed it to something less trivial: <QUOTE> sub calc_polynomial { my ($x, $coefficients) = @_; my $x_power = 1; my $result = 0; foreach my $coeff (@{$coefficients}) { $result += $coeff * $x_power; } continue { $x_power *= $x; } return $result; } print "(4*x^2 + 2x + 1)(x = 5) = ", calc_polynomial(5, [1, 2, 4]); </QUOTE> > > <QUOTE> > An arbitrary C-style loop can be replaced with a while loop with a > continue block. > </QUOTE> > > For example? > Well, I couldn't think of anything, but I guess I can grep my code for such examples and include that or a similar one. Thanks for your input! Regards, Shlomi Fish -- ----------------------------------------------------------------- Shlomi Fish http://www.shlomifish.org/ Freecell Solver - http://fc-solve.berlios.de/ <rindolf> She's a hot chick. But she smokes. <go|dfish> She can smoke as long as she's smokin'. Please reply to list if it's a mailing list post - http://shlom.in/reply . -- To unsubscribe, e-mail: beginners-unsubscr...@perl.org For additional commands, e-mail: beginners-h...@perl.org http://learn.perl.org/