There's no real difference between the two solutions you provide,

The difference is real enough. One of the variants has multiple non-obvious modes of failure.

First of all, the difference is that sub Package::func { ... } compiles the function in the current package and places it in the target package, whereas { package Package; sub func { ... } } compiles the function in the target package. There are a few issues with using sub Package::func { ... }:

It causes a problem when Carp is used. Carp will treat the function as belonging in the current package (main in this case), while it should treat it as being part of the target package to properly identify the caller.

It causes a problem when the function imports symbols. The symbols will be placed in the current pacakge while they should be placed in the target package.

It causes a problem when bless is used with one argument in the function. It will bless objects into the current package while they should be blessed into the target package.

It causes __PACKAGE__ to be the name of the current package instead of the name of the target package.


In reply to Re^2: Reopen a class, add methods ? by ikegami
in thread Reopen a class, add methods ? by Rudif

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.