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
#------ 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