in reply to Wrong idioms
Sometimes I get the feeling that simply the look and feel of the code is the reason to do so.
Or maybe just the effort to write the code in the first place. Or missing awareness.
There are many more similar but less obvious cases. For example code that interpolates an array into a double-quoted string depends on the value of $". Code that uses print implicitly uses $\ and $,, and if you use readline (or <$fh>) or chomp you'd better hope nobody messed up your $.
And good thing that $[ is deprecated.
And your seemingly harmless loop while (<>) { } gets an entirely new meaning if $^I is set.
Speaking of <>, did you know that it executes code for you?
$ perl -we 'while (<>) { print }' 'fortune |' To downgrade the human mind is bad theology. -- C. K. Chesterton
So you see there is a whole scale of things you should consider when you want to write truely robust and correct code. But once you start to write your code as
sub do_something { my ($filename, @rest) = @_; return unless defined $filename; # ensure basic sanity: local $/ = "\n"; local $, = ''; local $\ = ''; local $" = ' '; local $@; # we might want to use eval { } }
you get a lot of boilerplate code, and writing Perl stops to be fun.
So you can either leave Perl 5 for good, or decide where on your scale between fun and correctness your code should be. Don't condemn anybody for using a simple truth check instead of defined, but not for failing to reset all used global variables in each routine that could be called from the outside.
Note that Perl 6 drastically reduces the number of global and special variables, and most built-in functions don't depend on them to the same degree as in Perl 5.
|
---|
Replies are listed 'Best First'. | |
---|---|
Re^2: Wrong idioms
by McA (Priest) on Mar 29, 2013 at 21:35 UTC | |
Re^2: Wrong idioms
by vsespb (Chaplain) on Mar 29, 2013 at 23:58 UTC | |
by moritz (Cardinal) on Mar 30, 2013 at 08:41 UTC | |
by McA (Priest) on Mar 30, 2013 at 10:02 UTC | |
by vsespb (Chaplain) on Mar 30, 2013 at 09:36 UTC | |
Re^2: Wrong idioms
by Jenda (Abbot) on Apr 01, 2013 at 10:42 UTC | |
by moritz (Cardinal) on Apr 01, 2013 at 13:14 UTC | |
by Jenda (Abbot) on Apr 01, 2013 at 18:39 UTC | |
by moritz (Cardinal) on Apr 01, 2013 at 19:21 UTC |