LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 12-25-2009, 04:09 AM   #16
druuna
LQ Veteran
 
Registered: Sep 2003
Posts: 10,532
Blog Entries: 7

Rep: Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405

Quote:
Originally Posted by ghostdog74 View Post
why is that a "value" of Perl, considering other languages like Ruby,Python, PHP etc also capable of producing reports ?
Yes, other languages can do it too, but I was kinda pointing at this: Practical Extraction and Report Language. Which is what perl stands for
 
Old 12-25-2009, 08:11 AM   #17
MTK358
LQ 5k Club
 
Registered: Sep 2009
Posts: 6,443
Blog Entries: 3

Rep: Reputation: 723Reputation: 723Reputation: 723Reputation: 723Reputation: 723Reputation: 723Reputation: 723
This is pretty funny:

http://www.lmgtfy.com/?q=pathologica...rubbish+lister

Note the second result

Last edited by MTK358; 12-25-2009 at 08:12 AM.
 
Old 12-25-2009, 08:28 AM   #18
Sergei Steshenko
Senior Member
 
Registered: May 2005
Posts: 4,481

Rep: Reputation: 454Reputation: 454Reputation: 454Reputation: 454Reputation: 454
Quote:
Originally Posted by theNbomr View Post
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'.
 
Old 12-25-2009, 09:11 AM   #19
knockout_artist
Member
 
Registered: Sep 2005
Distribution: fedora core 9
Posts: 324

Rep: Reputation: 33
I love C and I love perl.

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.
 
Old 12-25-2009, 11:42 AM   #20
Sergei Steshenko
Senior Member
 
Registered: May 2005
Posts: 4,481

Rep: Reputation: 454Reputation: 454Reputation: 454Reputation: 454Reputation: 454
Quote:
Originally Posted by MTK358 View Post
This is pretty funny:

http://www.lmgtfy.com/?q=pathologica...rubbish+lister

Note the second result
Yeah, I know.

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) ?
 
Old 12-25-2009, 12:54 PM   #21
smeezekitty
Senior Member
 
Registered: Sep 2009
Location: Washington U.S.
Distribution: M$ Windows / Debian / Ubuntu / DSL / many others
Posts: 2,339

Original Poster
Rep: Reputation: 231Reputation: 231Reputation: 231
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.
 
Old 12-25-2009, 01:09 PM   #22
MTK358
LQ 5k Club
 
Registered: Sep 2009
Posts: 6,443
Blog Entries: 3

Rep: Reputation: 723Reputation: 723Reputation: 723Reputation: 723Reputation: 723Reputation: 723Reputation: 723
Quote:
Originally Posted by Sergei Steshenko View Post
Yeah, I know.

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.
I don't really understand.
 
Old 12-25-2009, 01:45 PM   #23
Sergei Steshenko
Senior Member
 
Registered: May 2005
Posts: 4,481

Rep: Reputation: 454Reputation: 454Reputation: 454Reputation: 454Reputation: 454
Quote:
Originally Posted by MTK358 View Post
I don't really understand.
What you don't understand ?

I gave a simple mission to accomplish - a function which calculates factorial with a coefficient.

My C++ is rusty, but more or less in C++ it would look (except for anonymity/ name space pollution) like this:

Code:
class FactorialMultipliedByK
  {
  private:
    unsigned k;

  public:
    void FactorialMultipliedByK(unsigned k) // constructor setting k
      {
      this.k = k
      }

    unsigned factorial(unsigned n) // recursive function
      {
      if(n != 0)
        {
        return n * factorial(--n);
        }
      else
        {
        return k;
        }
      }
  }

Last edited by Sergei Steshenko; 12-25-2009 at 01:48 PM.
 
Old 12-25-2009, 01:50 PM   #24
MTK358
LQ 5k Club
 
Registered: Sep 2009
Posts: 6,443
Blog Entries: 3

Rep: Reputation: 723Reputation: 723Reputation: 723Reputation: 723Reputation: 723Reputation: 723Reputation: 723
So you want a function to calculate a factorial that doesn't use global variables?
 
Old 12-25-2009, 02:06 PM   #25
Sergei Steshenko
Senior Member
 
Registered: May 2005
Posts: 4,481

Rep: Reputation: 454Reputation: 454Reputation: 454Reputation: 454Reputation: 454
Quote:
Originally Posted by MTK358 View Post
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.
 
Old 12-25-2009, 02:31 PM   #26
Sergei Steshenko
Senior Member
 
Registered: May 2005
Posts: 4,481

Rep: Reputation: 454Reputation: 454Reputation: 454Reputation: 454Reputation: 454
Quote:
Originally Posted by smeezekitty View Post
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.
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.
 
Old 12-25-2009, 03:27 PM   #27
MTK358
LQ 5k Club
 
Registered: Sep 2009
Posts: 6,443
Blog Entries: 3

Rep: Reputation: 723Reputation: 723Reputation: 723Reputation: 723Reputation: 723Reputation: 723Reputation: 723
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.
 
Old 12-25-2009, 03:54 PM   #28
Sergei Steshenko
Senior Member
 
Registered: May 2005
Posts: 4,481

Rep: Reputation: 454Reputation: 454Reputation: 454Reputation: 454Reputation: 454
Quote:
Originally Posted by MTK358 View Post
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).
 
Old 12-25-2009, 04:11 PM   #29
MTK358
LQ 5k Club
 
Registered: Sep 2009
Posts: 6,443
Blog Entries: 3

Rep: Reputation: 723Reputation: 723Reputation: 723Reputation: 723Reputation: 723Reputation: 723Reputation: 723
I still just don't understand all of this...
 
Old 12-25-2009, 04:15 PM   #30
Sergei Steshenko
Senior Member
 
Registered: May 2005
Posts: 4,481

Rep: Reputation: 454Reputation: 454Reputation: 454Reputation: 454Reputation: 454
Quote:
Originally Posted by MTK358 View Post
I still just don't understand all of this...
There are two relevant items WRT "all":
  1. my code example;
  2. my explanations.

So, which is the first code item or the first word/sentence you don't understand ?
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Languages Worth Learning TaylanUB Programming 30 01-09-2010 10:45 AM
Python - worth learning? vharishankar Programming 39 07-08-2005 02:38 AM
Is Visual Basic worth learning? titanium_geek Programming 20 07-07-2005 09:58 AM
Is it really worth learning vi at this point? Tyir Linux - General 8 02-24-2004 12:51 AM
Is scripting worth learning ChimpFace9000 Linux - General 3 07-05-2001 04:02 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 12:15 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration