inquiry to Perl monks about Perl as a first programming language
ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
For example, encapsulation and abstraction can be achieved in functional programming - if you are interested, I can publish some examples. And the examples will most likely show the difference between Perl and Python - according to my limited Python knowledge you won't be able to implement the same concepts in Python.
there are many discussions about Python encapsulation
and functional programming in the newsgroup.
also, if you really want to know how your examples can be implemented in Python, make a post to the newsgroup. The experts there can help you. Otherwise, i don't really think you should judge something based on limited knowledge.
there are many discussions about Python encapsulation
and functional programming in the newsgroup.
also, if you really want to know how your examples can be implemented in Python, make a post to the newsgroup. The experts there can help you. Otherwise, i don't really think you should judge something based on limited knowledge.
I can post my examples here, and you as an expert will tell me whether they can be implemented in Python.
...
AFAIR, Python does not fully support lexical scoping (according to Wiki article), and my examples are based on lexical scoping.
Last edited by Sergei Steshenko; 03-20-2009 at 02:02 AM.
I can post my examples here, and you as an expert will tell me whether it can be implemented in Python.
i am not an expert in the area of OO, functional programming, lexical scoping, that's why i suggest to you if you want to know more or you want to find Python equivalent of those examples you have, go to the Python newsgroup. There are lots of experts there who can answer your questions. Until it is proven that it can't be implemented, no point in judging anything.
i am not an expert in the area of OO, functional programming, lexical scoping, that's why i suggest to you if you want to know more or you want to find Python equivalent of those examples you have, go to the Python newsgroup. There are lots of experts there who can answer your questions. Until it is proven that it can't be implemented, no point in judging anything.
The only thing that needs to be verified is that Python doesn't fully support lexical scoping - it it is so, my examples can't be implemented in Python.
And Python hasn't fully supported lexical scoping from the very beginning.
The only thing that needs to be verified is that Python doesn't fully support lexical scoping - it it is so, my examples can't be implemented in Python.
post 1 example then. Not too long, but sufficient to make you believe it can't be implemented.
post 1 example then. Not too long, but sufficient to make you believe it can't be implemented.
Here it is - it's just the beginning, I also intend later to show code reuse and inheritance - kind of.
The point if this example is scoping + closures + no need to thik about variable names because lexical scoping encapsulates the variables:
Code:
sergei@amdam2:~/junk> cat -n ./points_setters_getters.pl
1 #!/usr/bin/perl -w
2
3 use strict;
4
5
6 my $setter_1d_sub;
7 my $getter_1d_sub;
8
9 # 1d point:
10 {
11 my $x; # private data which is invisible in outer scopes
12 my $setter_sub = sub{$x = shift};
13 my $getter_sub = sub{$x};
14
15 # "connection" with the external world:
16
17 $setter_1d_sub = $setter_sub;
18 $getter_1d_sub = $getter_sub;
19 }
20
21
22 my $setter_2d_sub;
23 my $getter_2d_sub;
24
25 # 2d point:
26 {
27 my ($x, $y); # private data which is invisible in outer scopes
28
29 my $setter_sub = sub{$x = shift; $y = shift};
30 my $getter_sub = sub{($x, $y)};
31
32 # "connection" with the external world:
33
34 $setter_2d_sub = $setter_sub;
35 $getter_2d_sub = $getter_sub;
36 }
37
38 &{$setter_1d_sub}(1);
39 &{$setter_2d_sub}(2, 3);
40
41 warn "1d point: ", &{$getter_1d_sub}(), " 2d point: ", join(' ', &{$getter_2d_sub}());
sergei@amdam2:~/junk> ./points_setters_getters.pl
1d point: 1 2d point: 2 3 at ./points_setter_getters.pl line 41.
Also, I do not functionally need to introduce internal $setter_sub, $getter_sub and then "connect" them - I could have directly assigned values to $setter_1d_sub, $getter_1d_sub, $setter_2d_sub, $getter_2d_sub inside the lexical scopes, but I have some more things on my mind to be shown later.
Here it is - it's just the beginning, I also intend later to show code reuse and inheritance - kind of.
The point if this example is scoping + closures + no need to thik about variable names because lexical scoping encapsulates the variables:
Code:
sergei@amdam2:~/junk> cat -n ./points_setters_getters.pl
1 #!/usr/bin/perl -w
2
3 use strict;
4
5
6 my $setter_1d_sub;
7 my $getter_1d_sub;
8
9 # 1d point:
10 {
11 my $x; # private data which is invisible in outer scopes
12 my $setter_sub = sub{$x = shift};
13 my $getter_sub = sub{$x};
14
15 # "connection" with the external world:
16
17 $setter_1d_sub = $setter_sub;
18 $getter_1d_sub = $getter_sub;
19 }
20
21
22 my $setter_2d_sub;
23 my $getter_2d_sub;
24
25 # 2d point:
26 {
27 my ($x, $y); # private data which is invisible in outer scopes
28
29 my $setter_sub = sub{$x = shift; $y = shift};
30 my $getter_sub = sub{($x, $y)};
31
32 # "connection" with the external world:
33
34 $setter_2d_sub = $setter_sub;
35 $getter_2d_sub = $getter_sub;
36 }
37
38 &{$setter_1d_sub}(1);
39 &{$setter_2d_sub}(2, 3);
40
41 warn "1d point: ", &{$getter_1d_sub}(), " 2d point: ", join(' ', &{$getter_2d_sub}());
sergei@amdam2:~/junk> ./points_setters_getters.pl
1d point: 1 2d point: 2 3 at ./points_setter_getters.pl line 41.
Also, I do not functionally need to introduce internal $setter_sub, $getter_sub and then "connect" them - I could have directly assigned values to $setter_1d_sub, $getter_1d_sub, $setter_2d_sub, $getter_2d_sub inside the lexical scopes, but I have some more things on my mind to be shown later.
All I see you doing in the above example is creating two unnamed functions within the main body of your script. I don't see what's so good about that? Wouldn't it be more sensible to move the two blocks ("1d point", "2d point") into separate functions, which would accomplish not introducing the variables into your main namespace as well make them available to be called later?
All I see you doing in the above example is creating two unnamed functions within the main body of your script. I don't see what's so good about that? Wouldn't it be more sensible to move the two blocks ("1d point", "2d point") into separate functions, which would accomplish not introducing the variables into your main namespace as well make them available to be called later?
My point was to show a piece of code which, albeit being simple, cannot (AFAIK) be implemented in Python and to show the power of lexical scoping in avoiding name conflict.
My intent was then to provide a better approximation to OO by indeed creating constructors for 1d and 2d points, and the constructors could be called as many times as one wants. I.e. I meant to convert the example from kind of "inline object" into "create on demand object".
Furthermore, the constructors will return setter and getter - functions are first class objects, aren't they ?
So, you've exactly guessed my intent.
Hopefully later today I'll publish a working example, and this step is not the final one - I want to also show kind of inheritance/enhancement.
My point was to show a piece of code which, albeit being simple, cannot (AFAIK) be implemented in Python and to show the power of lexical scoping in avoiding name conflict.
Python, does have support for lexical closures it seems. by the way, the sub{} anonymous function in your Perl example is called lambda in Python.
Last edited by ghostdog74; 03-21-2009 at 02:18 AM.
Python's limited support for anonymous functions is the lambda construct. Since the availability of full anonymous functions is non-existent then named functions is the primary use of functions in Python. Lambdas are limited to containing expressions rather than statements, although control flow can still be implemented less elegantly within lambda by using short-circuiting.[3]
My point was to show a piece of code which, albeit being simple, cannot (AFAIK) be implemented in Python and to show the power of lexical scoping in avoiding name conflict.
My intent was then to provide a better approximation to OO by indeed creating constructors for 1d and 2d points, and the constructors could be called as many times as one wants. I.e. I meant to convert the example from kind of "inline object" into "create on demand object".
Furthermore, the constructors will return setter and getter - functions are first class objects, aren't they ?
So, you've exactly guessed my intent.
Hopefully later today I'll publish a working example, and this step is not the final one - I want to also show kind of inheritance/enhancement.
So let me take a stab at doing that in Python (I honestly don't know Perl so if I'm missing some subtlety let me know)
Code:
if __name__ == '__main__':
def point_1d():
x = 10
return x, x + x
setter_1d_sub, getter_1d_sub = point_1d()
print setter_1d_sub
print getter_1d_sub
setter_2d_sub = None
getter_2d_sub = None
def point_2d():
x = 100
setter_2d_sub, getter_2d_sub = x, x + x
print setter_2d_sub
print getter_2d_sub
return x, x + x
setter_2d_sub, getter_2d_sub = point_2d()
print setter_2d_sub
print getter_2d_sub
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.