Checking prototype with objects

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

  1. #1

    Default Checking prototype with objects


    --
    Hi there.

    I am trying to use prototype functions with object but this seems not
    to work, I mean perl does not check for the type of arguments.

    I usually do this.

    sub array_print(\@);

    ...
    ....

    sub array_print(\@)
    {
    # my code
    }


    Then when I call it as:

    array_print(@this_array);

    Perl is supposed to check the prototype and change the array by its
    reference. Now I try to put this same behavior but with object, and
    then when I call the same function as:

    $obj_array->array_print(@this_array);

    Perl sends the complete array to the function and not the reference.
    So I need to do this

    $obj_array->array_print(\@this_array);

    Does any one knows how to force perl to behave as normal?

    Cheers
    Hazael Guest

  2. Similar Questions and Discussions

    1. RIA Prototype Developer contract position
      RIA Prototype Developer Bank of America eCommerce User Centered Design & Research is currently interviewing candidates for Contract positions...
    2. setInterval + prototype referencing
      I'm having two problems which are no doubt related I've got my Showbox functions working so that all images download and the program traces "file...
    3. MovieClip.prototype - movement
      THE PROBLEM: ------------------ The goal is to use a custom prototype OnClipEvent(enterFrame) of 2 or more movieclips, that will "pause" for a...
    4. MX when to assign the prototype for a class
      In the AS-code for the radiobutton-component in MX, the statement that assigns a prototype to the function is placed at te thop of the script, well...
    5. Movieclip.prototype.status
      Hi. Does someone know why Movieclip.prototype.status = function(msg) { getURL("javascript:window.status='"+msg+"';void(0)"); }; will not...
  3. #2

    Default Re: Checking prototype with objects

    Hazael Maldonado Torres wrote: 

    There is no way of prototyping object methods as run-time inheritance
    means that Perl cannot know which subroutine will provide the
    method until it is actually called.

    Perl isn't C, and it's hardly ever a good idea to prototype
    Perl subroutines. It's far more useful to be able to pass an array,
    an array or hash slice, or a list of scalars (or a mixture of these)
    to a subroutine that simply expects a LIST.

    If you must, you can code

    sub array_print {
    my $array = shift;
    die unless ref $array eq 'ARRAY';
    }

    but I would expect something like 'array_print' to look at
    least something like the built-in 'print' operator which
    takes a list of values.

    HTH,

    Rob


    Rob Guest

  4. #3

    Default Re: Checking prototype with objects

    Hazael Maldonado Torres wrote:
     

    Your immediate problem here is that you are trying to use an expression as a
    formal argument. Formal arguments in Perl can only take storage types. Really,
    the only meaningful ones are scalar and list. The list, if it is present, must
    be the last parameter, and there can only be one, since any list will consume
    all arguments offered from there on.

    The formal parmeter expressions you show are both array references. These are
    scalars. They should be prtotyped as $.

    Joseph

    R. Guest

  5. #4

    Default Re: Checking prototype with objects

    >>>>> "Hazael" == Hazael Maldonado Torres <com> writes:

    Hazael> I am trying to use prototype functions with object but this seems not
    Hazael> to work, I mean perl does not check for the type of arguments.

    Exactly. Prototypes are meant for one thing only: to have a Perl function
    emulate a built-in.

    They are not meant for "checking argument lists". And there are no
    built-ins that have odd argument processing for method calls, which is
    why they don't need to work there either.

    Please do not use prototypes until you understand why you should not
    use prototypes. :)

    --
    Randal L. Schwartz - Stonehenge Consulting Services, Inc. - +1 503 777 0095
    <com> <URL:http://www.stonehenge.com/merlyn/>
    Perl/Unix/security consulting, Technical writing, Comedy, etc. etc.
    See PerlTraining.Stonehenge.com for onsite and open-enrollment Perl training!
    Randal Guest

  6. #5

    Default Re: Checking prototype with objects

    R. Joseph Newton wrote: 
    >
    > Your immediate problem here is that you are trying to use an expression as a
    > formal argument. Formal arguments in Perl can only take storage types. Really,
    > the only meaningful ones are scalar and list. The list, if it is present, must
    > be the last parameter, and there can only be one, since any list will consume
    > all arguments offered from there on.
    >
    > The formal parmeter expressions you show are both array references. These are
    > scalars. They should be prtotyped as $.[/ref]

    Sorry Joseph. This from perldoc perlsub

    Prototypes

    Perl supports a very limited kind of compile-time argument checking
    using function prototyping. If you declare

    sub mypush (\@@)

    then "mypush()" takes arguments exactly like "push()" does.

    Cheers,

    Rob


    Rob Guest

  7. #6

    Default Re: Checking prototype with objects

    Rob Dixon wrote:
     
    > >
    > > Your immediate problem here is that you are trying to use an expression as a
    > > formal argument. Formal arguments in Perl can only take storage types. Really,
    > > the only meaningful ones are scalar and list. The list, if it is present, must
    > > be the last parameter, and there can only be one, since any list will consume
    > > all arguments offered from there on.
    > >
    > > The formal parmeter expressions you show are both array references. These are
    > > scalars. They should be prtotyped as $.[/ref]
    >
    > Sorry Joseph. This from perldoc perlsub
    >
    > Prototypes
    >
    > Perl supports a very limited kind of compile-time argument checking
    > using function prototyping. If you declare
    >
    > sub mypush (\@@)
    >
    > then "mypush()" takes arguments exactly like "push()" does.
    >
    > Cheers,
    >
    > Rob[/ref]

    Thanks, Rob,

    That information actually makes prototypes at least marginally more useful. If you
    can specify the type to which a references refers, that certainly offers more
    information than the simplescalar and list types I have seen. It also provides a way
    to set up more complicatedparameters lists and still use prototypes.

    Joseph

    R. 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