Beefy Boxes and Bandwidth Generously Provided by pair Networks
XP is just a number
 
PerlMonks  

Re: Real World 1, Great Expectations 0

by wog (Curate)
on Oct 18, 2001 at 03:56 UTC ( #119595=note: print w/replies, xml ) Need Help??


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:

  • 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)

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

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://119595]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others lurking in the Monastery: (5)
As of 2022-11-26 13:13 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    Notices?