> #!/usr/bin/perl
> use strict;
> use warnings;
>
> sub my_fn { print "in sub my_fn, args='@_'\n" }
>
> # This happily calls my_fn(): the parameters received by my_fn are
> # 'sample.t' and 'def'. But why?
>
> 'sample.t'->main::my_fn('def');
>
> # ... yet this fails with: Can't call method "main::my_fn" without
> # a package or object reference
>
> './sample.t'->main::my_fn('def');
For cheap thrills, I grep'ed the Perl C sources for the error message
and found it in file pp_hot.c/S_method_common():
if (!packname ||
((UTF8_IS_START(*packname) && DO_UTF8(sv))
? !isIDFIRST_utf8((U8*)packname)
: !isIDFIRST(*packname)
))
{
Perl_croak(aTHX_ "Can't call method \"%s\" %s", name,
SvOK(sv) ? "without a package or object reference"
: "on an undefined value");
}
/* assume it's a package name */
...
Hmm, it seems that if the first char is [A-Za-z_] it's assumed to be
a package -- which explains why 'sample.t' worked and './sample.t'
didn't. To test that theory further:
#!/usr/bin/perl
sub What::the::_::is::going::on { print+reverse$/,@_ }
'[EMAIL PROTECTED]&?!! is going on?'->What::the::_::is::going::on('What the ');
Not sure if this is a bug or a feature, but it sure is fun. :-)
/-\
http://mobile.yahoo.com.au - Yahoo! Mobile
- Check & compose your email via SMS on your Telstra or Vodafone mobile.