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:
would it meansomething $array[$i]
orsomething {$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]
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])something {$array} [$i] $something_else
Thus, there are two ways to make that take $handles[$i] be the first argument:
- Use {}s: print {$handles[$i]} "...";
- Use the IO::Handle module and use the -> syntax: use IO::Handle; $handles[$i]->print("...");
update: see also perlobj which discusses this ambiguity under the heading "WARNING".
(update: minor grammatical edit(s) above)
In Section
Seekers of Perl Wisdom