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.
Which was exactly my point. I was demonstrating that 'x' is not still defined in the second function, hence the error when running.
Quote:
Third, your "def point_1d", "def point_2d" do _not_ exist in my case, i.e. I do not have anything like this in the outer namespace, and nowhere else for that matter.
True enough. However you also previously said:
Quote:
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".
So it also seems like you were planning on doing something similar.
Your other issues don't seem pertinent to the issue at hand, which is variable scope.
Which was exactly my point. I was demonstrating that 'x' is not still defined in the second function, hence the error when running.
True enough. However you also previously said:
So it also seems like you were planning on doing something similar.
Your other issues don't seem pertinent to the issue at hand, which is variable scope.
Look, I've shown a simple example, effectively having two scopes.
Even though my Pyhon knowledge is superficial, I _do_ know that functions have separate scope, i.e. essentially, there are two scopes in Python:
current module
function
.
So, I can introduce an example with more than two scopes.
Anyway, with your named scopes you yet have to show a piece of code which produces the same results as mine, i.e. code which has working setters and getters.
Another important point of mine is anonymity - my scopes are anonymous, so there's no danger of name contention.
You had to introduce scope names, the names which come after 'def' statements, so each new item added in such a manner requires remembering names of previous items. I am too lazy for this - especially, because mankind has already developed languages which allow me to be lazy.
I like my life simple, so the way i see it, i would just use a database.
Are you telling me that I am obliged to use a database to export/import parse trees ?
If yes, what for ? I need parse trees to be represented as data structures in the language I am working in, so why would I need yet another level of complexity and bugs called database ?
Are you telling me that I am obliged to use a database to export/import parse trees ?
no you are free to do what you want. The last i look at it, you did not say your use case for doing that is for parsing trees. Again, i have no experience on this subject, but there are Python modules for parsing trees...
So to turn this around, please show me how this small change is possible to the code you posted earlier.
Code:
$ cat example_2.py
x = 10
import my_dict
print 'x =', x
for dict in my_dict.dict, my_dict.dict2:
for key in sorted(dict):
print key, '=', dict[key]
$ cat my_dict.py
x = 0
print "inside imported file, x =", x
dict = {'foo': x, 'bar': x + 1}
dict2 = {'baz': x + 2, 'blah': x + 3}
$ python example_2.py
inside imported file, x = 0
x = 10
bar = 1
foo = 0
baz = 2
blah = 3
This example proves my point: lack of anonymity.
You have your main program:
Code:
x = 10
import my_dict
print 'x =', x
for dict in my_dict.dict, my_dict.dict2:
for key in sorted(dict):
print key, '=', dict[key]
and you have the imported file:
Code:
x = 0
print "inside imported file, x =", x
dict = {'foo': x, 'bar': x + 1}
dict2 = {'baz': x + 2, 'blah': x + 3}
Your 'x' variables are separated by the 'dict' namespace, the latter is global.
This means that if you want to split the work between developers, you have to assign the global names - otherwise there can be two or more developers who can use the same 'dict' name.
My code has nothing global, I do not care which variable names are used by the developer who created the imported data.
Likewise, your earlier example:
Code:
$ cat example_2.py
x = 10
import my_dict
hash_ref = my_dict.dict
print 'x =', x
for key in sorted(hash_ref):
print key, '=', hash_ref[key]
$ cat my_dict.py
x = 0
print "inside imported file, x =", x
dict = {'foo': x, 'bar': x + 1}
$ python example_2.py
inside imported file, x = 0
x = 10
bar = 1
has the same flaw - you are using 'my_dict' global entity - my code has no global entities.
Now, if your question
Quote:
show me how this small change is possible to the code you posted earlier
means the desire to be shown named import in Perl - no problem:
Code:
sergei@amdam2:~/junk> cat -n package_importer.pl
1 #!/usr/bin/perl -w
2
3 use strict;
4
5 my $x = 10;
6 require './Package.pm';
7
8 warn "inside MAIN: \$x=$x";
9
10
11 foreach my $hash_ref(\%Package::hash1, \%Package::hash2)
12 {
13 warn "\$hash_ref=$hash_ref BEGIN";
14
15 foreach my $key(sort keys %{$hash_ref})
16 {
17 warn "k/v: $key/$hash_ref->{$key}";
18 }
19
20 warn "\$hash_ref=$hash_ref END";
21 warn "\n\n";
22 }
23
24
25
sergei@amdam2:~/junk> cat -n Package.pm
1 package Package;
2
3
4 my $x = 0;
5 our %hash1 = # 'our' mean global - opposed to 'my' - lexical
6 (
7 foo => $x++,
8 bar => $x++
9 );
10
11 our %hash2 =
12 (
13 doo => $x++,
14 dah => $x++
15 );
16
17 warn "inside Package: \$x=$x";
18
19 1; # this is is needed for standard Perl export/import, any TRUE (non-FALSE)
20 # value is OK
sergei@amdam2:~/junk> ./package_importer.pl
Name "Package::hash2" used only once: possible typo at ./package_importer.pl line 11.
Name "Package::hash1" used only once: possible typo at ./package_importer.pl line 11.
inside Package: $x=4 at ./Package.pm line 17.
inside MAIN: $x=10 at ./package_importer.pl line 8.
$hash_ref=HASH(0x8194dcc) BEGIN at ./package_importer.pl line 13.
k/v: bar/1 at ./package_importer.pl line 17.
k/v: foo/0 at ./package_importer.pl line 17.
$hash_ref=HASH(0x8194dcc) END at ./package_importer.pl line 20.
$hash_ref=HASH(0x816e9c0) BEGIN at ./package_importer.pl line 13.
k/v: dah/3 at ./package_importer.pl line 17.
k/v: doo/2 at ./package_importer.pl line 17.
$hash_ref=HASH(0x816e9c0) END at ./package_importer.pl line 20.
sergei@amdam2:~/junk>
Now look at this:
Code:
sergei@amdam2:~/junk> cat -n example_2.py
1 x = 10
2
3 import my_dict
4
5 print 'x =', x
6
7 for dict in my_dict.dict, my_dict.dict2:
8 for key in sorted(dict):
9 print key, '=', dict[key]
10
11 print "added by Sergei: inside dict x=", my_dict.x
sergei@amdam2:~/junk> cat -n my_dict.py
1 x = 0
2
3 print "inside imported file, x =", x
4
5 dict = {'foo': x, 'bar': x + 1}
6 dict2 = {'baz': x + 2, 'blah': x + 3}
sergei@amdam2:~/junk> python example_2.py
inside imported file, x = 0
x = 10
bar = 1
foo = 0
baz = 2
blah = 3
added by Sergei: inside dict x= 0
sergei@amdam2:~/junk>
- this your code with just one added by me line:
Code:
11 print "added by Sergei: inside dict x=", my_dict.x
and this line shows that data inside your 'my_dict.py' is not encapsulated.
Opposed to that, if you look at my
Code:
4 my $x = 0;
line inside 'Package.pm', you'll see that this $x is encapsulated - because it is a lexical variable, and in Perl 'use'/'require' create implicit scope, i.e. you cant access it from 'main' or another file.
So, lexical scoping is about modularity, avoiding name contention, robustness.
And, just to make it more clear, my example with 'Package' does pollute global namespace - the same way as its Python counterpart.
Last edited by Sergei Steshenko; 03-22-2009 at 03:52 AM.
Now, if your question means the desire to be shown named import in Perl - no problem:
What I was trying to point out is that the model you were using, anonymous import, didn't look able to handle the creation of more than one hash. I should also point out that if we're actually talking about data persistence Python has separate modules strictly for that purpose, which allow for the importing of multiple anonymous pieces of data.
Quote:
and this line shows that data inside your 'my_dict.py' is not encapsulated.
What I was trying to point out is that the model you were using, anonymous import, didn't look able to handle the creation of more than one hash. I should also point out that if we're actually talking about data persistence Python has separate modules strictly for that purpose, which allow for the importing of multiple anonymous pieces of data.
I never claimed it was
I can always import multiple items (hash1 ... hashN) this way
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.