in reply to Why does this code think I'm trying to use symbolic references?

print $n ... expects $n to contain a file handle. 1000 is not a file handle and "strict refs" forbids using the string to reference a variable. Consider:

#! perl my $n = 'STDOUT'; print $n "Now we's happy\n"; use strict; print $n "Now we's not\n";

Prints:

Now we's happy Can't use string ("STDOUT") as a symbol ref while "strict refs" in use + at ...\noname.pl line 8.
Premature optimization is the root of all job security

Replies are listed 'Best First'.
Re^2: Why does this code think I'm trying to use symbolic references?
by BrowserUk (Patriarch) on Jun 12, 2016 at 22:25 UTC
    print $n ... expects $n to contain a file handle.

    But print $n + 4; doesn't; and that was my intent. And in (almost, if not) all other circumstances $n +4 and $n + 4 are equivalent, so It just didn't click even after I found that adding another space fixed the problem.

    As I'm normally quite prodigious with my horizontal whitespace, it's just not something I've encountered before.


    With the rise and rise of 'Social' network sites: 'Computers are making people easier to use everyday'
    Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
    "Science is about questioning the status quo. Questioning authority". I knew I was on the right track :)
    In the absence of evidence, opinion is indistinguishable from prejudice. Not understood.
      ... I'm normally quite prodigious with my horizontal whitespace ...

      Either prodigality or parsimony seem to work; e.g.,  print $n+4; also avoids the problem. Apparently it's only  $n +4 that Perl looks at and says "Oh, of course: a unary plus!" (I haven't tested it, but I assume the same would happen | I've tested it, and the same thing happens with a pseudo-unary minus.)

      Update: Per perlop, unary + and - have higher precedence than the binary operators.


      Give a man a fish:  <%-{-{-{-<