Beefy Boxes and Bandwidth Generously Provided by pair Networks
Think about Loose Coupling
 
PerlMonks  

Non-prefix form of sub

by Maqs (Deacon)
on Oct 08, 2000 at 15:57 UTC ( #35803=perlquestion: print w/ replies, xml ) Need Help??
Maqs has asked for the wisdom of the Perl Monks concerning the following question:

Greetings, ladies and gentlemen!
I have a small question for you :) :
so, i can write my own sub like
sub subname (@paremeters) {...};
and the call it like:
subname (@params);
so, my question is: is it possible to create something in
non-prefix form to get something like
if( $entry &in @array) { #do some great stuff to celebrate it!...};
? You see, my sub &in has to accept both $entry and @array parameters.
Thank you for attention.
/Maqs.

Comment on Non-prefix form of sub
Select or Download Code
Re: Non-prefix form of sub
by cianoz (Friar) on Oct 08, 2000 at 17:27 UTC
    i'm not sure to understand you, if you mean "i whant to pass both a scalar and an array argument" you could pass a scalar and an array reference as in
    mysub($scalar, \@array); #or [@array] if you need a copy of it sub mysub { my @params = @_; my @array_arg; for(@params) { if(ref($_) eq 'ARRAY') { @array_arg = @{$_}; } } ## do other stuff }
Re: Non-prefix form of sub
by merlyn (Sage) on Oct 08, 2000 at 18:19 UTC
    Yes, you can create a subroutine that accepts a scalar distinct from an array or even a list. No, you cannot create subroutines that in general act as infix operators. If you control the operands to a particular object type, you can use overloading to call the subroutine when an existing infix operator of your choice is called on your object type, but that'd probably be as close as you get.

    -- Randal L. Schwartz, Perl hacker

      Thanx, merlyn! You are consistent as always :)
      /Maqs.
Sorta (Re: Non-prefix form of sub)
by tye (Cardinal) on Oct 08, 2000 at 21:37 UTC

    With one minor change, you can make a pretty general in-fix operator:

    sub fred($) { return Overloaded::Module->new( @_ ); } $y= 4 +fred 8;

    So by replacing your "&" with a "+", you have a custom in-fix operator. Note the prototype so that the subroutine consumes a single argument in scalar context like "+" would.

    Yes, this is a pretty hackish thing to do. It can be confusing enough that you might not want to do this at all. That depends on your situation. For most situations I would not recommend this.

    Update: ...except you wanted the right-hand side to be an array. In that case I might use a prototype of (\@) to force an array on the right side.

            - tye (but my friends call me "Tye")
Re (tilly) 1: Non-prefix form of sub
by tilly (Archbishop) on Oct 09, 2000 at 00:53 UTC
    Why do you want to put a prototype in at all?

    I strongly recommend reading this rant on them and reconsidering trying to coerce parameters at all. I almost always declare my own routines like this:

    sub somename { # Code here pulling data out of @_ }
    and if I want to treat the arguments as a scalar and/or as a list, just manipulate the subroutine that way. (For instance shift off of object from a method and then drop the remaining arguments into a hash.)

    In fact other than when I have to modify code someone else wrote, or in playing around for educational purposes, I do not believe that I have ever used a prototype in Perl.

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://35803]
Approved by root
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others exploiting the Monastery: (15)
As of 2014-07-24 13:49 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    My favorite superfluous repetitious redundant duplicative phrase is:









    Results (160 votes), past polls