Jump to content

Clarifications for episode 22:


G+_Lee Crocker
 Share

Recommended Posts

Clarifications for episode 22:

====

 

Padre likes to point out that in both Python and Perl, you don't have to pre-declare the types of your variables. That's true, but it's important to understand that in both languages, values still do have types, and the languages accomplish that flexibility in very different ways that are important to understand.

 

In both Python and Perl, for example, when you ask a user for input, you get a string. If the user types "21", for example, into this Python code:

 

    first = raw_input()

 

The variable "first" will contain the string "21". Likewise, the Perl program:

 

    first = ;

 

Will result in "first" containing the string "21\n".

 

In our Python code, we then converted that string to an integer with int() before we did comparisons. We didn't do this in Perl. Why?

 

If we had not converted the strings to integers in Python, it would have told us that, for example, "21" is less than "5", because alphabetically the string "21" comes first. But because we converted the strings to integers, the "<" operation worked fine.

 

In Perl, we actually do the integer conversion implicitly when we compare. The Perl code

 

    if (first < second) {

 

Sees the "<" operator, and assumes that you want numeric comparisons, and will therefore tell you that "21" > "5" (though variables themselves still contain strings). In Perl, you have to use a whole different set of operators to compare strings as strings. You have to use "eq", "lt", "gt", etc.

 

Regular expressions

====

 

Perl was indeed the language that put regular expressions on the map. The idea already existed in tools like sed and awk, but Larry took them to a new level. Padre implied that Python didn't have the same capabilities. In fact Python, JavaScript, and most modern languages all have the ability to use regular expressions the same way Perl does--and in fact they generally all use Perl's expression syntax as well. In Python, you just have to spell it out:

 

    if re.search("bunny", input1):

 

Is the equivalent of Perl's

 

   if (input1 =~ /bunny/) {

 

Python's just more readable by humans. :-)

 

String interpolation

====

 

Perl is more focused on string manipulation than many languages, which is why it does what's called "string interpolation"--that's what Padre pointed to when showing that you can put variable names inside strings directly. That's one of the reasons for marking variables with special characters like $ and @. But a couple of things should be pointed out. First, this happens in double-quoted strings (and other places), but not single-quoted strings:

 

    $val = 5

    print "val = $val";

    print 'val = $val';

 

Will print

 

    val = 5

    val = $val

 

So you need to know when it does and when it doesn't. Also, realize that this is happening at runtime, so the string actually contains the characters "val = $val", and those last four characters are replaced with the current value of the variable by the code that executes the "print" command.

 

Other languages generally have string-formatting functions that you must call to do these variable substitutions. For example, in Python, you would use:

 

    print("val = {}".format(val));

 

and in C, you would use:

 

    printf("val = %d", val);

 

Note that in C, you have to use "%d" to tell the print command what type you want the variable printed as, while Perl and Python make some reasonable assumptions for you. The Go language, oddly, while being a low-level strongly-typed language like C, can also figure it out for you most of the time:

 

    fmt.Printf("val = %v", val)

 

will work whether val is a string, integer, or other (even complex) type.

Link to comment
Share on other sites

You made some great points Lee, and and excellent explanation of jumble between strings and numeric types in Perl.

 

As a side note, I suspect that most languages that support regular expressions internally employ the PCRE lib to get the job done, and was surprised to hear indications it was not available on Python (that just did not sound correct at all).

 

Thanks for your input. Great post!

 

Joe C. Hecht - Code4Sale, LLC

Link to comment
Share on other sites

Lee Crocker I took a look over at Python.org and noticed that the PCRE module was deprecated in 2.3.6.0, however, clicking the release notes link results in a 404-Page not found error.

 

I know that there are a lot of regular expression engines available, and they are most always benched against the PCRE lib (I believe to considered to pretty much be the industry standard).

 

Might you have any idea why they deprecated PCRE and switched?

 

I know the last couple of releases caused some minor hiccups around here, but we submitted bug reports and Phillip did resolve the issues.

 

TJoe

Link to comment
Share on other sites

 Share

×
×
  • Create New...