What is perl used for?

Discuss how to write good code, break bad code, your current pet projects, or the best way to approach novel problems

What is perl used for?

Post by Dagrzinthesmile on Mon Nov 03, 2014 2:43 am
([msg=84744]see What is perl used for?[/msg])

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?
Dagrzinthesmile
New User
New User
 
Posts: 15
Joined: Mon Jul 14, 2014 4:25 am
Blog: View Blog (0)


Re: What is perl used for?

Post by parakkafaith on Mon Nov 03, 2014 3:38 am
([msg=84745]see Re: What is perl used for?[/msg])

"Any technology distinguishable from magic is insufficiently advanced."
- Probably not Arthur C. Clarke
User avatar
parakkafaith
Poster
Poster
 
Posts: 176
Joined: Fri Jan 07, 2011 10:59 pm
Blog: View Blog (0)


Re: What is perl used for?

Post by WallShadow on Mon Nov 03, 2014 9:07 am
([msg=84755]see Re: What is perl used for?[/msg])

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
User avatar
WallShadow
Contributor
Contributor
 
Posts: 686
Joined: Tue Mar 06, 2012 9:37 pm
Blog: View Blog (0)


Re: What is perl used for?

Post by cyberdrain on Mon Nov 03, 2014 11:17 am
([msg=84758]see Re: What is perl used for?[/msg])

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.
Free your mind / Think clearly
User avatar
cyberdrain
Expert
Expert
 
Posts: 2160
Joined: Sun Nov 27, 2011 1:58 pm
Blog: View Blog (0)


Re: What is perl used for?

Post by Dagrzinthesmile on Mon Nov 03, 2014 3:50 pm
([msg=84767]see Re: What is perl used for?[/msg])

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".
Dagrzinthesmile
New User
New User
 
Posts: 15
Joined: Mon Jul 14, 2014 4:25 am
Blog: View Blog (0)



Return to Programming

Who is online

Users browsing this forum: No registered users and 0 guests