Beefy Boxes and Bandwidth Generously Provided by pair Networks
Welcome to the Monastery
 
PerlMonks  

comment on

( [id://3333]=superdoc: print w/replies, xml ) Need Help??
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)


In reply to Re: Real World 1, Great Expectations 0 by wog
in thread Real World 1, Great Expectations 0 by hsmyers

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":



  • Are you posting in the right place? Check out Where do I post X? to know for sure.
  • Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
    <code> <a> <b> <big> <blockquote> <br /> <dd> <dl> <dt> <em> <font> <h1> <h2> <h3> <h4> <h5> <h6> <hr /> <i> <li> <nbsp> <ol> <p> <small> <strike> <strong> <sub> <sup> <table> <td> <th> <tr> <tt> <u> <ul>
  • Snippets of code should be wrapped in <code> tags not <pre> tags. In fact, <pre> tags should generally be avoided. If they must be used, extreme care should be taken to ensure that their contents do not have long lines (<70 chars), in order to prevent horizontal scrolling (and possible janitor intervention).
  • Want more info? How to link or How to display code and escape characters are good places to start.
Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others studying the Monastery: (5)
As of 2024-04-23 07:35 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found