Line continuation in Perl

D

David Buchan

Hi guys,

This may seem a trivial/dumb question.

In FORTRAN, if a line gets really long (extending past column 72), you
can put a character in column six of the next line and continue.

In C, you use a backslash and then continue on the next line.

In Perl, it seems, you can make a line really really loooonnnng (at
least, Active Perl doesn't complain). But for the sake of readability,
is there a way to have my long expression chopped-up so that it spans
more than one line? Somebody running Perl on AIX said that you just
continue on the next line without any special characters. Active Perl
didn't seem to keen on that. It said something about interpreting it as
a function.

Any body knowledgeable on this admittedly very minor point?

Thanks,
Dave
 
A

Alan Mead

Star date: Thu, 23 Dec 2004 23:36:10 -0500, David Buchan's log:
In Perl, it seems, you can make a line really really loooonnnng (at
least, Active Perl doesn't complain). But for the sake of readability,
is there a way to have my long expression chopped-up so that it spans
more than one line? Somebody running Perl on AIX said that you just
continue on the next line without any special characters. Active Perl
didn't seem to keen on that. It said something about interpreting it as
a function.

I agree with the AIX guy. Why don't you post the code that caused
problems. Chances are, you are choosing a poor place to use a newline or,
perhaps even more likely, you have a completely unrelated syntax problem
that you are misperceiving.

-Alan
 
J

Jürgen Exner

David Buchan wrote:
[...]
In Perl, it seems, you can make a line really really loooonnnng (at
least, Active Perl doesn't complain). But for the sake of readability,
is there a way to have my long expression chopped-up so that it spans
more than one line?

Perl does not care about line breaks (there are a few exceptions).
Any sequence of white space characters (i.e. space, tab, and newline) is
treated the same way in Perl. That means, wherever you would put a plain
space you could just as well put a newline and some additional space
characters or tabs to make a nice indentation. Just see how people format
their code samples here.

The exceptions I mentioned above are the obvious ones: inside of a string,
inside of numbers, identifiers, or keywords, around closing tag of here
documents (which are just a different style of quoting), and the like.
Somebody running Perl on AIX said that you just
continue on the next line without any special characters.

That is basically correct.
Active Perl
didn't seem to keen on that. It said something about interpreting it
as a function.

Show us your code (actually a minimal example that demonstrates the problem)
and I'm sure we can help you figure out where you went wrong.

jue
 
H

Henry Law

continue on the next line without any special characters. Active Perl
didn't seem to keen on that. It said something about interpreting it as
a function.

A gentle piece of advice. "... said something about ..." doesn't help
anyone to help you, in this group or any other, and hasty people can
get quite irritated if you write it! Please copy and paste the code
you ran, and then copy and paste what ActivePerl actually did say .

This produces the expected output, viz the numbers 0 to 4, one per
line:
-------------------------
# Test Activeperl's line endings

use strict;
use warnings;

for (my $i=0;
$i<
5; $i++
) {
print
"$i\n";
}
 
D

David Buchan

Hi Jurgen,
Show us your code (actually a minimal example that demonstrates the problem)
and I'm sure we can help you figure out where you went wrong.

The code snippet is:

printf (OUTF "%.2f %.2f lineto\n", (($col*93.75)+31.25),(750-($row*
93.75)));

versus

printf (OUTF "%.2f %.2f lineto\n", (($col*93.75)+31.25),
(750-($row*93.75)));

i.e., I start a newline just before the "(750-...")

The message ActivePerl writes is:

"printf (...) interpreted as function at program.pl line 117."

Perhaps this is not an error but rather just an informational message
letting me know that the line was somewhat unusual as compared to the
rest. It just seemed odd that it would give any message at all,
especially if Perl simply ignores whitespace.

Thanks,
Dave
 
D

David Buchan

Hi Bob,
ActiveState Perl (I assume that's what you
mean by "Active Perl") is no different in that regard (or, for
that matter, almost any other regard).

ActivePerl is the name of ActiveState's release of Perl.

Anyway, see snippet of code in response to Jurgen.

I'm now thinking maybe it's not an error message.

Dave
 
T

Tad McClellan

David Buchan said:
Hi Jurgen,


The code snippet is:

printf (OUTF "%.2f %.2f lineto\n", (($col*93.75)+31.25),(750-($row*
93.75)));

versus

printf (OUTF "%.2f %.2f lineto\n", (($col*93.75)+31.25),
(750-($row*93.75)));

i.e., I start a newline just before the "(750-...")

The message ActivePerl writes is:

"printf (...) interpreted as function at program.pl line 117."


Are you saying that one of those makes the message and one of
them does not?

If so, then it looks like a bug in perl to me.

They *both* make the same message for me...

Perhaps this is not an error but rather just an informational message
^^^^^^^

There is no need to guess about messages from perl, just go look
them up in perldiag.pod and *know*.

letting me know that the line was somewhat unusual as compared to the
rest.


perldiag says it is a warning message, not an error message.

It just seemed odd that it would give any message at all,


That seems odd to me too, especially when...

especially if Perl simply ignores whitespace.


.... using "printf(" instead of "printf (" makes the message go away.
 
D

David Buchan

Hi Tad,

Changing from "printf (" to "printf(" got rid of the warning message for
me too. Interesting.

I clearly have lots to learn. Thanks for the perldiag.pod tip. I'm new
to this language and still a bit overwhelmed.

Thanks,
Dave
 
D

David Buchan

Sorry, I forgot to answer your question:

Yes, the first instance works with no warnings:

printf (OUTF "%.2f %.2f lineto\n", (($col*93.75)+31.25),(750-($row*
93.75)));

versus the second, which gives the warning:

printf (OUTF "%.2f %.2f lineto\n", (($col*93.75)+31.25),
(750-($row*93.75)));

i.e., I start a newline just before the "(750-...")

