question about 'bless'

Ask a Question related to PERL Miscellaneous, Design and Development.

  1. #1

    Default question about 'bless'

    Hi.

    i'm reading a book about OOP with perl.
    in constructor, for example

    sub new
    {
    my ($class, %args) = @_;
    ...
    bless { ... }, ref($class) || $class;
    }

    i'm wondering this. "ref($class) || $class".
    i cannot understand this phrase.
    plz, explain to me.

    thanks for reading. :)

    Kim Jonguk Guest

  2. Similar Questions and Discussions

    1. how to "bless" while "strict"?
      -----BEGIN PGP SIGNED MESSAGE----- Hash: SHA1 I'm working through perlboot right now and have run up against a minor challenge. Unlike the...
    2. Thanks and God bless!
      Goodday, This might seem very deplorable for a person that you do not know but as the title implies ,I am Omoh Abacha, the Second son of the late...
    3. PURCHASE, GOD BLESS YOU.
      GOOD DAY, This is to inform you that I’m very interested in your property . I live in UK and investing on properties abroad and on fixed assets...
  3. #2

    Default Re: question about 'bless'

    Kim Jonguk <kju@ajou.ac.kr> writes:
    > i'm reading a book about OOP with perl.
    > in constructor, for example
    >
    > sub new
    > {
    > my ($class, %args) = @_;
    > ...
    > bless { ... }, ref($class) || $class;
    > }
    >
    > i'm wondering this. "ref($class) || $class".
    > i cannot understand this phrase.
    Can you tell us what parts (if any) of the reference manual
    descriptions of the bless() funtion, ref() function and the ||
    operator you can't understand?

    Without this information we are just likely to repeat what the manual
    says and you'll be no better off.

    I shall, therefore, proceded on the assumuption that you've read and
    understood those manual entries.

    The first argument passed to a subroutine being called as a method is
    an object reference for an instance method call and a string
    containing the class name for a class method call.

    A constructor is usually called as a class method.

    The variable $class, in this case, therefore contains the class name.

    However the author of the above code obviously wanted to allow new to
    be callable as an instance method too. In that case $class would
    contain an object reference and ref($class) would return the class
    name.

    --
    \\ ( )
    . _\\__[oo
    .__/ \\ /\@
    . l___\\
    # ll l\\
    ###LL LL\\
    Brian McCauley Guest

  4. #3

    Default Re: question about 'bless'

    Kim Jonguk <kju@ajou.ac.kr> wrote in comp.lang.perl.misc:
    > Hi.
    >
    > i'm reading a book about OOP with perl.
    > in constructor, for example
    >
    > sub new
    > {
    > my ($class, %args) = @_;
    > ...
    > bless { ... }, ref($class) || $class;
    > }
    >
    > i'm wondering this. "ref($class) || $class".
    > i cannot understand this phrase.
    > plz, explain to me.
    It's an idiom that makes the new() method usable as both an object
    method and a class method. In a class-method call, (the normal way
    to call new()), $class contains a string with a class name. In that
    case, "ref( $class)" is "" (a false value), and the result of "ref( $class)
    || $class" is $class itself. If you call new() through an object,
    $class will contain that object. Then, "ref( $class)" is the name
    of the object's class. In both cases, bless() is called with a
    class name, as it should be.

    That said, in my opinion, using new() as an object method is not a
    good idea. If "$object->new( ...)" creates an entirely independent
    object, the presence of "$object" in the call is confusing, since it has
    no consequences. If "$object->new( ...)" creates a clone of object,
    then call the method "clone" or "copy", but not "new".

    So, while the construct works and gives the new() method a spurious
    universality, I'd just replace ""ref($class) || $class" with "$class".

    Anno
    Anno Siegel Guest

Posting Permissions

  • You may not post new threads
  • You may post replies
  • You may not post attachments
  • You may not edit your posts

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139