in reply to Re^4: Calling module function from within a blessed module
in thread Calling module function from within a blessed module

Naming a package 0 is a fatal, compilation error ... The problem is likely to occur where, in some code completely separate from yours, someone writes '$x->your_method()' when, perhaps, '$y->your_method()' was intended.

Another example might be auto-generated package names (though if such code generated a package named "0", one could question the sanity of that choice...)

use warnings; use strict; { package Foo; use Data::Dump; use Scalar::Util qw/blessed/; sub new { return bless {}, shift } sub foo { my $self = shift if blessed($_[0]) && $_[0]->isa(__PACKAGE__); dd 'foo', $self, \@_; } sub bar { my $self = shift if defined blessed($_[0]) && $_[0]->isa(__PACKAGE__); dd 'bar', $self, \@_; } } { no strict 'refs'; @{"0::ISA"} = ('Foo') } my $x = bless {}, '0'; $x->foo(); # ("foo", undef, ["0=HASH(0x561239194f3e)"]) $x->bar(); # ("bar", "0=HASH(0x561239194f3e)", [])

Replies are listed 'Best First'.
Re^6: Calling module function from within a blessed module
by kcott (Archbishop) on Jan 02, 2021 at 21:31 UTC

    The general idea I trying to get across — and, perhaps, didn't explain sufficiently well — was that problems are less likely to occur from a hard-coded package name, but rather from accidentally using an incorrect variable; the likelihood of which is exacerbated by poorly chosen (especially single-letter) variable names.

    The type of scenario I envisaged, involved $x being used for some purpose, and ultimately ending up with a value of zero. Later in the code, a variable to represent either an X class, or an object thereof, was wanted. As $x was already taken, the coder chose the next available, single-letter variable, $y.

    Subsequently, and paraphasing your example, the intuitive but incorrect @{"${x}::ISA"} and bless {}, $x was used; when, of course, the unintuitive yet correct @{"${y}::ISA"} and bless {}, $y was needed.

    Possibly getting a little off-topic (and hopefully not heading towards too much of a rant) but, in the main, meaningful names should be used, and single-letter names should be generally avoided. There are a few well-known examples where single-letter names are fine; e.g. sort { $a <=> $b } and in the limited scope of for my $i (0..$#ary). I also use them in one-liners and short example code. Other than that, I always aim to use meaningful names.

    — Ken

      The general idea I trying to get across — and, perhaps, didn't explain sufficiently well — was that problems are less likely to occur from a hard-coded package name, but rather from accidentally using an incorrect variable; the likelihood of which is exacerbated by poorly chosen (especially single-letter) variable names.

      Sorry, I probably should have said: I think your example is good, I was just giving another scenario :-)

        No need to apologise; your example was also good. Any fault probably lies with me; as stated: "I ... perhaps, didn't explain sufficiently well".

        — Ken