expression eats newline

G

Gerard Oberle

The following Perl script

print ((time/86400) % 4), " foo foo foo foo\n";
print "bar\n";

produces the output

0bar

What happened to " foo foo foo foo\n"?

print 12, " foo\n";

prints '12 foo' and a newline. Ergo, ((time/86400) % 4) should just
be another numeric expression, just like 12, shouldn't it? Or am I
missing something really stupid?

Thanks in advance.
- Jerry Oberle
perl -e 'printf "mailto%c%s%c%s%cchase%ccom%c", 58, "Gerard", 46,
"Oberle", 64, 46, 10;'
 
P

Purl Gurl

Gerard Oberle wrote:

(snipped)
The following Perl script
print ((time/86400) % 4), " foo foo foo foo\n";
print "bar\n";
produces the output

What happened to " foo foo foo foo\n"?


You are neglecting Perl command formats, which are
typically, command() as with print().

Your parentheses are the problem. Perl is doing
precisely what your syntax indicates to do.

Compare these to your syntax:

print time/86400 % 4, " foo foo foo foo\n";
print "bar\n";

print (((time/86400) % 4), " foo foo foo foo\n");
print "bar\n";


Purl Gurl
--

#!perl -w

print ((time/86400) % 4), " foo foo foo foo\n";
print "bar\n";


PRINTED RESULTS:
________________

print (...) interpreted as function at test.pl line 3.
Useless use of a constant in void context at test.pl line 3.
0bar
 
E

Eric J. Roode

-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

(e-mail address removed) (Gerard Oberle) wrote in
The following Perl script

print ((time/86400) % 4), " foo foo foo foo\n";

Perl has a rule when it comes to interpreting statements like this:

"if it looks like a function, it IS a function."

So, because of your parentheses, it is as if you wrote:

$x = print( (time/86400)%4 );
$x, " foo foo foo foo\n";

The solution is either to add a + before the first parenthesis:

print +((time/86400) % 4), " foo foo foo foo\n";

or to add another set of parentheses:

print (((time/86400) % 4), " foo foo foo foo\n");


For your future reference, comp.lang.perl is a defunct newsgroup. General
perl questions should be posted to comp.lang.perl.misc; you'll get a better
response there.
- --
Eric
$_ = reverse sort qw p ekca lre Js reh ts
p, $/.r, map $_.$", qw e p h tona e; print

-----BEGIN PGP SIGNATURE-----
Version: PGPfreeware 7.0.3 for non-commercial use <http://www.pgp.com>

iQA/AwUBPynqOmPeouIeTNHoEQIu7ACghrgjRzXwWjijpWA+ihwOUKSSZQEAnRAr
dlcsK2oa9GH1M3p1WYE8AUVW
=2btu
-----END PGP SIGNATURE-----
 
P

Purl Gurl

Gerard Oberle wrote:

(Topic is use of parentheses with Perl print)

(snipped)
As I suspected, I was missing something stupid.

Not really stupid. It is very rare to read Perl code
which employs C style parentheses with a print command.
Almost all books and reference sources omit parentheses
within examples of print commands. Most do not know of
this until a rather quirky problem pops up.

As for comp.lang.perl being defunct, I am not sure how one can
determine that. groups.google.com still carries it, and there are
lots of recent postings, so I assume people are still using it.

Many if not most news servers carry this group and alt.perl as well.
Humors me when reading those proclaiming this group does not exist.
Quite the oxymoron, this is, posting to a group which does not exist.


Purl Gurl
 

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

Forum statistics

Threads
473,755
Messages
2,569,535
Members
45,007
Latest member
obedient dusk

Latest Threads

Top