"... anyone who names their package "0" is going to asking for trouble!"

Naming a package 0 is a fatal, compilation error:

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

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.

I had some difficulty coming up with an example. Here's some valid, albeit highly contrived, code to demonstrate the concept.

I've used a common alias of mine to show code validity:

$ alias perle alias perle='perl -Mstrict -Mwarnings -Mautodie=:all -MCarp::Always -E +'
$ perle ' package X { use Scalar::Util "blessed"; sub new { bless {}, __PACKAGE__; } sub meth1 { say "meth1: [", ref $_[0], "]" } sub meth2 { say "meth2: [", blessed $_[0] // "undef", "]" } sub meth3 { say "meth3: [", defined blessed $_[0], "]" } }; package main; my $x = 0; my $y = X::->new(); $x->X::meth1(); $x->X::meth2(); $x->X::meth3(); $y->X::meth1(); $y->X::meth2(); $y->X::meth3(); ' meth1: [] meth2: [undef] meth3: [] meth1: [X] meth2: [X] meth3: [1]

The 'package X;' is intended to represent your code; the 'package main;' (although quite superfluous here) is intended to represent "some code completely separate from yours".

— Ken


In reply to Re^4: Calling module function from within a blessed module by kcott
in thread Calling module function from within a blessed module by Bod

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.