Beefy Boxes and Bandwidth Generously Provided by pair Networks
Don't ask to ask, just ask
 
PerlMonks  

strings->can->isa but numbers->can't?

by djerius (Beadle)
on Apr 26, 2012 at 15:06 UTC ( #967380=perlquestion: print w/ replies, xml ) Need Help??
djerius has asked for the wisdom of the Perl Monks concerning the following question:

Whilst trying to explain to someone the benefits of

eval { $x->isa( $class ) }

to determine if an object belongs in a class, I embarrassingly came across the following behavior, which I could not explain.

use strict; use warnings; my $x = 'string'; $x->isa('huh?');
This, surprisingly, runs without error. If instead, I make $x a number:
use strict; use warnings; my $x = 3; $x->isa('huh?')
I get the expected error:
Can't call method "isa" without a package or object reference at foo.pl line 5.
Why should a string have access to UNIVERSAL methods while a number doesn't? I've tried this with Perl 5.10.1 and 5.12.2.

Comment on strings->can->isa but numbers->can't?
Select or Download Code
Re: strings->can->isa but numbers->can't?
by Anonymous Monk on Apr 26, 2012 at 15:16 UTC

    Why should a string have access to UNIVERSAL methods while a number doesn't?

    Because :)

    $ perl -e " package 666; " syntax error at -e line 1, near "package 666;" Execution of -e aborted due to compilation errors.

    autobox::Core

    $ perl -Mautobox::Core -e " 666->say " 666
Re: strings->can->isa but numbers->can't?
by moritz (Cardinal) on Apr 26, 2012 at 15:32 UTC
      Strings are simply interpreted as package names when you call a method on them. This is how Foo->new works in the first place.

      D'oh! Of course! I even use that behavior on occasion.

      Thanks for the reminder.

Re: strings->can->isa but numbers->can't?
by ikegami (Pope) on Apr 26, 2012 at 15:39 UTC
    Elsewhere, Perl will stringify when it wants a string. There's no good explanation as to why it doesn't do that here.

      Elsewhere, Perl will stringify when it wants a string. There's no good explanation as to why it doesn't do that here.

      Sure there is, 3 is not a valid package or subroutine, name

      $ perl -le " package 3; " syntax error at -e line 1, near "package 3;" Execution of -e aborted due to compilation errors. $ perl -le " $3::3 " Bareword found where operator expected at -e line 1, near "$3::3" (Missing operator before ::3?) syntax error at -e line 1, near "$3::3 " Execution of -e aborted due to compilation errors. $ perl -le " sub 3 { 3 } " Illegal declaration of anonymous subroutine at -e line 1.
        But, interestingly
        perl -le "*3 = sub { 4 };print 3->()" 4

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://967380]
Approved by lidden
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others imbibing at the Monastery: (6)
As of 2015-07-03 21:26 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    The top three priorities of my open tasks are (in descending order of likelihood to be worked on) ...









    Results (56 votes), past polls