Page 1 of 1

What is perl used for?

PostPosted: Mon Nov 03, 2014 2:43 am
by Dagrzinthesmile
I wanted to know what perl was used for because it seems for the most part an interesting langauge to learn. I also wanted to know in the case of writing programs, what are the types of programs that might work better in perl than in other languages?

Re: What is perl used for?

PostPosted: Mon Nov 03, 2014 3:38 am
by parakkafaith

Re: What is perl used for?

PostPosted: Mon Nov 03, 2014 9:07 am
by WallShadow
Perl is good for rapid test processing and quick testing of things. In short, perl has many small little accelerations combined with very powerful modules in order to complete difficult tasks quickly.

ex; i have a list of username and password pairs (in a "username:password" format) and i want to strip off the usernames and collect the passwords in a different file. a simple way to do this is:
Code: Select all
#!/usr/bin/perl
while (<>) {
   chomp;
   s/^.*://;
   print "$_\n";
}


But perl believes in "there's more than one way to do this", so can we make it shorter?
Code: Select all
#!/usr/bin/perl
while (<>) {
   print s/^.*:(.*)$/$1/r;
}


STILL shorter?
Code: Select all
#!/usr/bin/perl
print s/^.*:(.*)$/$1/r while <>;


And that's why perl is great. Short and dense statements like that can do magical things. You don't have to use them (and i often don't unless the code is under 10 lines) because then it becomes hard to expand it with features. ex; lets skip any empty lines or lines starting with a hash tag:
Code: Select all
#!/usr/bin/perl
while (<>) {
   next if /^(#|$)/;
   print s/^.*:(.*)$/$1/r;
}



But perl isn't only good for nitty gritty text processing. It has great modules that let you test anything and anywhere. Lets say i want to make a bot that serves a quote directly from hackthissite.org to the citizens of #coffeesh0p whenever they request coffee, but i don't want to spend more than 10 minutes on it (because that's about what they'll spend on it).
Code: Select all
#!/usr/bin/perl
package TestBot;
use parent 'Bot::BasicBot';
use LWP::Simple 'get';
sub getquote { $1 if get('https://www.hackthissite.org') =~ /<blockquote>(.*)<\/blockquote>/m }
sub said { getquote() // 'sorry, failed to get a quote :(' if pop->{body} =~ /^!coffee( |$)/ }
TestBot->new(
   server => 'irc.hackthissite.org', port   => '7000', ssl => 1,
   channels => ['#coffeesh0p'],
   nick      => 'JAPB', username  => 'JAPB',
   name      => 'Just Another Perl Bot',
)->run();


There are lots of great things about it, this is just a few examples.

- WallShadow <3

Re: What is perl used for?

PostPosted: Mon Nov 03, 2014 11:17 am
by cyberdrain
I heard someone say Perl is very useful for manipulating binary data, as something like Python usually handles strings just as well, but is pretty bad at manipulation of data. The regular expressions I've seen in Perl seem also very useful, as shown by Wallshadow above.

Re: What is perl used for?

PostPosted: Mon Nov 03, 2014 3:50 pm
by Dagrzinthesmile
Thanks Cyber and Wall for the info, also Wall thanks for the examples especially that bot one. I might practice perl a little bit 'cause it seems like a really useful language to learn and i like the idea of "there's more way than one".