the antichomp

A

Anno Siegel

Uri Guttman said:
w> I would not thought of it myself (the use of map), but now I will.
w> Thanks :)

map is easy but for some reason freaks out many perl newbies.

Well, it's "functional programming" which is supposedly hard. Some
languages do make it hard, requiring things like "pure functions"
defined by "lambda expressions" to feed to things like map().

Perl uses coderefs and, more importantly, automatically translates
some blocks and expressions to coderefs, so functional programming
introduces no new syntax. It's a successful case of making easy
things easy.

Anno
 
A

Arndt Jonasson

Well, it's "functional programming" which is supposedly hard. Some
languages do make it hard, requiring things like "pure functions"
defined by "lambda expressions" to feed to things like map().

Perl uses coderefs and, more importantly, automatically translates
some blocks and expressions to coderefs, so functional programming
introduces no new syntax. It's a successful case of making easy
things easy.

One might want recursion, but deep recursion does not work well in
perl, not even tail recursion (at least not in 5.005 - I haven't checked
later versions).
 
T

Tassilo v. Parseval

Also sprach Arndt Jonasson:
One might want recursion, but deep recursion does not work well in
perl, not even tail recursion (at least not in 5.005 - I haven't checked
later versions).

Tail recursion in fact works very well. The only problem with it is that
perl is not smart enough to detect those cases automatically so you have
to write your own by assigning to @_ and using 'goto &func'.

Tassilo
 
W

wana

Tad McClellan said:
If you want to "transform a list", use map().

If you want to "filter a list", use grep().

These are going to be the functions of the week for me. I will try to
use them in a Perl sentence at least once a day.

wana - trying to build on limited Perl vocabulary.
 
T

Tad McClellan

Anno Siegel said:
Well, it's "functional programming" which is supposedly hard.


.... but really isn't (well, _this part_ of func programming isn't hard).

I present it to students like this:

The return value(s) of a function serves as arguments to some
other function, and the return value(s) from that function
serve as arguments to yet another function... as deep
as you want to go.

You read them "backwards", from right to left. For example:

print sort <>;

<> supplies a list of all the lines from all the files as
arguments to sort().

sort() supplies a list of all the rearranged list elements as
arguments to print().

and print() prints them.
 
J

J. Romano

Is there a better way to it than this?

$_ .= "\n" for @ARGV;


This may or may not be of use to you, but it might be handy to
learn about Perl's "-l" switch.

If a Perl script begins with a line like:

#!/usr/bin/perl -l

(or if a script is invoked with "perl -l script.pl"), then two things
will happen:

1. The newline will be automatically stripped from any
input read with the <...> operator.

2. The newline will automatically be printed with every
call to print().

This switch is handy if anyone wants to, say, add a certain string
to the end of lines of input, but before their newlines.

For example, let's say that you wanted to add "!!!" to the end of
each line of input that you process (but before the newline). A
programmer who is not aware of the "-l" switch might write a script
like this:

#!/usr/bin/perl
use strict;
use warnings;
while (<>)
{
chomp; # remove the newline
$_ .= "!!!"; # append the "!!!" string
print "$_\n"; # print the line with a newline
}
__END__

or perhaps as a one-liner, like this:

perl -pe 'chomp; $_ .= "!!!\n"'

In each case, the programmer has to explicitly remove the newline
(done here with chomp()) and add it back in later.

However, a programmer who knows about the "-l" switch might write a
script like this:

#!/usr/bin/perl -l
use strict;
use warnings;
while (<>)
{
$_ .= "!!!"; # append the "!!!" string
print;
}
__END__

or perhaps as a one-liner, like this:

perl -lpe '$_ .= "!!!"'

Of course, this switch works well only if you don't mind that the
newline is always stripped out or that the newline is always printed
with the print() function. So don't use it if this poses a problem
for you.

I hope what I said was clear.

-- Jean-Luc
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,483
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top