in reply to Re: Re: Re: Re: Calling can method
in thread Calling can method

When can't you call $x->can('foo') safely? When $x can't can(), of course!
for $x ('main', undef, \1) { if (UNIVERSAL::can($x, 'can')) { # ok print "can can\n"; $x->can('foo'); print "\n"; } else { # error: Can't call method "can" on undefined value/unblessed refe +rence ... print "can't can\n"; eval { $x->can('foo') }; print "$@\n"; } }

Replies are listed 'Best First'.
(tye)Re: Calling can method
by tye (Sage) on May 30, 2001 at 22:25 UTC

    Clarification: The else part wouldn't generate an error for 'main' but would/does for the other two (well, it wasn't clear to me at first but I think that is more my problem than chipmunk's).

    One case you didn't mention is "strings that don't start with a \w character":

    $x= '*fred'; print $x->can('can'); Can't call method "can" without a package or object reference
    and, yes, it is (currently, at least) only the first character that matters for this test.

    BTW, these errors are fatal (which is why chipmunk used eval, otherwise the loop would not have continued).

    Updated many times while I tried to make some sense.

            - tye (but my friends call me "Tye")