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

perl -e 'print x x x + x x x;' outputs nothing... Can anybody shed light on this mystery? thx! j.

Unconsider by theorbtwo, too many keeps to delete, was considered as [DrHyde]: delete, question is stupid, poster did no research, poster deliberately chose meaningless subject. Was at Keep/Edit/Delete: 9/3/5.

Replies are listed 'Best First'.
Re: what gives?
by tall_man (Parson) on Mar 03, 2005 at 00:04 UTC
    Try this:
    perl -e 'print STDOUT x x x + x x x;'
    That prints "0". The first bareword argument to print is being treated as an output file symbol. Is this meant to be an obfuscated/trick question? I don't see why anyone would write this code otherwise.

    Update:I dumped the code as follows,

    > perl -MO=Concise -e 'print x x x + x x x;' c <@> leave[t1] vKP/REFC ->(end) 1 <0> enter ->2 2 <;> nextstate(main 1 -e:1) v ->3 b <@> print vKS ->c 3 <0> pushmark s ->4 5 <1> rv2gv sKR/1 ->6 4 <#> gv s ->5 a <2> repeat[t2] sK/2 ->b 8 <2> repeat[t1] sK/2 ->9 6 <$> const(SPECIAL Null)[t4] s/BARE ->7 7 <$> const(SPECIAL Null)[t5] s/BARE ->8 9 <$> const(SPECIAL Null)[t6] s/BARE ->a -e syntax OK
    I believe the parse is saying: 'Print to file handle "x" the value of global symbol "x" (null) repeated the number of times given by the expression (the value of global symbol "x" (null) repeated "x" (null) times)'.

    Update 2: Interestingly, the following prints "xxx":

    perl -e 'print STDOUT x x + 3;'
    Where is the string value "x" coming from? A bareword symbol must stringify to its own name.
Re: what gives?
by betterworld (Curate) on Mar 03, 2005 at 00:49 UTC
    Next time, please use (at least) warnings:
    perl -we 'print x x x + x x x' ... print() on unopened filehandle x at -e line 1.
      I take this to be an obfuscated perl puzzle rather than merely bad code, so the lack of stricture and warnings is deliberate.

      There are two mysteries: first, why it works at all and second, why it doesn't print. Now that I've looked at it, I think it's fairly clever.

Re: what gives?
by sh1tn (Priest) on Mar 03, 2005 at 00:14 UTC
    "Binary "x" is the repetition operator. In scalar context or if the left
    operand is not enclosed in parentheses, it returns a string consisting
    of the left operand repeated the number of times specified by the right
    operand.
    Binary "+" returns the sum of two numbers."


    "x" will not return anything because it's not given "number of times" so the same but readable:
    print 1 x $anything_not_a_number
    Please, see perldoc perlop.


Re: what gives?
by DrHyde (Prior) on Mar 03, 2005 at 09:38 UTC
    I've downvoted this node and considered it for deletion, on the grounds that:
    • the question is stupid;
    • the poster would seem not to have done any research to answer the question himself
    • the poster deliberately chose a meaningless title
    Let me explain those. First of all, trying to print a meaningless string of operators *with nothing to operate on* is clearly not going to do anything useful. The exact mechanics of why it does nothing as opposed to blowing up in your face will become clear once a trivial amount of diagnostics are performed, such as by adding -Mwarnings

    Second, it is good practice when asking people to help you for free to at least tell them how you have tried to help yourself. For example, you could say "I tried turning on warnings but I don't understand all that stuff about unquoted strings".

    Third, "what gives" gives potential readers precisely no clue as to what you are asking about. For all I knew before looking, you might have been about to ask why your algorithm for calculating the length of great circle routes wasn't working. I consider nodes to have their titles changed quite often, but at least in those cases the poster has *tried* to choose a good title, and I can only think of a better one because I understand the problem better. You, however, chose to deliberately obfuscate. That to me indicates that you're not really serious about finding out "what gives".

Re: what gives?
by manav (Scribe) on Mar 03, 2005 at 05:48 UTC
    perldoc -f print lists the print syntax as
    print FILEHANDLE LIST
    Hence, in your case, it takes the first argument as the FILEHANDLE to write to which it doesnt find. As warnings pragma is not 'use'd, the script silently errors out.

    Remember, whitespace is not a list argument seperator in Perl, comma is. (in list context obviously ;) )

    Manav