<rt id="bn8ez"></rt>
<label id="bn8ez"></label>

  • <span id="bn8ez"></span>

    <label id="bn8ez"><meter id="bn8ez"></meter></label>

    隨筆-124  評論-194  文章-0  trackbacks-0

    多行注釋:
    perl沒有多行注釋,可以用下面代替:
    =pod
    代碼行;
    .
    .
    .
    代碼行;
    =cut


    關于ref函數:
     ref EXPR
     ref     Returns a non-empty string if EXPR is a reference, the empty
             string otherwise. If EXPR is not specified, $_ will be used. The
             value returned depends on the type of thing the reference is a
             reference to. Builtin types include:

                 SCALAR
                 ARRAY
                 HASH
                 CODE
                 REF
                 GLOB
                 LVALUE

             If the referenced object has been blessed into a package, then
             that package name is returned instead. You can think of "ref" as
             a "typeof" operator.



    講類的段落,比較明了:
    Object Construction
    All objects are references, but not all references are objects. A reference won't work as an object unless its referent is specially marked to tell Perl what package it belongs to. The act of marking a referent with a package name--and therefore, its class, since a class is just a package--is known as blessing. You can think of the blessing as turning a reference into an object, although it's more accurate to say that it turns the reference into an object reference.

    The bless function takes either one or two arguments. The first argument is a reference and the second is the package to bless the referent into. If the second argument is omitted, the current package is used.

    $obj = { };                 # Get reference to anonymous hash.
    bless($obj);                # Bless hash into current package.
    bless($obj, "Critter");     # Bless hash into class Critter.
    Here we've used a reference to an anonymous hash, which is what people usually use as the data structure for their objects. Hashes are extremely flexible, after all. But allow us to emphasize that you can bless a reference to anything you can make a reference to in Perl, including scalars, arrays, subroutines, and typeglobs. You can even bless a reference to a package's symbol table hash if you can think of a good reason to. (Or even if you can't.) Object orientation in Perl is completely orthogonal to data structure.

    Once the referent has been blessed, calling the built-in ref function on its reference returns the name of the blessed class instead of the built-in type, such as HASH. If you want the built-in type, use the reftype function from the attributes module. See use attributes in Chapter 31, "Pragmatic Modules".

    And that's how to make an object. Just take a reference to something, give it a class by blessing it into a package, and you're done. That's all there is to it if you're designing a minimal class. If you're using a class, there's even less to it, because the author of a class will have hidden the bless inside a method called a constructor, which creates and returns instances of the class. Because bless returns its first argument, a typical constructor can be as simple as this:

    package Critter;
    sub spawn { bless {}; }
    Or, spelled out slightly more explicitly:
    package Critter;
    sub spawn {
        my     $self = {};       # Reference to an empty anonymous hash
        bless  $self, "Critter"; # Make that hash a Critter object
        return $self;            # Return the freshly generated Critter
    }
    With that definition in hand, here's how one might create a Critter object:
    $pet = Critter->spawn;

    12.4.1. Inheritable Constructors
    Like all methods, a constructor is just a subroutine, but we don't call it as a subroutine. We always invoke it as a method--a class method, in this particular case, because the invocant is a package name. Method invocations differ from regular subroutine calls in two ways. First, they get the extra argument we discussed earlier. Second, they obey inheritance, allowing one class to use another's methods.

    We'll describe the underlying mechanics of inheritance more rigorously in the next section, but for now, some simple examples of its effects should help you design your constructors. For instance, suppose we have a Spider class that inherits methods from the Critter class. In particular, suppose the Spider class doesn't have its own spawn method. The following correspondences apply:

    Method Call Resulting Subroutine Call
    Critter->spawn() Critter::spawn("Critter")
    Spider->spawn() Critter::spawn("Spider")

    The subroutine called is the same in both cases, but the argument differs. Note that our spawn constructor above completely ignored its argument, which means our Spider object was incorrectly blessed into class Critter. A better constructor would provide the package name (passed in as the first argument) to bless:

    sub spawn {
        my $class =  shift;       # Store the package name
        my $self  =  { };
        bless($self, $class);     # Bless the reference into that package
        return $self;
    }
    Now you could use the same subroutine for both these cases:
    $vermin = Critter->spawn;
    $shelob = Spider->spawn;
    And each object would be of the proper class. This even works indirectly, as in:
    $type  = "Spider";
    $shelob = $type->spawn;         # same as "Spider"->spawn
    That's still a class method, not an instance method, because its invocant holds a string and not a reference.

    If $type were an object instead of a class name, the previous constructor definition wouldn't have worked, because bless needs a class name. But for many classes, it makes sense to use an existing object as the template from which to create another. In these cases, you can design your constructors so that they work with either objects or class names:

    sub spawn {
        my $invocant = shift;
        my $class    = ref($invocant) || $invocant;  # Object or class name
        my $self     = { };
        bless($self, $class);
        return $self;
    }

    12.4.2. Initializers
    Most objects maintain internal information that is indirectly manipulated by the object's methods. All our constructors so far have created empty hashes, but there's no reason to leave them empty. For instance, we could have the constructor accept extra arguments to store into the hash as key/value pairs. The OO literature often refers to such data as properties, attributes, accessors, member data, instance data, or instance variables. The section "Instance Variables" later in this chapter discusses attributes in more detail.

    Imagine a Horse class with instance attributes like "name" and "color":

    $steed = Horse->new(name => "Shadowfax", color => "white");
    If the object is implemented as a hash reference, the key/value pairs can be interpolated directly into the hash once the invocant is removed from the argument list:
    sub new {
        my $invocant = shift;
        my $class = ref($invocant) || $invocant;
        my $self = { @_ };          # Remaining args become attributes
        bless($self, $class);       # Bestow objecthood
        return $self;
    }
    This time we used a method named new for the class's constructor, which just might lull C++ programmers into thinking they know what's going on. But Perl doesn't consider "new" to be anything special; you may name your constructors whatever you like. Any method that happens to create and return an object is a de facto constructor. In general, we recommend that you name your constructors whatever makes sense in the context of the problem you're solving. For example, constructors in the Tk module are named after the widgets they create. In the DBI module, a constructor named connect returns a database handle object, and another constructor named prepare is invoked as an instance method and returns a statement handle object. But if there is no suitable context-specific constructor name, new is perhaps not a terrible choice. Then again, maybe it's not such a bad thing to pick a random name to force people to read the interface contract (meaning the class documentation) before they use its constructors.

    Elaborating further, you can set up your constructor with default key/value pairs, which the user could later override by supplying them as arguments:

    sub new {
        my $invocant = shift;
        my $class   = ref($invocant) || $invocant;
        my $self = {
            color  => "bay",
            legs   => 4,
            owner  => undef,
            @_,                 # Override previous attributes
        };
        return bless $self, $class;
    }

    $ed       = Horse->new;                    # A 4-legged bay horse
    $stallion = Horse->new(color => "black");  # A 4-legged black horse
    This Horse constructor ignores its invocant's existing attributes when used as an instance method. You could create a second constructor designed to be called as an instance method, and if designed properly, you could use the values from the invoking object as defaults for the new one:
    $steed  = Horse->new(color => "dun");
    $foal   = $steed->clone(owner => "EquuGen Guild, Ltd.");

    sub clone {
        my $model = shift;
        my $self  = $model->new(%$model, @_);
        return $self;     # Previously blessed by ->new
    }

    posted on 2007-05-24 15:31 我愛佳娃 閱讀(8323) 評論(2)  編輯  收藏 所屬分類: Perl

    評論:
    # re: 關于Perl的幾點:多行注釋,REF函數,類的初始化 2008-05-15 14:38 | 奪取
    =cut
    '
    '
    '
    '
    '
    =cut //多行注釋

    #號 //單行注釋
      回復  更多評論
      
    # re: 關于Perl的幾點:多行注釋,REF函數,類的初始化 2008-07-12 20:28 | youyuanyin
    =begin
    注釋
    =end
    =cut

      回復  更多評論
      
    主站蜘蛛池模板: 国产成人亚洲精品狼色在线| 在线成人a毛片免费播放| 亚洲国产女人aaa毛片在线| 午夜毛片不卡免费观看视频| 一区二区免费视频| 手机永久免费的AV在线电影网| 亚洲一区二区影视| 亚洲国产精品久久久久网站| 亚洲国产午夜中文字幕精品黄网站| 最近高清国语中文在线观看免费 | 成年女性特黄午夜视频免费看| 91福利免费网站在线观看| 久久精品国产亚洲av品善 | 国产成人精品亚洲| 亚洲男人av香蕉爽爽爽爽| 在线精品免费视频无码的 | 亚洲一区二区三区高清不卡| 免费欧洲美女牲交视频| 91情侣在线精品国产免费| 免费一级特黄特色大片| 亚洲日韩精品无码专区| 亚洲免费视频播放| 亚洲视频手机在线| 亚洲免费在线播放| 亚洲AV人无码激艳猛片| 亚洲国产成人一区二区精品区| 毛色毛片免费观看| 中文字幕乱码免费视频| 污网站在线免费观看| 亚洲AV噜噜一区二区三区| 亚洲欧洲日产国码久在线观看| 日韩亚洲人成在线综合日本| 国产gv天堂亚洲国产gv刚刚碰 | 一级毛片a女人刺激视频免费| 亚洲永久中文字幕在线| 亚洲大片在线观看| 91亚洲国产成人久久精品网站| 亚洲A∨无码一区二区三区 | 日韩精品无码免费一区二区三区| 国产成人AV片无码免费| 永久免费无码网站在线观看个|