Movatterモバイル変換


[0]ホーム

URL:


functions /bless
(source,CPAN)
You are viewing the version of this documentation from Perl 5.43.2. This is a development version of Perl.
#bless REF,CLASSNAME
#bless REF

bless tells Perl to mark the item referred to byREF as an object in a package. The two-argument version ofbless is always preferable unless there is a specific reason tonot use it.

  • Bless the referred-to item into a specific package (recommended form):

    bless $ref, $package;

    The two-argument form adds the object to the package specified as the second argument.

  • Bless the referred-to item into packagemain:

    bless $ref, "";

    If the second argument is an empty string,bless adds the object to packagemain.

  • Bless the referred-to item into the current package (not inheritable):

    bless $ref;

    Ifbless is used without its second argument, the object is created in the current package. The second argument should always be supplied if a derived class might inherit a method executingbless. Because it is a potential source of bugs, one-argumentbless is discouraged.

Seeperlobj for more about the blessing (and blessings) of objects.

bless returns its first argument, the supplied reference, as the value of the function; sincebless is commonly the last thing executed in constructors, this means that the reference to the object is returned as the constructor's value and allows the caller to immediately use this returned object in method calls.

CLASSNAME should always be a mixed-case name, as all-uppercase and all-lowercase names are meant to be used only for Perl builtin types and pragmas, respectively. Avoid creating all-uppercase or all-lowercase package names to prevent confusion.

Also avoidblessing things into the class name0; this will cause code which (erroneously) checks the result ofref to see if a reference isblessed to fail, as "0", a false value, is returned.

See"Perl Modules" in perlmod for more details.

Perldoc Browser is maintained by Dan Book (DBOOK). Please contact him via theGitHub issue tracker oremail regarding any issues with the site itself, search, or rendering of documentation.

The Perl documentation is maintained by the Perl 5 Porters in the development of Perl. Please contact them via thePerl issue tracker, themailing list, orIRC to report any issues with the contents or format of the documentation.


[8]ページ先頭

©2009-2026 Movatter.jp