in reply to technique of push

Operators define their own parsing rules. Some of these can be approximated using prototypes.

sub my_push(\@@) { my $ref = shift; push @$ref, @_; return 0+@$ref; } my @a = (1..4); my_push(@a, 5); print "@a\n"; # 1 2 3 4 5

Replies are listed 'Best First'.
Re^2: technique of push
by saranperl (Initiate) on Sep 02, 2009 at 07:14 UTC
    could you explain what is "my_push(\@@)" and "return 0+ @$ref"

      I said it was a prototype. You could have searched the docs for "prototype" :-( They're documented in perlsub.

      As for 0+@$ref, it adds zero to the value to which @$ref evaluates. Keep in mind that the addition operator imposes a scalar context on its operands. perldata says "If you evaluate an array in scalar context, it returns the length of the array." Zero plus the length of the array is the length of the array.

      Since push returns the length of the resulting array, I made my_push return the same thing.

      (Without the 0+, @$ref would be evaluated in the same context as the my_push call, and could result in the wrong value being returned.)