http://qs321.pair.com?node_id=119595


in reply to Real World 1, Great Expectations 0

Your error message indicates that you had print $handles[$i] "Hi how the hell are ya!";, not what you copied here, so I am going to assume that is what you meant to copy. (And copying-and-pasting in the future will prevent transcription mistakes like this.)

Your problem is caused by an odd ambiguity of the "indirect object" syntax that print uses. That is, when you say:

something $array[$i]
would it mean
something {$array} [$i]
or
something {$array[$i]}
(where the {} can be used to set off the actual "object" you want to act on)? To resolve this ambiguity without incuring too much lookahead, perl treats it like:
something {$array} [$i]
You do have arguments after it, so concievably perl could disambiguite:
something $array[$i] $something_else
into:
something {$array[$i]} $something_else
but it doesn't try to look that far ahead, it just treats it like:
something {$array} [$i] $something_else
which is a syntax error because it has no comma (update: after the [$i])

Thus, there are two ways to make that take $handles[$i] be the first argument:

update: see also perlobj which discusses this ambiguity under the heading "WARNING".

(update: minor grammatical edit(s) above)

Replies are listed 'Best First'.
Re: Re: Real World 1, Great Expectations 0
by hsmyers (Canon) on Oct 18, 2001 at 04:41 UTC
    Yes! and the winner is… Thank you for precisely the information I was looking for. I had tried something like your first answer, but the perldoc said parens or plus sign, not curley braces (what are they called anyway?), so I didn't get far. As for answer #2, certainly this works (although as I said, I hadn't gotten around to IO::Handle yet) but then so do a lot of other techniques as well. What I was looking for was what you explained— why it didn't work, not workarounds. Thanks again,

    hsm
      ...not curley braces (what are they called anyway?)

      I seem to remember reading this in a previous life:
          () are braces
          {} are brackets

      Update: I stand corrected. See pjf's response below.

      mr greywolf
        According to The Free On-Line Dictionary of Computing (and my own experience and teaching), I've found the following conventions are normally used:

        • {} are braces (also called squiggly or curly brackets)
        • [] are brackets (also called square brackets)
        • () are parentheses (also called parens, round brackets, or bananas)

        Of course, "braces vs brackets vs parentheses" are not immediately obvious to everyone, whereas "curly brackets vs square brackets vs round brackets" are. If you're not certain that your audience is going to use the same conventions as you, it's worth clarifying things first.

        That reminds me, I've got a bunch of course notes I should be updating to do exactly that. ;)

        Cheers,
        Pedantic Paul