I am learning perl and python... at the same time, not my by design but it has to be done.
Question:
In a perl script I use(see below) at the head of my txt.
As other users have posted, Python has no strict pragma. And this, in my opinion is one of its biggest deficiencies. Moreover, it is one of the reasons that, for serious programming projects, I still use Perl.
There will no doubt be Python devotees that take umbrage with this statement. I have heard some say that they don't need strict. I find that those that say this typically don't know what strict buys you. Consider the following code block in Python:
def Main():
print(GetPrice(100,"Alaska"))
print(GetPrice(100,"Florida"))
print(GetPrice(100,"Michigan"))
print(GetPrice(100,"Wisconsin"))
def GetPrice(UnitPrice,State):
StateSalesTaxRate = 0
if State == "Alabama": StateSalesTaxRate = 0.04
if State == "Alaska": StateSalesTaxRate = 0
if State == "Arizona": StateSalesTaxRate = 0.056
if State == "Arkansas": StateSalesTaxRate = 0.065
if State == "California": StateSalesTaxRate = 0.075
if State == "Colorado": StateSalesTaxRate = 0.029
if State == "Connecticut": StateSalesTaxRate = 0.0635
if State == "Delaware": StateSalesTaxRate = 0
if State == "Florida": StateSalesTaxRate = 0.06
if State == "Georgia": StateSalesTaxRate = 0.04
if State == "Guam": StateSalesTaxRate = 0.04
if State == "Hawaii": StateSalesTaxRate = 0.04
if State == "Idaho": StateSalesTaxRate = 0.06
if State == "Illinois": StateSalesTaxRate = 0.0625
if State == "Indiana": StateSalesTaxRate = 0.07
if State == "Iowa": StateSalesTaxRate = 0.06
if State == "Kansas": StateSalesTaxRate = 0.0615
if State == "Kentucky": StateSalesTaxRate = 0.06
if State == "Louisiana": StateSalesTaxRate = 0.04
if State == "Maine": StateSalesTaxRate = 0.055
if State == "Maryland": StateSalesTaxRate = 0.06
if State == "Massachusetts": StateSalesTaxRate = 0.0625
if State == "Michigan": StateSalesTexRate = 0.06
if State == "Minnesota": StateSalesTaxRate = 0.06875
if State == "Mississippi": StateSalesTaxRate = 0.07
if State == "Missouri": StateSalesTaxRate = 0.04225
if State == "Montana": StateSalesTaxRate = 0
if State == "Nebraska": StateSalesTaxRate = 0.055
if State == "Nevada": StateSalesTaxRate = 0.0685
if State == "New Hampshire": StateSalesTaxRate = 0
if State == "New Jersey": StateSalesTaxRate = 0.07
if State == "New Mexico": StateSalesTaxRate = 0.05125
if State == "New York": StateSalesTaxRate = 0.04
if State == "North Carolina": StateSalesTaxRate = 0.0475
if State == "North Dakota": StateSalesTaxRate = 0.05
if State == "Ohio": StateSalesTaxRate = 0.0575
if State == "Oklahoma": StateSalesTaxRate = 0.045
if State == "Oregon": StateSalesTaxRate = 0
if State == "Pennsylvania": StateSalesTaxRate = 0.06
if State == "Puerto Rico": StateSalesTaxRate = 0.105
if State == "Rhode Island": StateSalesTaxRate = 0.07
if State == "South Carolina": StateSalesTaxRate = 0.06
if State == "South Dakota": StateSalesTaxRate = 0.04
if State == "Tennessee": StateSalesTaxRate = 0.07
if State == "Texas": StateSalesTaxRate = 0.0625
if State == "Utah": StateSalesTaxRate = 0.0595
if State == "Vermont": StateSalesTaxRate = 0.06
if State == "Virginia": StateSalesTaxRate = 0.053
if State == "Washington": StateSalesTaxRate = 0.065
if State == "West Virginia": StateSalesTaxRate = 0.06
if State == "Wisconsin": StateSalesTaxRate = 0.05
if State == "Wyoming": StateSalesTaxRate = 0.04
return(UnitPrice*(1+StateSalesTaxRate))
if __name__ == '__main__': Main()
This code computes the cost for purchases including sales tax. Granted there are more efficient ways to do this, but it is only an illustration.
So, do you see anything wrong with the code? No? Try running it. When you do you get:
100
106.0
100
105.0
Still don't see an problem? Then you've got a bigger problem than you know. Here is the equivalent code rendered in Perl:
use strict;
sub Main
{
print GetPrice(100,"Alaska"), "\n";
print GetPrice(100,"Florida"), "\n";
print GetPrice(100,"Michigan"), "\n";
print GetPrice(100,"Wisconsin"), "\n";
}
sub GetPrice
{
my($UnitPrice,$State) = @_;
my $StateSalesTaxRate = 0;
$StateSalesTaxRate = 0.04 if $State eq "Alabama";
$StateSalesTaxRate = 0 if $State eq "Alaska";
$StateSalesTaxRate = 0.056 if $State eq "Arizona";
$StateSalesTaxRate = 0.065 if $State eq "Arkansas";
$StateSalesTaxRate = 0.075 if $State eq "California";
$StateSalesTaxRate = 0.029 if $State eq "Colorado";
$StateSalesTaxRate = 0.0635 if $State eq "Connecticut";
$StateSalesTaxRate = 0 if $State eq "Delaware";
$StateSalesTaxRate = 0.06 if $State eq "Florida";
$StateSalesTaxRate = 0.04 if $State eq "Georgia";
$StateSalesTaxRate = 0.04 if $State eq "Guam";
$StateSalesTaxRate = 0.04 if $State eq "Hawaii";
$StateSalesTaxRate = 0.06 if $State eq "Idaho";
$StateSalesTaxRate = 0.0625 if $State eq "Illinois";
$StateSalesTaxRate = 0.07 if $State eq "Indiana";
$StateSalesTaxRate = 0.06 if $State eq "Iowa";
$StateSalesTaxRate = 0.0615 if $State eq "Kansas";
$StateSalesTaxRate = 0.06 if $State eq "Kentucky";
$StateSalesTaxRate = 0.04 if $State eq "Louisiana";
$StateSalesTaxRate = 0.055 if $State eq "Maine";
$StateSalesTaxRate = 0.06 if $State eq "Maryland";
$StateSalesTaxRate = 0.0625 if $State eq "Massachusetts";
$StateSalesTexRate = 0.06 if $State eq "Michigan";
$StateSalesTaxRate = 0.06875 if $State eq "Minnesota";
$StateSalesTaxRate = 0.07 if $State eq "Mississippi";
$StateSalesTaxRate = 0.04225 if $State eq "Missouri";
$StateSalesTaxRate = 0 if $State eq "Montana";
$StateSalesTaxRate = 0.055 if $State eq "Nebraska";
$StateSalesTaxRate = 0.0685 if $State eq "Nevada";
$StateSalesTaxRate = 0 if $State eq "New Hampshire";
$StateSalesTaxRate = 0.07 if $State eq "New Jersey";
$StateSalesTaxRate = 0.05125 if $State eq "New Mexico";
$StateSalesTaxRate = 0.04 if $State eq "New York";
$StateSalesTaxRate = 0.0475 if $State eq "North Carolina";
$StateSalesTaxRate = 0.05 if $State eq "North Dakota";
$StateSalesTaxRate = 0.0575 if $State eq "Ohio";
$StateSalesTaxRate = 0.045 if $State eq "Oklahoma";
$StateSalesTaxRate = 0 if $State eq "Oregon";
$StateSalesTaxRate = 0.06 if $State eq "Pennsylvania";
$StateSalesTaxRate = 0.105 if $State eq "Puerto Rico";
$StateSalesTaxRate = 0.07 if $State eq "Rhode Island";
$StateSalesTaxRate = 0.06 if $State eq "South Carolina";
$StateSalesTaxRate = 0.04 if $State eq "South Dakota";
$StateSalesTaxRate = 0.07 if $State eq "Tennessee";
$StateSalesTaxRate = 0.0625 if $State eq "Texas";
$StateSalesTaxRate = 0.0595 if $State eq "Utah";
$StateSalesTaxRate = 0.06 if $State eq "Vermont";
$StateSalesTaxRate = 0.053 if $State eq "Virginia";
$StateSalesTaxRate = 0.065 if $State eq "Washington";
$StateSalesTaxRate = 0.06 if $State eq "West Virginia";
$StateSalesTaxRate = 0.05 if $State eq "Wisconsin";
$StateSalesTaxRate = 0.04 if $State eq "Wyoming";
return($UnitPrice*(1+$StateSalesTaxRate));
}
Main();
Without Perl's strict pragma enabled, you even get the identical output:
100
106.0
100
105.0
But with strict turned on, you get the following error message when you run this Perl script:
Global symbol "$StateSalesTexRate" requires explicit package name at line 37.
Execution aborted due to compilation errors.
The problem in both examples is that there is a typo in one of the computation lines. I have "StateSalesTexRate" instead of "StateSalesTaxRate" for the line computing sales tax for the state of Michigan. Perl finds and squashes this bug explicitly. Meanwhile, Python turns its head and looks the other way.
This is a big deal. Imagine this software is being used by your online business to calculate how much you charge a customer's credit card. How long will it take before you realize Michigan customers are getting a pass on sales tax? When you do, do you go back to the customer and say "Sorry, we need more money from you" or do you eat the loss yourself?
Of course any company using this type of coding algorithm to compute sales tax probably has bigger problems. But you can clearly see by this example what the strict pragma in Perl does and why I and others believe it should be an essential feature of any scripting language.
There are lots of things that I really like about Python. I get why some people prefer Python to Perl. But there are a few things that I truly detest about Python. This is one.