Can (nearly) any Perl program be written on a single line?

U

usenet

Not that I'm advocating this in any way, of course, but I'm just
curious...

I believe that nearly any Perl script can be written on a single line
(without comments, of course). I'm thinking here of "executable" Perl
code, not stuff in __DATA__, etc.

The only exceptions I can think of are "here" documents and formats
(both of which I tend to avoid).

Are there other programming exceptions that would "force" the use of
more than one line?
 
M

Matt Garrish

Not that I'm advocating this in any way, of course, but I'm just
curious...

I believe that nearly any Perl script can be written on a single line
(without comments, of course). I'm thinking here of "executable" Perl
code, not stuff in __DATA__, etc.

The only exceptions I can think of are "here" documents and formats
(both of which I tend to avoid).

Are there other programming exceptions that would "force" the use of
more than one line?

You could never have an octothorp/number sign/hash character anywhere in
your code (not just a comment).

Matt
 
J

Josef Moellers

Matt said:
You could never have an octothorp/number sign/hash character anywhere in
your code (not just a comment).

You mean, David couldn't write

foreach (@ARGV) { print '#'; }

compiles and executes perfectly, yet has a hash in the code.
 
D

Dave Weaver

Why on earth would you want to do that?

I don't understand some peoples' pre-occupation with squashing code
down to as few lines as possible. Making the program shorter like this
will generally make little difference to memory/disk usage or
execution time, but will have a huge (negative) impact on its
readability and hence maintainability.

It's fine for a JAPH, or golf, but otherwise my advice would be to
write your program in a clear, readable fashion, making good use of
white space and appropriate comments.

You could never have an octothorp/number sign/hash character anywhere in
your code (not just a comment).

perl -e '$_="yes#you#could\n"; s#y#Y#; print'
 
T

thundergnat

Matt said:
You could never have an octothorp/number sign/hash character anywhere in
your code (not just a comment).

Matt

perl -e "$hash{'#'} = 'foo'; print '#####' if $hash{'#'} =~ s#foo#bar#;"
 
U

usenet

Dave said:
Why on earth would you want to do that?

I wouldn't, of course. In fact, I do my level best to not let any line
exceed 70 characters in length (an old hang-up from my days pounding
out FORTRAN on 80x24 greenscreen mainframe terminals, but still
generally good practice, IMHO).

But that doesn't stop me from wondering about the structure,
capabilities, and limitations of Perl. I like to study obfuscated
code, also (even though I would never program that way!). I'm a
curious type of guy.
 
A

Ala Qumsieh

Not that I'm advocating this in any way, of course, but I'm just
curious...

I believe that nearly any Perl script can be written on a single line
(without comments, of course). I'm thinking here of "executable" Perl
code, not stuff in __DATA__, etc.

By "single line" you mean a single Perl statement?
I don't think that's possible, except for the simplest of programs.
The only exceptions I can think of are "here" documents and formats
(both of which I tend to avoid).

Why? Generally, I do avoid formats because I don't really need them. But
in some cases, they are extremely useful.

As for heredocs, I don't see a reason to avoid them. If you want clearer
multi-line print statements, go with heredocs.

--Ala
 
A

Anno Siegel

Ala Qumsieh said:
(e-mail address removed) wrote:
[...]
I believe that nearly any Perl script can be written on a single line
(without comments, of course). I'm thinking here of "executable" Perl
code, not stuff in __DATA__, etc.

By "single line" you mean a single Perl statement?
I don't think that's possible, except for the simplest of programs.

Why not? Just wrap do { ... } around the whole thing.

:)

Anno
 
J

jl_post

I do my level best to not let any line
exceed 70 characters in length (an old
hang-up from my days pounding out FORTRAN
on 80x24 greenscreen mainframe terminals,
but still generally good practice, IMHO).

But that doesn't stop me from wondering
about the structure, capabilities, and
limitations of Perl. I like to study
obfuscated code, also (even though I
would never program that way!). I'm a
curious type of guy.


I may not be remembering correctly, but I think I remember reading
in the first edition "Programming Perl" book that Perl doesn't place
limitations like arrays, strings, and lines of code can be no longer
than some mysterious power of two in length. In other words, a line of
code won't be incorrect just because it exceeds some predetermined
length. Perl will attempt to handle all code, strings, and arrays of
arbitrary length provided that it has enough memory to do so.

Is this true? Well, let's just say that I've never proven this
wrong. If you want my opinion, you'll probably never have to worry
about having a line that's too long for the Perl interpreter, so don't
worry about any line of code length limit. (But keep your good
practice of not letting any (or most) lines exceed 70 characters in
length.)

-- Jean-Luc
 
U

usenet

Ala said:
By "single line" you mean a single Perl statement?

No. I mean if I have a 10,000 line Perl program (which follows perlstyl
conventions and suggestions, except no comments), and I
s/\n//g;
on the whole program, what sorts of constructs might it contain which
would cause it not to work exactly as before? (besides formats and
heredocs)?

I guess another way of asking it would be this: Are there Perl
constructs BESIDES formats and heredocs which care about the presence
of newlines within the code?
 
U

usenet

If you want my opinion, you'll probably never have to worry
about having a line that's too long for the Perl interpreter

