develooper Front page | perl.perl5.porters | Postings from April 2006

Re: [PATCH] overridable filetest operators

Thread Previous | Thread Next
From:
Salvador Fandino
Date:
April 3, 2006 08:36
Subject:
Re: [PATCH] overridable filetest operators
Message ID:
20060403153630.15457.qmail@lists.develooper.com
Salvador Fandiño wrote:
> Rafael Garcia-Suarez wrote:
> 
>> Salvador Fandino wrote:
>>
>>>>  - subroutines with (;*) or (;$) prototypes are not handled as unary
>>>> operators so I can not write a replacement for stat that works as the
>>>> built-in for both these two cases:
>>>>
>>>>   print stat, "foo\n";
>>>>   print stat "foo", "\n";
>>>
>>> I have been looking into this problem. A patch to change (;$) to behave
>>> like an unary operator is attached.
>>>
>>> BTW, stat wasn't a good example, 'rand' is better:
>>>
>>>
>>>   sub myrand (;$) { 0.5 * (@_ ? $_[0] : 1.0) }
>>>
>>>   print rand, "\n";      # ==> print(rand(), "\n");
>>>   print rand 2, "\n";    # ==> print(rand(2), "\n");
>>>
>>>   print myrand, "\n";    # ==> print(myrand(), "\n");
>>>   print myrand 2, "\n";  # ==> print(myrand(2, "\n")); croaks!!!
>>>
>>> (other builtins with the same prototype are 'caller', 'exit' or 'alarm')
>>>
>>>
>>> The problem is that this change is going to break things.
>>
>>
>> I'd hate to break things here, because that's going to be very difficult
>> to debug and detect afterwards. That's difficult to admit, but Perl 5 is
>> not an evolving language anymore :) that's why we introduced the
>> "feature" pragma instead of simply adding new operators and keywords.
> 
> 
>> I'd rather extend the existing syntax than modifying it.
> 
> 
> how about adding another prototype '1' to explicetelly indicate that the
> sub has to be parsed as an unary operator?
> 
> Using the 1, ^ and / prototypes we could mimic almost any built-in:
> 
>   myrand (1;$)
>   mystat (1;^/)
>   myrequire (1*)
>   mypop (1;\^@)

Well, I have finally done it. The patch attached adds support for:

- '/' prototype that is like '$' but that converts barewords to globs so
it can be used on subroutines accepting both a file handler or a file name.

- '^' prototype prefix, that causes the argument of the prototype it
prefixes to default to $_, @_ or %_ from the caller (%_ is probably
useless, but I have added it just for completeness).

- '1', to force the subroutine into being parsed as an unary operator
with unary precedence.


I think that with those extensions it should be possible to mimic almost
any perl built-in.

Cheers,

  - Salva

Thread Previous | Thread Next


nntp.perl.org: Perl Programming lists via nntp and http.
Comments to Ask Bjørn Hansen at ask@perl.org | Group listing | About