and now the third, which works with no warnings:

printf(OUTF "%.2f %.2f lineto\n", (($col*93.75)+31.25),
(750-($row*93.75)));

The warning message, when I get it, is always:

"printf (...) interpreted as function at program.pl line 117."

Dave
 
J

Jürgen Exner

David said:
Hi Jurgen,


The code snippet is:

printf (OUTF "%.2f %.2f lineto\n", (($col*93.75)+31.25),(750-($row*
93.75)));

versus

printf (OUTF "%.2f %.2f lineto\n", (($col*93.75)+31.25),
(750-($row*93.75)));

i.e., I start a newline just before the "(750-...")

The message ActivePerl writes is:

"printf (...) interpreted as function at program.pl line 117."

Yes, I can reproduce this behaviour.

Just remove the outermost paranthesis, then you won't get that message any
more.
From 'perldoc -f print':
Also be careful not to
follow the print keyword with a left parenthesis [...]

Coming to think of it, get rid of all the paranthesis in that statement.
They are just confusing and none of them is needed.
Perhaps this is not an error but rather just an informational message
letting me know that the line was somewhat unusual as compared to the
rest. It just seemed odd that it would give any message at all,
especially if Perl simply ignores whitespace.

Now, why perl produces this message only if there is a line break I have no
idea. For all I know there is no logical reason for it but I am sure the
experts can shed some light on it.

jue
 
J

Jürgen Exner

Tad said:
Are you saying that one of those makes the message and one of
them does not?

If so, then it looks like a bug in perl to me.

They *both* make the same message for me...

I can reproduce the exact issue (simplified the code a bit more because the
OPs newsreader injected a line break in the first example which if not
removed caused the same message to appear in both cases):

No message:
printf (OUTF "%.2f %.2f", 31.25, 93.75);

Produces said message:
printf (OUTF "%.2f %.2f", 31.25,
93.75);

perl, v5.6.1, Binary build 630 provided by ActiveState

jue
 
J

John W. Kennedy

Bob said:
Really??? I think you're thinking of Unix shell code. C is like Perl
in that a newline is simply another form of whitespace, and a newline
can be used anywhere where other forms of whitespace is syntactically
permitted.

Backslash is used to continue a C preprocessor statement. It also used
to be used to continue a string constant, but ANSI C added a better way.

--
John W. Kennedy
"The bright critics assembled in this volume will doubtless show, in
their sophisticated and ingenious new ways, that, just as /Pooh/ is
suffused with humanism, our humanism itself, at this late date, has
become full of /Pooh./"
-- Frederick Crews. "Postmodern Pooh", Preface
 
M

Michele Dondi

"Somebody" is correct, in Perl1 to Perl5, you can put whitespace between
tokens. Including newlines. (Perl6 will be different, putting whitespace
between certain tokens will change the meaning of your program. Or be
a syntax error. Fun, fun, fun!)

Indeed will be slightly less free-form than we're used to (in some
senses). I know that you're particularly upset by this choice, in fact
I seem to remeber you even wrote something like "Perl6 will pass
Python on the wrong side"; OTOH I think we all have something about
Perl6 that we just can't feel at ease with, but aren't your concerns
just a little bit exaggerated? I mean: yes, you won't be allowed e.g.
to leave whitespace after a sigil or brackets, but you will still be
allowed to do this provided you explicitly put the dereferencer there,
which incidentally happens to be a simple dot.


Just curious,
Michele
 
M

Michele Dondi

No, I'm not exaggerating. I'm talking about a change that will effect
almost *EVERY* line of code I write. And many lines will be effected
multiple times.

But so will, for example the new semantics of sigils and tons of other
things. OTOH you will be able to modify the grammar the way you like
most, which is not that bad after all. (And I bet you will do, BTW!)

Recent developments suggest that most probably another very common
And the problem is, in many cases, putting whitespace there isn't
even a compile error. It just quietly changes the meaning of your
program.

In Perl6:

$var1 = cos (0) * 2; # Equals 1.
$var2 = cos(0) * 2; # Equals 2.

You know, I'm on p6l (but I'm the last arrived there and I've not even
started becoming confident with Perl6's syntax) but I'm not sure about
this. I know that it would be indeed so for methods, but I'm not sure
about functions/subs.

It's hard to say anything definitive on it, though, since Larry is
still actively changing his mind on rather basilar stuff too.


Michele
 
M

Michele Dondi

-: things. OTOH you will be able to modify the grammar the way you like
-: most, which is not that bad after all. (And I bet you will do, BTW!) [snip]
Beside that I don't think it's going to be feasable to change such
significant parsing rules, I don't think it's a smart thing to do, even
if it's possible. If every one is going to create his/her language, with
significant different syntax, it's going to be very hard to cooperate.
Would you want to maintain programs where every author used his/her
language? Would you bother producing patches for CPAN modules if every
author used his/her own language?

This is a serious concern many people share with you. But somehow it
surprises me that you're concerned in the first place, as I though you
were rather inclined to apply patches to current perls and I remember
you wrote quite a few articles about portability not being a thing
about which to be just as mad as some people are.
But then, my retirement will be in less than 25 years. I don't expect
perl6 to ship before that. ;-)

;-)


Michele
 
M

Michele Dondi

Having said that, I fail to see what portability between platforms
has to do with maintaining code.

You're right: my fault, although I would claim that there's some
relationship albeit fundamentally of an indirect nature.


Michele
 

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,763
Messages
2,569,563
Members
45,039
Latest member
CasimiraVa

Latest Threads

Top