John M. Dlugosz has asked for the wisdom of the Perl Monks concerning the following question:

This works:
use Win32::OLE; ... my $Paragraphs= $Doc->Paragraphs(); foreach my $para (in $Paragraphs) { ...
but this doesn't:
foreach my $para (in $Doc->Paragraphs()) {
complaining "Can't call method "Paragraphs" without a package or object reference".

If I write

foreach my $para (in ($Doc->Paragraphs())) {
thinking maybe it's binding in tighter than the arrow, I get "Undefined subroutine &main::in called", so now it no longer knows what to do with in. I guess it's a "static" method; I had supposed it to be imported.

What's going on here? Is this a basic parsing/context issue, or is something wierd going on?

—John

Replies are listed 'Best First'.
Re: Syntax and Temporary Variables
by wog (Curate) on Jul 20, 2001 at 23:14 UTC
    This is a problem with indirect object syntax mentioned in perlobj, quote (update: this quote is grabbed from bleadperl docs):

    ... what if there are no arguments? In that case, Perl must guess what you want. Even worse, it must make that guess at compile time. Usually Perl gets it right, but when it doesn't you get a function call compiled as a method, or vice versa....
    .... the indirect object is limited to a name, a scalar variable, or a block, because it would have to do too much lookahead otherwise, just like any other postfix dereference in the language. (These are the same quirky rules as are used for the filehandle slot in functions like "print" and "printf".) This can lead to horribly confusing precedence problems, as in these next two lines:
    move $obj->{FIELD}; # probably wrong! move $ary[$i]; # probably wrong!
    Those actually parse as the very surprising:
    $obj->move->{FIELD}; # Well, lookee here $ary->move([$i]); # Didn't expect this one, eh?

    The docs go on to explain that move {$ary[$i]}, etc. could have been used to gain the proper effect, but still they advise strongly to use -> exclusively because of these problems.

    update (2): added "etc."

      Thanks, I understand it now. Using braces fixes it while still maintaining the prefix notation of in.

      Now if it was imported, then calling it as a function with one argument should work just fine, right? It calls the same function. But when I specified it in the import list, it still complained that &main::in did not exist. That is strange.

      —John

Re: Syntax and Temporary Variables
by rchiav (Deacon) on Jul 21, 2001 at 00:41 UTC
    Also, if you're going to use "in", you have to include that in your use

    use Win32::OLE qw(in);
    Accessing OLE information can get quite tricky when you don't know exactly what is where. The easiest way I've found (on top of scouring msdn is to send the variables to Data::Dumper. I'd have to say it's saved me more time when working with OLE stuff than anything else.

    Hope this helps..
    Rich

      If I import in, nothing changes. It still tells me that there is no &main::in! So I guess it didn't import?

      It works normally without importing it, since it's a "indirect object" syntax. You don't import new to say new Foo::, right?

      —John