[Tutor] Feature or ... after python 2.7

Alan Gauld alan.gauld at yahoo.co.uk
Wed Jan 25 19:12:02 EST 2017


On 25/01/17 17:32, source liu wrote:

>>> $cat test.py
>>> #!/bin/env python
>>> print "I am python"
>>>
>>> $perl test.py
>>> I am python
>>>
>>
>> That is just because this particular print statement
>> is compatible with Perl. If you had tried anything
>> more complex then it would have bombed.
> 
> How to you define complex?  The script import lots of modules such as
> urllib2

The script above doesn't, it just has a single print line.
That's why Perl is able to run it. If it had any import
statements Perl would complain.

When you type

perl filename

The perl interpreter tries to run the file regardless
of its extension or the shebang hint in the first line.
If it looks like valid perl (which the print is) then
it will execute it.

Similarly when you run

python filename

Python will do the same, the interpreter doesn't care
what the file is called or what the shebang line says
it just tries to run the text in the file.

The file extension and/or shebang line only matter
to the OS and its shell. (and even then only in specific
cases)

Now where urlib2 comes into the picture I have no
idea because you didn't mention that previously. But
I'm pretty sure both Python 2.7 and 2.6 will know
what to do with it whereas perl will not but it will
try to execute its own import method (which fails
silently...). But it will almost certainly fail
when you try to use the features of the imported
modules.

-- 
Alan G
Author of the Learn to Program web site
http://www.alan-g.me.uk/
http://www.amazon.com/author/alan_gauld
Follow my photo-blog on Flickr at:
http://www.flickr.com/photos/alangauldphotos




More information about the Tutor mailing list