indirect object notation confusion

S

slowpoison

I have a class defined as:

#------ MyClass.pm -----
package MyClass;
use strict;

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

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



and I have a Perl script:
#---- myclass.pl ----
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 myclass.pl ----

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!
-sp
 
I

Ilmari Karonen

slowpoison said:
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.

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?
 
S

slowpoison

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

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

-vish
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads


Members online

No members online now.

Forum statistics

Threads
473,768
Messages
2,569,574
Members
45,051
Latest member
CarleyMcCr

Latest Threads

Top