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.
In my opinion (who else's opinion could I express...?), Perl, when used for the purpose it is intended, can result in very high productivity. Parsing and otherwise handling large volumes of text is certainly at the center of that purpose. If you grock regular expressions, it will feel very comfortable, as well. Almost any problem that looks like I could solve with sed or awk, I just reach for Perl, because I know I can solve it with that.
I prefer to use Perl as a CGI over PHP, but that's just me. For any kind of programming that will probably end up in a customer's hands, I would instinctively not use Perl, but again, that's probably just me.
I do some system management and similar activities in my work, and I doubt that a single day goes by when I don't write at least a little Perl; often one-liners on the commandline.
--- rod.
At the moment I ma writing in Perl something to produce numerically intensive code in "C"; the Perl part implements symbolic manipulations and hard core optimizations.
The whole thing is written with bias towards functional programming (nested functions, closures, etc.) and metaprogramming (i.e. templates) - the latter is implemented through conditional compilation using Perl 'eval'.
I am good not good on either one of them, but I have make them work for me.
C is for hardcore stuff. Its real fun and fascinating. Like bear bone thing to build stuff on.
Perl is like stuff already built for you ready to use quickly.
Last edited by knockout_artist; 12-25-2009 at 09:19 AM.
Now here is a "quiz" for you and others - suppose I want a parametrized recursive function calculating
k * n!
where 'k' is parameter (i.e. moral equivalent to constructor argument in OO) and 'n' is the number factorial of which is to be calculated.
Write such a code in a way that doesn't pollute global name space, i.e. in a manner that anyone can take it and use it in his/her program without thinking/needing to check whether names collide or not.
Here is a solution in Perl:
Code:
sergei@amdam2:~/junk/parametrized_factorial>
sergei@amdam2:~/junk/parametrized_factorial> cat -n factorial_constructor.prl
1 use warnings;
2 use strict;
3
4 sub($$)
5 {
6 my ($self_ref, $k) = @_;
7
8 sub($)
9 {
10 my ($n) = @_;
11
12 return $k if $n == 0;
13
14 return $n * ${$self_ref}->($n - 1);
15 }
16 };
sergei@amdam2:~/junk/parametrized_factorial> cat -n main.pl
1 #!/usr/bin/perl -w
2
3 use warnings;
4 use strict;
5
6
7 my $factorial_constructor = require './factorial_constructor.prl';
8
9 my $factorial_with_k_equal_1;
10 $factorial_with_k_equal_1 = $factorial_constructor->(\$factorial_with_k_equal_1, 1);
11
12 my $factorial_with_k_equal_2;
13 $factorial_with_k_equal_2 = $factorial_constructor->(\$factorial_with_k_equal_2, 2);
14
15
16 warn "\$factorial_with_k_equal_1->(3)=", $factorial_with_k_equal_1->(3);
17 warn "\$factorial_with_k_equal_2->(5)=", $factorial_with_k_equal_2->(5);
18
sergei@amdam2:~/junk/parametrized_factorial> /home/sergei/junk/parametrized_factorial/main.pl
$factorial_with_k_equal_1->(3)=6 at /home/sergei/junk/parametrized_factorial/main.pl line 16.
$factorial_with_k_equal_2->(5)=240 at /home/sergei/junk/parametrized_factorial/main.pl line 17.
sergei@amdam2:~/junk/parametrized_factorial>
The parametrized function is described in 'factorial_constructor.prl' file and, as said, by construction it can be used in any program because it pollutes no namespace.
And, if one needs just one instance, see this one-liner:
Code:
sergei@amdam2:~/junk/parametrized_factorial> perl -e 'use strict; my $factorial; $factorial = (require "./factorial_constructor.prl")->(\$factorial, 2); warn "\$factorial->(3)=", $factorial->(3);'
$factorial->(3)=12 at -e line 1.
sergei@amdam2:~/junk/parametrized_factorial>
C++/Java aficionados (as well a Python ones) are welcome to implement the above example with all the features.
...
And where is the rubbish (even though I know the Pathologically Eclectic Rubbish Lister since the early nineties) ?
Distribution: M$ Windows / Debian / Ubuntu / DSL / many others
Posts: 2,339
Original Poster
Rep:
Code:
The parametrized function is described in 'factorial_constructor.prl' file and, as said, by construction it can be used in any program because it pollutes no namespace.
why not just use a sub name() { } and the user could rename the procedure if required without making unreada-perl.
Now here is a "quiz" for you and others - suppose I want a parametrized recursive function calculating
k * n!
where 'k' is parameter (i.e. moral equivalent to constructor argument in OO) and 'n' is the number factorial of which is to be calculated.
Write such a code in a way that doesn't pollute global name space, i.e. in a manner that anyone can take it and use it in his/her program without thinking/needing to check whether names collide or not.
So you want a function to calculate a factorial that doesn't use global variables?
No, you don't get my point.
In C/C++ functions (unless they are static) are global entities.
I.e. if I want to use your function in my code, I must make sure your function name does not appear in my code.
Remember, I am all the time saying that OO is not a true value, it's a mean, true values are robustness/maintainability/reusability.
Likewise, in C++ class names are global entities.
I.e. if I want to use a class (FactorialMultipliedByK in my example) written by you, I must make sure your class name does not appear in my code.
Perl (and other functional languages supporting closures, nested functions and anonymity) gives a clean and robust and careless solution - just reuse the code and have no fears about names at all. Because there are no names - the code user (in my example 'main.pl') decides on names (in my example '$factorial_constructor', '$factorial_with_k_equal_1', '$factorial_with_k_equal_2').
Also, due to Perl properties, the code included trough 'require' sees no lexical variables from the enclosing scope, and vice versa, i.e. complete encapsulation is present.
The parametrized function is described in 'factorial_constructor.prl' file and, as said, by construction it can be used in any program because it pollutes no namespace.
why not just use a sub name() { } and the user could rename the procedure if required without making unreada-perl.
Because Perl is for the lazy - and if you think about it, laziness is in this case a virtue - names are redundant in the piece of code which describes actions.
Also, when one works on a project with, say, 25 developers, either naming conventions should be established and complied with, or a tool (Perl in this case) can be used which gives freedom of expression/creativity without hampering reliability/robustness.
As, I said, Perl is not "C", and the less one thinks of it as of "C" with built-in hashes and elastic arrays, the better.
Perl was not my first or even second computer language, however, when I really got into it, I started liking it very much. The key is to free one's mind.
And if you think my code is unreadable, tell me what exactly in it is unreadable.
By the way, even "C" (C99 to be exact) has anonymous data structures.
So if I wrote a function in another file, I can choose the name for it in the other file? I don't understand how this is possible because you need some kind of reference.
So if I wrote a function in another file, I can choose the name for it in the other file? I don't understand how this is possible because you need some kind of reference.
In Perl 'require' operator is performed on files.
'require' returns a a value which is the value of the last exectuted statement.
If you look into may 'factorial_constructor.prl' file, you'll see the following structure:
Code:
sub
{
...
sub
{
...
}
}
i.e. we have an outer and an inner subroutine - both anonymous, so they return code references.
So, the last statement executed by 'require' is the outer anonymous subroutine. I.e. 'require' returns code reference to that subroutine. This is because Perl first compiles the file, and then executes it, and the only statement is that outer subroutine. It is not called, so just a reference to it is returned.
It's like in "C"
Code:
// in 'foo.c' file:
void foo(...)
{
}
// in perl_compiler.c
subroutine_type c_compiler(char *foo_file)
{
// compile foo_file into foo function
...
return foo; // pointer to 'foo' is returned
}
In 'main.pl' file this line:
Code:
7 my $factorial_constructor = require './factorial_constructor.prl';
assigns the code reference to '$factorial_constructor' variable - I chose the name to illustrate the purpose, but it can be any name.
Do you need further explanations or now you understand how factorial multiplied by hidden private '$k' is calculated ?
One important note - the principal/major/strategic/quintessential difference between Perl and C++ is that in Perl constructors do return a value. In functional languages functions are first class objects - they can be returned and passed as parameters to other functions.
So, in my example 'require' returns a function (code reference to be precise).
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.