Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Perl > Perl Misc > Can (nearly) any Perl program be written on a single line?

Reply
Thread Tools

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

 
 
usenet@DavidFilmer.com
Guest
Posts: n/a
 
      10-20-2005
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?

 
Reply With Quote
 
 
 
 
Matt Garrish
Guest
Posts: n/a
 
      10-20-2005

<(E-Mail Removed)> wrote in message
news:(E-Mail Removed) oups.com...
> 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


 
Reply With Quote
 
 
 
 
Josef Moellers
Guest
Posts: n/a
 
      10-20-2005
Matt Garrish wrote:
> <(E-Mail Removed)> wrote in message
> news:(E-Mail Removed) oups.com...
>
>>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).


You mean, David couldn't write

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

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

--
Josef Möllers (Pinguinpfleger bei FSC)
If failure had no penalty success would not be a prize
-- T. Pratchett

 
Reply With Quote
 
Dave Weaver
Guest
Posts: n/a
 
      10-20-2005
Matt Garrish <(E-Mail Removed)> wrote:
>
> <(E-Mail Removed)> wrote in message
> news:(E-Mail Removed) oups.com...
> > 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.


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.


> > 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).
>


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



 
Reply With Quote
 
thundergnat
Guest
Posts: n/a
 
      10-20-2005
Matt Garrish wrote:
> <(E-Mail Removed)> wrote in message
> news:(E-Mail Removed) oups.com...
>
>>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
>
>


perl -e "$hash{'#'} = 'foo'; print '#####' if $hash{'#'} =~ s#foo#bar#;"
 
Reply With Quote
 
usenet@DavidFilmer.com
Guest
Posts: n/a
 
      10-20-2005
Dave Weaver wrote:
> 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.

 
Reply With Quote
 
Ala Qumsieh
Guest
Posts: n/a
 
      10-20-2005
http://www.velocityreviews.com/forums/(E-Mail Removed) wrote:
> 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
 
Reply With Quote
 
Anno Siegel
Guest
Posts: n/a
 
      10-20-2005
Ala Qumsieh <(E-Mail Removed)> wrote in comp.lang.perl.misc:
> (E-Mail 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
--
If you want to post a followup via groups.google.com, don't use
the broken "Reply" link at the bottom of the article. Click on
"show options" at the top of the article, then click on the
"Reply" at the bottom of the article headers.
 
Reply With Quote
 
jl_post@hotmail.com
Guest
Posts: n/a
 
      10-20-2005
(E-Mail Removed) wrote:
>
> 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

 
Reply With Quote
 
usenet@DavidFilmer.com
Guest
Posts: n/a
 
      10-20-2005
Ala Qumsieh wrote:
> 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?

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Can parellelized program run slower than single process program? Yesterday Paid Python 1 06-21-2012 05:58 AM
Generally, are the programs written by C++ slower than written by C10% ? KaiWen C++ 102 09-14-2011 11:12 PM
501 PIX "deny any any" "allow any any" Any Anybody? Networking Student Cisco 4 11-16-2006 10:40 PM
ID3 Tags written by Perl can not be modified by any other ID3 editor ads@theboohers.org Perl Misc 9 12-21-2005 01:40 PM
Re: Can a usercontrol written in C# be used in Web Forms that is written in VB.Net? Steve C. Orr, MCSD ASP .Net 1 08-24-2003 12:06 AM



Advertisments