Learning Perl

Software related topics

Learning Perl

Postby Alfredo Tigolo » Fri Oct 09, 2015 12:04 pm

http://learn.perl.org/tutorials/

Here are some tutorials online in learning perl. Here is my simple for loop and ideas of how to calculate tables using perl with mysql

Code: Select all
#!/usr/bin/perl
print ("Hello, world\n");
for ( my $i=0; $i <= 9; $i++) {
    print "$i\n";
}

#cluster programming
#call mysql to calculate
Alfredo Tigolo
 
Posts: 2
Joined: Fri Oct 09, 2015 11:01 am
Location: California

Re: Learning Perl

Postby Doug Coulter » Sat Oct 10, 2015 2:20 pm

One of the stated reasons some people hate perl is that there are things like perl golf, in which the object is to get it done with minimal keystrokes - often leading to unreadable code.
However...it can be fun too, for trivial examples. Here's some trick code - doesn't do quite the same as the above (no linefeeds) but close, and a lot less strokes.
Code: Select all
#!/usr/bin/perl -w
use strict; # flag questionable constructs, -w above turns on warnings
for (0 .. 9) {print}


I used -w to turn on warnings globally, as well as the sanity-saving use strict. Strictly speaking, we don't need either one here - I can reduce the strokes, but I like to encourage their use. Often via the error messages and warnings these create, one can avoid all need for a debugger (perl has one, but ugh).

I make use of the perl pronoun, which is named $_, but used implicitly in many functions if you don't specify something else. The 0 .. 9 construct creates a list of numbers in that range (and is faster than incrementing a variable, and takes up less memory). print prints $_ in the absence of an argument. I don't need a semicolon on the last line of any block... In perl, for is synonymous with foreach, which takes a scalar (in this case the pronoun "it", or $_, but you can have your own variable if you like there) and a list, which here is generated by the .. range operator.
I'd bet this could be taken further...
Posting as just me, not as the forum owner. Everything I say is "in my opinion" and YMMV -- which should go for everyone without saying.
User avatar
Doug Coulter
 
Posts: 3515
Joined: Wed Jul 14, 2010 7:05 pm
Location: Floyd county, VA, USA

Re: Learning Perl

Postby Doug Coulter » Sat Oct 10, 2015 2:32 pm

Yup...OK, guys, beat this:
Code: Select all
#!/usr/bin/perl
$\ = "\n";
for (0 .. 9) {print}


$\ is perl's global default output record separator, normally set to undef - that's where our linefeeds now come from. I got rid of warnings and strict because...well, look at the strokes. You could even get rid of the shebang line (#!/usr/bin/perl) in exchange for more typing in the terminal, but hey. For what it's worth, I believe the .. range construct can use scalar variables to define the ends of the range...(I'd bet there's a slicker way to set $\ to newline as well).

And here it is:
Code: Select all
#!/usr/bin/perl
$\ = $/;
for (0 .. 9) {print}

$/ is perl's global default input record separator, and is automatically set to the opsys default newline character(s), as is \n in the previous example. Thus, either will run in linux where a newline is linefeed, or windows, where it is /r/n, or macs, where it is /r. How about that? And I saved two more strokes...
Posting as just me, not as the forum owner. Everything I say is "in my opinion" and YMMV -- which should go for everyone without saying.
User avatar
Doug Coulter
 
Posts: 3515
Joined: Wed Jul 14, 2010 7:05 pm
Location: Floyd county, VA, USA


Return to Software

Who is online

Users browsing this forum: No registered users and 4 guests

cron