Beefy Boxes and Bandwidth Generously Provided by pair Networks
go ahead... be a heretic
 
PerlMonks  

breaking long lines in my code

by idunno (Beadle)
on Feb 07, 2002 at 17:35 UTC ( [id://143933]=perlquestion: print w/replies, xml ) Need Help??

idunno has asked for the wisdom of the Perl Monks concerning the following question:

I have some long lines in my code that I want to break for formatting reasons. I'd also like all lines but the first to be tabbed over. Can I do this without affecting the way my code works?

i.e. Can I make

print "Here's a long line blah blah blah that I would like broken up\n";
output the same as this?
print "Here's a long line blah blah blah that I would like broken up\n +";

Replies are listed 'Best First'.
Re: breaking long lines in my code
by dvergin (Monsignor) on Feb 07, 2002 at 17:41 UTC
    No. You can do that, but it will effect the printed output. And you are asking if you can do it without changing the output.

    But ++ for the motivation to format your code for readability.

    There are many, many situations where you can break a line of code in the middle without hurting things. I would go so far as to say you can do it in most cases. (White space is white space: \n is as good as \s or \t.) And inserting a newline here will not break the code, but it does change the output. So...

    print "Here's a long line blah blah " . "blah that I would like broken up\n"; #or print "Here's a long line blah blah ", "blah that I would like broken up\n";
    Note here that the comma works specifically because of "print"s ability to handle a series of values. But comma will not work for $somevar = "...". For that, the dot concatenation approach does the job.
Re: breaking long lines in my code
by Purdy (Hermit) on Feb 07, 2002 at 17:54 UTC
    While perltidy doesn't break up a wide/long string (as far as I know), I find perltidy a neat program/utility to clean up code, making it more readable.

    Jason

(crazyinsomniac) Re: breaking long lines in my code
by crazyinsomniac (Prior) on Feb 08, 2002 at 02:24 UTC
    Potentially dangerous (untested)...
    perl -i.bak -ne's{^print\s\"(.*?)\"\;$}{my(@str)=split(/(.{76})/,$1); +"print ".join(",\n\t",map(qq,"$_",,@str)).";"}ge;print' file
    update
    #!/usr/bin/perl -wT use strict; while(<DATA>) { chomp; if( m{^(?:print\s\"(.*)\"\;)$}g ) { splittor($1); next; } print $_."\n"; } exit; sub splittor { my $str = shift; # my ( @str ) = ( $str ) =~ m/(.{1,60})/gs; my ( @str ) = grep $_, split /(.{1,60})/, $str; print 'print '; print join ( ",\n\t", map {'"'.$_.'"'} @str ); print ';'."\n"; return undef; } __END__ print "I am smelling like a rose that somebody gave me on my birthday +death bed, I am smelling like a rose that somebody gave me ... yeah!\ +n"; print "123456789123456789123456789123456789123456789123456789123456789 +123456789123456789\n."; ###### this is what the output shoud look like (does look like) print "I am smelling like a rose that somebody gave me on my birthd", "ay death bed, I am smelling like a rose that somebody gave m" +, "e ... yeah!\n"; print "123456789123456789123456789123456789123456789123456789123456", "789123456789123456789\n.";

     
    ______crazyinsomniac_____________________________
    Of all the things I've lost, I miss my mind the most.
    perl -e "$q=$_;map({chr unpack qq;H*;,$_}split(q;;,q*H*));print;$q/$q;"

      Ah, but what if the print statement's indented?
      #!/usr/bin/perl -wT use strict; while(<DATA>) { chomp; if( m{^(\s+)(?:print\s\"(.*)\"\;)$}g ) { splittor($1,$2); next; } print $_."\n"; } exit; sub splittor { my $indent = shift || ''; my $str = shift; # quick hack for accurate count my $tabcount=0; $tabcount++ for ($indent =~ /\t/g); # assuming tab == 4 spaces my $maxlength = 60-length($indent)-($tabcount*3); my ( @str ) = grep $_, split /(.{1,$maxlength})/, $str; print "${indent}print "; print join ( ",\n$indent\t", map {'"'.$_.'"'} @str ); print ';'."\n"; return undef; }
      or something like that (untested).

      cLive ;-)

Re: breaking long lines in my code
by Parham (Friar) on Feb 08, 2002 at 05:07 UTC
    i think the concatination method is the most elegant.
    print "this is line 1, " . "this is line 2, " . "this is line 3";
    would print: this is line 1, this is line 2, this is line 3 all on one single line. It breaks down your code very well without messing up your output.
      I agree that concatenation is the best way to go (to handle scalars as well as prints, CGI.pm parameters, etc.). I found that, to get emacs to indent properly in circumstances like this, it worked best to surround the entire string in a NOP function call:

      print nop("this is line 1, " . "this is line 2, " . "this is line 3");

      In this case, nop() is as simple as it looks:

      sub nop { return join("", @_) }

      If anyone has a better way of handling the auto-indenting problem, I'm open to it.

      - Richie

Re: breaking long lines in my code
by reclaw (Curate) on Feb 08, 2002 at 03:08 UTC

    I'm not sure this is what you want, but it works:

    print "Here's a long line blah blah "; print "blah that I would like broken up\n";
Re: breaking long lines in my code
by m0ren0 (Initiate) on Feb 08, 2002 at 00:11 UTC
    use the wonderful qq ?

    print qq{ Hello...

    what is my...

    name ??

    };

    yes.
    do that.
    rod.
      This, unfortunately, does not solve the problem, since it acts just like 'double-quotes' (at least as far as line breaks are concerned).

      Impossible Robot
Re: breaking long lines in my code
by strat (Canon) on Feb 08, 2002 at 13:42 UTC
    Something you could do is the following, because print also accepts a list:
    print qw(Here's a long line blah blah blah that I would like broken up), "\n";
    But if there are commas inside, some warnings might appear

    Best regards,
    perl -le "s==*F=e=>y~\*martinF~stronat~=>s~[^\w]~~g=>chop,print"

Re: breaking long lines in my code
by dmmiller2k (Chaplain) on Feb 08, 2002 at 18:06 UTC

    Still another approach, one that I prefer:

    print join(" ", "Here's a long line blah blah", "blah that I would like broken up\n" );

    This has a modest performance penalty over the original version (the join()) but has the distinct advantage of being nicely formattable by syntax editors (such as XEmacs, which I use).

    The same technique can be used to coalesce a series of separate print statements into one, which is how I use it mostly, to erduce the number of calls to print(), on the theory that IO statements are more expensive than memory-based ones (particularly when autoflush in enabled, e.g., $|=1, as is often the case with CGI scripts).

    print join("\n", "This is line 1", "This is line 2", "This is line 3", # etc. "This is line n"), "\n";

    dmm

    If you GIVE a man a fish you feed him for a day
    But,
    TEACH him to fish and you feed him for a lifetime
Re: breaking long lines in my code
by mrmick (Curate) on Feb 07, 2002 at 18:24 UTC
    It saddens me to see that you didn't try to see for yourself simply by comparing the output of the two statements.

    Mick
      I did compare the output. That's why I'm asking if there's something I don't know about that I can change or add to my code.
      It saddens me to see someone would actually assume the original poster is that much of an idiot. Must be a big horse.
Re: breaking long lines in my code
by Anonymous Monk on Feb 10, 2002 at 01:25 UTC
    Greetings,

    Would an underscore at the end help?
    ie:
    print "yada yada _
    yada yada";

    ~lurch

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://143933]
Approved by root
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others rifling through the Monastery: (5)
As of 2024-04-23 18:59 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found