What Other Languages Have Features And/Or Libraries Similar to Perl's Format

What other languages have features and/or libraries similar to Perl's format?

FormatR provides Perl-like formats for Ruby.

Here is an example from the documentation:

require "formatr"
include FormatR

top_ex = <<DOT
Piggy Locations for @<< @#, @###
month, day, year

Number: location toe size
-------------------------------------------
DOT

ex = <<TOD
@) @<<<<<<<<<<<<<<<< @#.##
num, location, toe_size
TOD

body_fmt = Format.new (top_ex, ex)

body_fmt.setPageLength(10)
num = 1

month = "Sep"
day = 18
year = 2001
["Market", "Home", "Eating Roast Beef", "Having None", "On the way home"].each {|location|
toe_size = (num * 3.5)
body_fmt.printFormat(binding)
num += 1
}

Which produces:

   Piggy Locations for Sep 18, 2001

Number: location toe size
-------------------------------------------
1) Market 3.50
2) Home 7.00
3) Eating Roast Beef 10.50
4) Having None 14.00
5) On the way home 17.50

Are you taking up Perl and what got you into it?

With Perl (and the expressive power behind TMTOWTDI), programming becomes a creative task. I can write if($expr) { $statement; } if I plan on having many else statements, or I can write $statement if $expr; if that makes more sense (for example, I'm fond of writing:

sub doSomething {
my($toObject, $argument) = @_;

die "No object specified" unless defined($toObject);
die "Object invalid: $toObject" unless $toObject->ISA('Example');

# Do stuff
}

but of course that's not always the easiest, and most expressive way of doing it; so I come up with a better way for the task at hand!). People complain because Perl lets you write horrible looking code; I love Perl because it lets me write code which looks pretty to me (and yes, I can see the downside of having a dozen different programmers writing in their own styles; I'll hold to the idea that good writers can be pretty expressive and understandable no matter how different the subject matter).

With other programming languages, I end up having to think my way through abstraction layers (how will this Map give me Collections whose Iterators I can use to ... and so on). With Perl, I'm usually only one abstraction level above basic Perl. For instance, DBI will give me database results as ordinary, everyday Perl scalars, lists and hashes, so everything I know about these simple, core data structures carries over to every task I put DBI to (Complicated data structures? That's what PostgreSQL is for!).

