Why is this Perl code not a bug?

I am writing a WxPerl program and instead

$panel->SetDropTarget( MyTextDropTarget->new($panel) ); 

I wrote

 $panel->SetDropTarget->( MyTextDropTarget->new($panel) ); 

and Perl did not complain. (Perl called some function with some arguments, and the script made an error with an error message:

Usage: Wx :: Window :: SetDropTarget (IT, target) in a string. /x.pl 230.

I'm curious how Perl parsed the โ€œwrongโ€ version.

Editing added:

Ok, so if $ x = \ & some_function, then I understand why

 $x->($arg1, $arg2, ...) 

calls some_function ($ arg1, $ arg2, .....).

The runrig example is one of the possibilities, but I am sure that: 1) $ panel contains a link to a blissful object, not a string. 2) SetDropTarget is a method (in the base class) of $ panel.

From the error message, it can be assumed that SetDropTarget () is being called. If the Runrig example is used here (and I would suggest that it is), then even if SetDropTarget does not follow the brackets, that is, I have

 $panel->SetDropTarget-> 

instead

  $panel->SetDropTarget()-> 

Does Perl still call SetDropTarget? (which checks the number and type of its arguments, and then mortally complains at run time). Is that what is happening?

+4
source share
3 answers

Because it is absolutely correct syntax. $panel->SetDropTarget->( MyTextDropTarget->new($panel) ) means "Call the &SetDropTarget method from $panel , and then dereference the substring returned by &SetDropTarget ". This is not what you wanted to write, I suppose, but any error you selected will be at runtime, even in strict mode.

+6
source

Your SetDropTarget will most likely not return a link to the code, but you cannot say so until runtime:

 #!/usr/bin/perl my $panel = 'main'; $panel->SetDropTarget->("bloomin'"); sub SetDropTarget { return sub { print "Set the $_[0] drop target!\n" } } 
+6
source

Perl parsed

 $panel->SetDropTarget->( MyTextDropTarget->new($panel) ); 

means " $panel is an object, its SetDropTarget method is SetDropTarget (optional bracket is omitted), which will return a reference to the sub call; sub , passing the result MyTextDropTarget->new($panel) as its only parameter.

That is, Perl expected SetDropTarget to look like foo in the following code:

 sub foo { return sub { my $bar = shift; return $bar . ', world!'; } } foo()->('Hello'); foo->('Goodby'); 
+1
source

All Articles