expression eats newline

Discussion in 'Perl' started by Gerard Oberle, Jul 31, 2003.

  1. 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;'
     
    Gerard Oberle, Jul 31, 2003
    #1
    1. Advertising

  2. Gerard Oberle

    Purl Gurl Guest

    Gerard Oberle wrote:

    (snipped)

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



    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
     
    Purl Gurl, Jul 31, 2003
    #2
    1. Advertising

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

    (Gerard Oberle) wrote in
    news::

    > 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-----
     
    Eric J. Roode, Aug 1, 2003
    #3
  4. Gerard Oberle

    Purl Gurl Guest

    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
     
    Purl Gurl, Aug 1, 2003
    #4
    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. Antonio
    Replies:
    0
    Views:
    424
    Antonio
    Jan 19, 2005
  2. Kevin Spencer

    Re: ASP.NET eats memory too much

    Kevin Spencer, Jun 30, 2003, in forum: ASP .Net
    Replies:
    0
    Views:
    433
    Kevin Spencer
    Jun 30, 2003
  3. George Ter-Saakov

    Re: ASP.NET eats memory too much

    George Ter-Saakov, Jun 30, 2003, in forum: ASP .Net
    Replies:
    0
    Views:
    384
    George Ter-Saakov
    Jun 30, 2003
  4. Jos Vernon

    Re: ASP.NET eats memory too much

    Jos Vernon, Jul 1, 2003, in forum: ASP .Net
    Replies:
    2
    Views:
    380
    Jos Vernon
    Jul 1, 2003
  5. ScottO
    Replies:
    2
    Views:
    529
    ScottO
    Nov 3, 2003
Loading...

Share This Page