prototypes

Discussion in 'Perl Misc' started by ace, Oct 20, 2009.

  1. ace

    ace Guest

    I would like to make a prototype for hpush subroutine which then can be
    used like:

    hpush %hash, %h2;
    hpush %hash, {foo => 1};


    Currently I have to dereference the hash which looks quite ugly:

    hpush %hash, %{{foo => 1}};


    =============
    sub hpush(\%\%) {
    my ($h1, $h2) = @_;
    %$h1 = (%$h1, %$h2);
    return $h2;
    }
    ace, Oct 20, 2009
    #1
    1. Advertising

  2. ace

    sreservoir Guest

    Ben Morrow wrote:
    > Quoth ace <>:
    >> I would like to make a prototype for hpush subroutine which then can be
    >> used like:
    >>
    >> hpush %hash, %h2;
    >> hpush %hash, {foo => 1};

    >
    > You can't do that with prototypes.
    >
    >> Currently I have to dereference the hash which looks quite ugly:
    >>
    >> hpush %hash, %{{foo => 1}};

    >
    > The best answer here is
    >
    > sub hpush (\%%) {
    > my ($h1, %h2) = @_;
    > %$h1 = (%$h1, %$h2);
    > return scalar keys %$h1;
    > }


    Note that a % in the prototype will still allow odd-numbered arguments.

    --

    "Six by nine. Forty two."
    "That's it. That's all there is."
    "I always thought something was fundamentally wrong with the universe"
    sreservoir, Oct 20, 2009
    #2
    1. Advertising

  3. ace

    sreservoir Guest

    Ben Morrow wrote:
    > Quoth sreservoir <>:
    >> Ben Morrow wrote:
    >>> sub hpush (\%%) {
    >>> my ($h1, %h2) = @_;
    >>> %$h1 = (%$h1, %$h2);
    >>> return scalar keys %$h1;
    >>> }

    >> Note that a % in the prototype will still allow odd-numbered arguments.

    >
    > Yes. % in a prototype is in every way equivalent to @.
    >
    > The assignment will give a warning, and that can trivially be replaced
    > with a (more helpful) error with
    >
    > sub hpush (\%%) {
    > @_ % 2 or croak "Odd number of elements in hpush";


    that croak should be:
    @_ % 2 and croak ...

    @_ % 2 == 1 if @_ is odd, 0 if even.


    > In general trying to to compile-time argument checking is futile in Perl
    > 5: the language is just too dynamic. Perl 6 is making a serious attempt
    > to fix that, at least where it's useful to do so.


    Yet another reason for perl6!

    --

    "Six by nine. Forty two."
    "That's it. That's all there is."
    "I always thought something was fundamentally wrong with the universe"
    sreservoir, Oct 20, 2009
    #3
  4. ace

    sreservoir Guest

    Ben Morrow wrote:
    > Quoth sreservoir <>:
    >> Ben Morrow wrote:
    >>> The assignment will give a warning, and that can trivially be replaced
    >>> with a (more helpful) error with
    >>>
    >>> sub hpush (\%%) {
    >>> @_ % 2 or croak "Odd number of elements in hpush";

    >> that croak should be:
    >> @_ % 2 and croak ...

    >
    > No it shouldn't.
    >
    >> @_ % 2 == 1 if @_ is odd, 0 if even.

    >
    > Yes...
    >
    > (think about it more carefully :))


    Oh, yes, the \%. I was giving bad advice for a while yesterday. Maybe I
    need to sleep more. :)

    --

    "Six by nine. Forty two."
    "That's it. That's all there is."
    "I always thought something was fundamentally wrong with the universe"
    sreservoir, Oct 21, 2009
    #4
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Lars Hedström

    Prototypes of html-pages

    Lars Hedström, Apr 8, 2004, in forum: HTML
    Replies:
    3
    Views:
    380
    Jim Roberts
    Apr 8, 2004
  2. Paul Davis
    Replies:
    2
    Views:
    309
    Paul Davis
    Jul 15, 2003
  3. Dave Vandervies

    Default arguments and prototypes

    Dave Vandervies, Oct 2, 2003, in forum: C++
    Replies:
    5
    Views:
    467
    Victor Bazarov
    Oct 3, 2003
  4. Denis Remezov

    order of const in function prototypes

    Denis Remezov, Jun 30, 2004, in forum: C++
    Replies:
    7
    Views:
    431
    John Harrison
    Jun 30, 2004
  5. fb

    Prototypes

    fb, Sep 2, 2003, in forum: C Programming
    Replies:
    14
    Views:
    749
    Dave Thompson
    Sep 8, 2003
Loading...

Share This Page