I've only been using Perl full-time for about a year, but these are the big wins for me, and the reason I first went full-time onto Perl after a year suffering at the hands of Java 1.4's Collections model (don't ask). Other programming languages make me feel like I'm putting together a jigsaw puzzle, as you line up all the modules and packages just right; Perl feels like a box full of Legos, with some "special" bricks (like DBI, CGI.pm and Test::*) thrown in for good measure. There are tons of different ways to solve any problem, and Perl lets you try any of them you like, in any way you like.

What do people mean when they say “Perl is very good at parsing”?

They mean that Perl was originally designed for processing text files and has many features that make it easy:

  • Perl has many functions for string processing: substr, index, chomp, length, grep, sort, reverse, lc, ucfirst, ...
  • Perl automatically converts between numbers and strings depending on how a value is used. (e.g. you can read the character string '100' from a file and add one to it without needing to do an string to integer conversion first)
  • Perl automatically handles conversion to and from the platform encoding (e.g. CRLF on Windows) and a logical newline ("\n") within your program.
  • Regular expressions are integrated into the syntax instead of being a separate library.
  • Perl's regular expressions are the "gold standard" for power and functionality.
  • Perl has full Unicode support.

Python and Ruby also have good facilities for text processing. (Ruby in particular took much inspiration from Perl, much as Perl has shamelessly borrowed from many other languages.) There's little point in asking which is better. Use what you like.

Is there a module which gives me an output of two colums ( or more ) on STDOUT?

Text::Column

use Text::Column qw(format_array_table);

print format_array_table([map [$_, 50+$_], 1..50], [6, 6], [qw(first second)]);

Or, if you're on a UNIX system, pipe through pr(1).

open COLUMN, '|-', qw(pr -T2);
print COLUMN "$_\n" for 1..100;

Is there something in ruby similar to perlform?

you can use FormatR gem:

require "formatr"
include FormatR

# Specify format
report_top = <<DOT
Charge Code Hours Description
============= ======== ===============================================
DOT

report = <<DOT
@<<<<<<<<<<<< @<<<<<<< @<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
reportChargeCode, reportHours, reportDescription
DOT

body_fmt = Format.new (report_top, report)

body_fmt.setPageLength(10)
num = 1

Reaports.each do |(reportChargeCode, reportHours, reportDescription)|
body_fmt.printFormat(binding)
end

CL-WHO-like HTML templating for other languages?

For CPAN offerings have a look at the following (in alphabetical order)...

  • Builder
  • HTML::AsSubs
  • HTML::Tiny
  • Markapl
  • Template::Declare
  • XML::Generator

Using the table part of the CL-WHO example provided (minus Roman numerals and s/background-color/color/ to squeeze code into screen width here!)....


Builder

use Builder;
my $builder = Builder->new;
my $h = $builder->block( 'Builder::XML' );

$h->table( { border => 0, cellpadding => 4 }, sub {
for ( my $i = 1; $i < 25; $i += 5 ) {
$h->tr( { align => 'right' }, sub {
for my $j (0..4) {
$h->td( { color => $j % 2 ? 'pink' : 'green' }, $i + $j );
}
});
}
});

say $builder->render;


HTML::AsSubs

use HTML::AsSubs;

my $td = sub {
my $i = shift;
return map {
td( { color => $_ % 2 ? 'pink' : 'green' }, $i + $_ )
} 0..4;
};

say table( { border => 0, cellpadding => 4 },
map {
&tr( { align => 'right' }, $td->( $_ ) )
} loop( below => 25, by => 5 )
)->as_HTML;


HTML::Tiny

use HTML::Tiny;
my $h = HTML::Tiny->new;

my $td = sub {
my $i = shift;
return map {
$h->td( { 'color' => $_ % 2 ? 'pink' : 'green' }, $i + $_ )
} 0..4;
};

say $h->table(
{ border => 0, cellpadding => 4 },
[
map {
$h->tr( { align => 'right' }, [ $td->( $_ ) ] )
} loop( below => 25, by => 5 )
]
);


Markapl

use Markapl;

template 'MyTable' => sub {
table ( border => 0, cellpadding => 4 ) {
for ( my $i = 1; $i < 25; $i += 5 ) {
row ( align => 'right' ) {
for my $j ( 0.. 4 ) {
td ( color => $j % 2 ? 'pink' : 'green' ) { $i + $j }
}
}
}
}
};

print main->render( 'MyTable' );


Template::Declare

package MyTemplates;
use Template::Declare::Tags;
use base 'Template::Declare';

template 'MyTable' => sub {
table {
attr { border => 0, cellpadding => 4 };
for ( my $i = 1; $i < 25; $i += 5 ) {
row {
attr { align => 'right' };
for my $j ( 0..4 ) {
cell {
attr { color => $j % 2 ? 'pink' : 'green' }
outs $i + $j;
}
}
}
}
}
};

package main;
use Template::Declare;
Template::Declare->init( roots => ['MyTemplates'] );
print Template::Declare->show( 'MyTable' );


XML::Generator

use XML::Generator;
my $x = XML::Generator->new( pretty => 2 );

my $td = sub {
my $i = shift;
return map {
$x->td( { 'color' => $_ % 2 ? 'pink' : 'green' }, $i + $_ )
} 0..4;
};

say $x->table(
{ border => 0, cellpadding => 4 },
map {
$x->tr( { align => 'right' }, $td->( $_ ) )
} loop( below => 25, by => 5 )
);



And the following can be used to produce the "loop" in HTML::AsSubs / HTML::Tiny / XML::Generator examples....

sub loop {
my ( %p ) = @_;
my @list;

for ( my $i = $p{start} || 1; $i < $p{below}; $i += $p{by} ) {
push @list, $i;
}

return @list;
}

Should I use Perl or PHP or something else for this project?

Since I'm a PHP guy, here is what I can offer about PHP

  • PHP scales well due to it's shared nothing architecture
  • PHP has native support for various XML libs
  • PHP has native support for a number of RDBMS
  • PHP has native support for caching
  • PHP has native support for webservices
  • PHP is a templating engine

So the requirements to a language from your question are met by PHP.

However, Perl, Python or Ruby or even ServerSide JavaScript (...) should all be capable of doing what you are asking for either. PHP has it's quirks, so do the other languages. If you are a Java Guy, you might like Ruby for it's syntax, but then again, only you can decide.

Mapping Languages to Paradigms

I think you're approaching it wrong. As esr himself says, it's not the language that matters, it's the paradigm. So when you say that

  1. Perl is a functional language
  2. It's great for quick text substitutions in multiple files from the command line

you are missing one of the main points of a functional language which is that they are great for building large systems using a bottom up approach: solve a bunch of (well chosen) small problems with well designed functions until we have a complete system. We cut down on code duplication by identifying what algorithms that we are using have in common and using higher order functions to encapsulate their commonality. We minimize (overt) branching behavior by using higher order functions to cook up just the function that we need for a given situation.

Likewise, I could say that

  1. Java is mainly an OOP language
  2. It's good for writing large, robust systems,

but that misses the point that OOP languages are about modeling concepts from the problem domain in code so that we are left with a clear way to imperatively solve the problem at hand. We cut down on code duplication by identifying what the relevant concepts have in common and encapsulating the code that deals with those commonalities in a class that describes it. We minimize (overt) branching behavior by providing different subclasses of an abstraction with appropriately different behavior.

On the whole, the basic point of programming languages and their associated paradigms is

  • to allow you to not think about anything that doesn't affect the quality of the resulting program. If that wasn't a (largely) desirable thing, then we would all be writing machine code.

  • This is accomplished by (among other things) providing a set of tools for building abstractions.

Shop around and pick one that you like and get good at. Just make sure that you learn when the other ones allow for a better solution (this will probably mean getting good at them eventually too ;). I think that you can mainly take "good solution" to mean, "clear mapping of code to ideas". (modulo concerns about efficiency that would force you (provide an excuse?) to write in a language like C)

CL-WHO-like HTML templating for other languages?

For CPAN offerings have a look at the following (in alphabetical order)...

  • Builder
  • HTML::AsSubs
  • HTML::Tiny
  • Markapl
  • Template::Declare
  • XML::Generator

Using the table part of the CL-WHO example provided (minus Roman numerals and s/background-color/color/ to squeeze code into screen width here!)....


Builder

use Builder;
my $builder = Builder->new;
my $h = $builder->block( 'Builder::XML' );

$h->table( { border => 0, cellpadding => 4 }, sub {
for ( my $i = 1; $i < 25; $i += 5 ) {
$h->tr( { align => 'right' }, sub {
for my $j (0..4) {
$h->td( { color => $j % 2 ? 'pink' : 'green' }, $i + $j );
}
});
}
});

say $builder->render;


HTML::AsSubs

use HTML::AsSubs;

my $td = sub {
my $i = shift;
return map {
td( { color => $_ % 2 ? 'pink' : 'green' }, $i + $_ )
} 0..4;
};

say table( { border => 0, cellpadding => 4 },
map {
&tr( { align => 'right' }, $td->( $_ ) )
} loop( below => 25, by => 5 )
)->as_HTML;


HTML::Tiny

use HTML::Tiny;
my $h = HTML::Tiny->new;

my $td = sub {
my $i = shift;
return map {
$h->td( { 'color' => $_ % 2 ? 'pink' : 'green' }, $i + $_ )
} 0..4;
};

say $h->table(
{ border => 0, cellpadding => 4 },
[
map {
$h->tr( { align => 'right' }, [ $td->( $_ ) ] )
} loop( below => 25, by => 5 )
]
);


Markapl

use Markapl;

template 'MyTable' => sub {
table ( border => 0, cellpadding => 4 ) {
for ( my $i = 1; $i < 25; $i += 5 ) {
row ( align => 'right' ) {
for my $j ( 0.. 4 ) {
td ( color => $j % 2 ? 'pink' : 'green' ) { $i + $j }
}
}
}
}
};

print main->render( 'MyTable' );


Template::Declare

package MyTemplates;
use Template::Declare::Tags;
use base 'Template::Declare';

template 'MyTable' => sub {
table {
attr { border => 0, cellpadding => 4 };
for ( my $i = 1; $i < 25; $i += 5 ) {
row {
attr { align => 'right' };
for my $j ( 0..4 ) {
cell {
attr { color => $j % 2 ? 'pink' : 'green' }
outs $i + $j;
}
}
}
}
}
};

package main;
use Template::Declare;
Template::Declare->init( roots => ['MyTemplates'] );
print Template::Declare->show( 'MyTable' );


XML::Generator

use XML::Generator;
my $x = XML::Generator->new( pretty => 2 );

my $td = sub {
my $i = shift;
return map {
$x->td( { 'color' => $_ % 2 ? 'pink' : 'green' }, $i + $_ )
} 0..4;
};

say $x->table(
{ border => 0, cellpadding => 4 },
map {
$x->tr( { align => 'right' }, $td->( $_ ) )
} loop( below => 25, by => 5 )
);



And the following can be used to produce the "loop" in HTML::AsSubs / HTML::Tiny / XML::Generator examples....

sub loop {
my ( %p ) = @_;
my @list;

for ( my $i = $p{start} || 1; $i < $p{below}; $i += $p{by} ) {
push @list, $i;
}

return @list;
}

Survey of programming languages and their purpose and strengths

My three favorite languages are Haskell, C++, and Python. Conveniently, those break up nicely into rather distinct groups.

  • I use Haskell when I feel like I want to program in a very mathematically rigorous sense --- the functional programming helps, as does the strong type-checker. I am quite interested in algorithms, and at least personally I find it much easier to look at a functional algorithm rather than an imperative one, once I wade through all the map/folds/lambdas etc. I think this holds true for many functional languages in general, though Lisp adds on that extra layer of "holy crap my code is my data", which can help or hinder.

  • I use C++ as the counterpart to Haskell. Rather than feeling mathematically vigorous, if I want to feel like I'm total control of the program flow, I use C++. Defining all the various built-in operators for a new class gives me a warm fuzzy feeling, what can I say. It has never really come up, but if I desperately need a fast program I would turn to C++. As mathematical control is to Haskell, so is system control to C++? May be a bit hackneyed analogy, but hey. Of course, C++ compiler does a whole lot of magic, but its the psychological effect I'm going for.

  • I use python for "casual" programming. If I want to produce test data, or scan through some strange file, I'd use python (I don't know perl). It is just a very nice, easy-to-use language. Like any scripting language, I suppose, though this one's code is actually readable. Believe it or not, I would use Python second for making readable algorithms, because of that readability.

As a final disclaimer, this is what I think of those languages, not necessarily what they're actually designed for.



Related Topics



Leave a reply



Submit