expression eats newline

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Gerard Oberle

    expression eats newline

    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;'
  • Purl Gurl

    #2
    Re: expression eats newline

    Gerard Oberle wrote:

    (snipped)
    [color=blue]
    > The following Perl script[/color]
    [color=blue]
    > print ((time/86400) % 4), " foo foo foo foo\n";
    > print "bar\n";[/color]
    [color=blue]
    > produces the output[/color]
    [color=blue]
    > 0bar[/color]
    [color=blue]
    > What happened to " foo foo foo foo\n"?[/color]


    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

    Comment

    • Eric J. Roode

      #3
      Re: expression eats newline

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

      goberle@hotmail .com (Gerard Oberle) wrote in
      news:c4d7e237.0 307310725.45258 a70@posting.goo gle.com:
      [color=blue]
      > The following Perl script
      >
      > print ((time/86400) % 4), " foo foo foo foo\n";[/color]

      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/AwUBPynqOmPeouI eTNHoEQIu7ACghr gjRzXwWjijpWA+i hwOUKSSZQEAnRAr
      dlcsK2oa9GH1M3p 1WYE8AUVW
      =2btu
      -----END PGP SIGNATURE-----

      Comment

      • Purl Gurl

        #4
        Re: expression eats newline

        Gerard Oberle wrote:

        (Topic is use of parentheses with Perl print)

        (snipped)
        [color=blue]
        > As I suspected, I was missing something stupid.[/color]

        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.

        [color=blue]
        > As for comp.lang.perl being defunct, I am not sure how one can
        > determine that. groups.google.c om still carries it, and there are
        > lots of recent postings, so I assume people are still using it.[/color]

        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

        Comment

        Working...