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

Discussion in 'Perl Misc' started by usenet@DavidFilmer.com, Oct 20, 2005.

  1. Guest

    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?
     
    , Oct 20, 2005
    #1
    1. Advertising

  2. Matt Garrish Guest

    <> wrote in message
    news:...
    > 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
     
    Matt Garrish, Oct 20, 2005
    #2
    1. Advertising

  3. Matt Garrish wrote:
    > <> wrote in message
    > news:...
    >
    >>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
     
    Josef Moellers, Oct 20, 2005
    #3
  4. Dave Weaver Guest

    Matt Garrish <> wrote:
    >
    > <> wrote in message
    > news:...
    > > 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'
     
    Dave Weaver, Oct 20, 2005
    #4
  5. thundergnat Guest

    Matt Garrish wrote:
    > <> wrote in message
    > news:...
    >
    >>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#;"
     
    thundergnat, Oct 20, 2005
    #5
  6. Guest

    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.
     
    , Oct 20, 2005
    #6
  7. Ala Qumsieh Guest

    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
     
    Ala Qumsieh, Oct 20, 2005
    #7
  8. Anno Siegel Guest

    Ala Qumsieh <> wrote in comp.lang.perl.misc:
    > 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.
     
    Anno Siegel, Oct 20, 2005
    #8
  9. Guest

    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
     
    , Oct 20, 2005
    #9
  10. Guest

    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?
     
    , Oct 20, 2005
    #10
  11. Guest

    wrote:
    > 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.
     
    , Oct 20, 2005
    #11
  12. Anno Siegel Guest

    <> wrote in comp.lang.perl.misc:
    > 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;


    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
    --
    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.
     
    Anno Siegel, Oct 20, 2005
    #12
  13. Matt Garrish Guest

    "Dave Weaver" <> wrote in message
    news:43574b69$0$21316$...
    > Matt Garrish <> wrote:
    >>
    >> 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'
    >


    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
     
    Matt Garrish, Oct 21, 2005
    #13
  14. In article <>, says...
    >
    >
    >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

    --
    Heinrich Mislik
    Zentraler Informatikdienst der Universitaet Wien
    A-1010 Wien, Universitaetsstrasse 7
    Tel.: (+43 1) 4277-14056, Fax: (+43 1) 4277-9140
     
    Heinrich Mislik, Oct 21, 2005
    #14
  15. John Bokma Guest

    (Heinrich Mislik) wrote:

    > In article <>,
    > says...
    >>
    >>
    >>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.


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

    --
    John Small Perl scripts: http://johnbokma.com/perl/
    Perl programmer available: http://castleamber.com/
    I ploink googlegroups.com :)
     
    John Bokma, Oct 21, 2005
    #15
  16. Guest

    On 19 Oct 2005 17:30:00 -0700, 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.
    >
    >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?
     
    , Oct 30, 2005
    #16
  17. Guest

    wrote:
    > 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?"
     
    , Oct 30, 2005
    #17
  18. Guest

    On 30 Oct 2005 00:54:09 -0700, wrote:

    > wrote:
    >> 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?"

    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.....
     
    , Oct 31, 2005
    #18
  19. wrote:

    > 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.
     
    William Herrera, Oct 31, 2005
    #19
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Steve C. Orr, MCSD
    Replies:
    1
    Views:
    595
    reaway lee
    Aug 24, 2003
  2. Puneet
    Replies:
    16
    Views:
    1,159
    Daniel Vallstrom
    Mar 20, 2005
  3. Jeremy McAnally
    Replies:
    0
    Views:
    89
    Jeremy McAnally
    Jan 3, 2011
  4. Replies:
    9
    Views:
    193
  5. Alberto
    Replies:
    9
    Views:
    131
Loading...

Share This Page