indirect object notation confusion

Do you have a question? Post it now! No Registration Necessary.  Now with pictures!

Threaded View

I have a class defined as:

#------ -----
package MyClass;
use strict;

use Exporter;
our @EXPORT=qw(foo);

sub foo($$) { print "foo says $_[1]\n"; }
#---- end of ----

and I have a Perl script:
#---- ----
require MyClass;

# section 1
my $obj = {};
bless $obj, MyClass;
foo $obj "bar";

# section 2
my $mc = 'MyClass';
foo $mc "bar";

# section 3
foo MyClass "bar";

# section 4
foo 'MyClass' "bar";

# section 5
sub fmc { return 'MyClass'; }
foo fmc "bar";

# section 6
use constant mc => 'MyClass';
foo mc "bar";
#---- end of ----

sections 1,2 and 3 compile fine, but, sections 4,5 and 6 make Perl
complain. I think I know why this happens. Perl doesn't recognize these
as indirect object notations because what follows foo doesn't look like
an object or a class. Any in-depth insights from readers would be
useful though.

A followup question: If 4,5 and 6 won't work, how does Perl define
STDOUT and STDERR  internally to make them work with print:

print STDERR "error: PEBKAC"

thanks a ton!

Re: indirect object notation confusion

Quoted text here. Click to load it

Yes, that's essentially correct, and a very good example of why
indirect object notation should usually be avoided.

Is there some reason why you wouldn't rather use arrow notation?

Ilmari Karonen
To reply by e-mail, please replace ".invalid" with ".net" in address.

Re: indirect object notation confusion

Ilmari Karonen wrote:
Quoted text here. Click to load it

I want to implement a print like function:
myprint MYSTDOUT "coooooool!"
and don't know how to define MYSTDOUT.


Site Timeline