I wasn't actually thinking about whether I might overwhelm the
interpreter (I think that's doubtful). I was wondering what sorts of
Perl statements (besides formats and heredocs) wouldn't work as
expected/desired if a program had no newlines in it.
 
A

Anno Siegel

No. I mean if I have a 10,000 line Perl program (which follows perlstyl
conventions and suggestions, except no comments), and I
s/\n//g;

You want

s/\n/ /g; # or rather tr/\n/ /

Newlines *are* significant in Perl just like other white space.

Newlines in literals (strings, regexes etc.) would have to be
exempt from the substitution, otherwise the program behavior can change
in unpredictable ways.
on the whole program, what sorts of constructs might it contain which
would cause it not to work exactly as before? (besides formats and
heredocs)?

I guess another way of asking it would be this: Are there Perl
constructs BESIDES formats and heredocs which care about the presence
of newlines within the code?

The __END__ and __DATA__ tokens are only recognized at the beginning of
a line. Lines that begin with "=" are special in support of POD-related
constructs (=head1, =cut etc). The value of the __LINE__ token would
change.

There are lots of places in Perl code where a newline is not equivalent
to other white space. Trying to list them all is of little interest.

Anno
 
M

Matt Garrish

Dave Weaver said:
perl -e '$_="yes#you#could\n"; s#y#Y#; print'

I'm putting a mental block on yesterday. Just about everything I wrote came
out stupid. I honestly can't even remember why I posted that when it's so
obviously wrong. The next time I'm sorely lacking in sleep I'm staying away
from usenet...

Matt
 
H

Heinrich Mislik

Not that I'm advocating this in any way, of course, but I'm just
curious...

I believe that nearly any Perl script can be written on a single line
(without comments, of course). I'm thinking here of "executable" Perl
code, not stuff in __DATA__, etc.

The only exceptions I can think of are "here" documents and formats
(both of which I tend to avoid).

Are there other programming exceptions that would "force" the use of
more than one line?

Try "use Switch" on a one-liner:

----------------------
#!/bin/perl

use Switch;
switch($ARGV[0]){case 1 {print 1}}

#Does work.
----------------------
#!/bin/perl

use Switch;switch($ARGV[0]){case 1 {print 1}}

#Won't compile.
----------------------
I don't know what is behind this. There may be other modules showing the same behaviour.

Cheers

Heinrich
 
J

John Bokma

Not that I'm advocating this in any way, of course, but I'm just
curious...

I believe that nearly any Perl script can be written on a single line
(without comments, of course). I'm thinking here of "executable" Perl
code, not stuff in __DATA__, etc.

The only exceptions I can think of are "here" documents and formats
(both of which I tend to avoid).

Are there other programming exceptions that would "force" the use of
more than one line?

Try "use Switch" on a one-liner:

----------------------
#!/bin/perl

use Switch;
switch($ARGV[0]){case 1 {print 1}}

#Does work.
----------------------
#!/bin/perl

use Switch;switch($ARGV[0]){case 1 {print 1}}

#Won't compile.

IIRC Switch has some warnings in its documentation. IIRC, because it
tries to simulate expected Perl 6 behaviour using source filter.
 
R

robic0

Not that I'm advocating this in any way, of course, but I'm just
curious...

I believe that nearly any Perl script can be written on a single line
(without comments, of course). I'm thinking here of "executable" Perl
code, not stuff in __DATA__, etc.

The only exceptions I can think of are "here" documents and formats
(both of which I tend to avoid).

Are there other programming exceptions that would "force" the use of
more than one line?

It would appear that you would have to parse and track
Perl language escaped sequences for embedded (in-solution),
control codes. Outside the sequence they can be chopped.
Do you plan on distributing a single line of code?
Or making a binary out of it?
 
U

usenet

Do you plan on distributing a single line of code?
Or making a binary out of it?

Of course not. As I said, I'm just curious. I like to understand the
capabilities and limitations of a programming language. When the docs
say things like "Perl _generally_ doesn't care about whitespace or line
breaks," I start thinking, "hmmm - well, exactly when _does_ Perl care
about these things?"
 
R

robic0

Of course not. As I said, I'm just curious. I like to understand the
capabilities and limitations of a programming language. When the docs
say things like "Perl _generally_ doesn't care about whitespace or line
breaks," I start thinking, "hmmm - well, exactly when _does_ Perl care
about these things?"
David,
You should have a "intutive" sense of "in solution" control codes.
Control codes (escaped) take care of themselves "in solution".
By that I mean they are in balance when in variables and not
in sourse. Once, twice, 3 times removed. But when "in solution"
(a chemistry phrase) there is no worry. They can be brought
"out of solution", then thats a problem.

"Do you plan on distributing a single line of code?
Or making a binary out of it?"

Some that use Perl2Exe worry about this. I think its possible
the source can be extracted from the binary.
I use P2E. It was just a question, no harm intended.
Peace.....
 
W

William Herrera

say things like "Perl _generally_ doesn't care about whitespace or line
breaks," I start thinking, "hmmm - well, exactly when _does_ Perl care
about these things?"

here-docs expect multiple lines to work.
 

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
474,175
Messages
2,570,946
Members
47,497
Latest member
PilarLumpk

Latest Threads

Top