in reply to Re^7: why doesn't "my ($a,$b)" return a list?
in thread why doesn't "my ($a,$b)" return a list?

> No references are taken. So that's both an inaccurate and a very complex way of thinking about it. It's much simpler to think of it as taking a list of variables.

Well, you started claiming an analogy between builtin functions and named operators. :)

from Prototypes

Because the intent of this feature is primarily to let you define subr +outines that work like built-in functions, here are prototypes for so +me other functions that parse almost exactly like the corresponding b +uilt-in.<P> 1. Declared as Called as 2. 3. sub mylink ($$) mylink $old, $new 4. sub myvec ($$$) myvec $var, $offset, 1 5. sub myindex ($$;$) myindex &getstring, "substr" 6. sub mysyswrite ($$$;$) mysyswrite $buf, 0, length($buf) - $off, +$off 7. sub myreverse (@) myreverse $a, $b, $c 8. sub myjoin ($@) myjoin ":", $a, $b, $c 9. sub mypop (\@) mypop @array 10. sub mysplice (\@$$@) mysplice @array, 0, 2, @pushme 11. sub mykeys (\%) mykeys %{$hashref} 12. sub myopen (*;$) myopen HANDLE, $name 13. sub mypipe (**) mypipe READHANDLE, WRITEHANDLE 14. sub mygrep (&@) mygrep { /foo/ } $a, $b, $c 15. sub myrand (;$) myrand 42 16. sub mytime () mytime

> But why would you want to make my() behave differently than foo(). That doesn't remove confusion or problems. It adds to them.

nope, don't think so. Already the fact that you can assign a list, i.e. my(LIST)=LIST is beyond the normal patterns of lvalue subs.

An operator working on lists of variables having a special behavior in scalar context is IMHO strange!

Cheers Rolf

Replies are listed 'Best First'.
Re^9: why doesn't "my ($a,$b)" return a list?
by ikegami (Patriarch) on Aug 19, 2010 at 21:18 UTC

    Well, you started claiming an analogy between builtin functions and named operators. :)

    "builtin function" is another name for "named operator". No analogy was made between the two.

    I understand your point even less.

    Already the fact that you can assign a list, i.e. my(LIST)=LIST is beyond the normal patterns of lvalue sub

    How would changing my affect that statement?

    I don't understand this point either.

    An operator working on lists of variables having a special behavior in scalar context is IMHO strange!

    No operator or builtin or sub can return more than value in scalar context. It simply makes no sense. The only reason you're in scalar context is because what will receive the value cannot handle more than one value.

    map, sort, grep, reverse, and more all work on lists and behave specially in scalar context. What about keys, values, split, splice, m//g, s///g, and more whose primarily purpose is to return a list? They all return something else in scalar context.

    I don't see what you find strange.

      in my world "eq" is a named operator and "grep" is a CORE function.

      maybe "my" is something else, for me it belongs in the same class as "sub", but I don't know about the correct terminology. Keyword? ¹)

      Taking into consideration that even "," is an operator I choosed the word operator.

      Cheers Rolf

      1) perlsyn uses the term "statement".

        Funny you should pick grep as your example of a function because there is no grep function anywhere. It's gets compiled into the grepstart and grepwhile opcodes. You'll find that none of the named operators are compiled into function calls. They are typically compiled into opcodes which the same name as the function or operator.

        $ perl -MO=Concise,-exec -e'time();' 1 <0> enter 2 <;> nextstate(main 1 -e:1) v:{ 3 <0> time[t1] v 4 <@> leave[1 ref] vKP/REFC -e syntax OK

        As a function call, it would look like:

        $ perl -MO=Concise,-exec -e'timex();' 1 <0> enter 2 <;> nextstate(main 1 -e:1) v:{ 3 <0> pushmark s 4 <$> gv(*timex) s/EARLYCV 5 <1> entersub[t1] vKS/TARG,1 6 <@> leave[1 ref] vKP/REFC -e syntax OK

        The point is that there's really no difference between grep, eq and the rest of perlfunc and perlop except for perceived differences. There's a set you perceive as functions, and that's exactly what I said earlier.

        maybe "my" is something else, for me it belongs in the same class as "sub",

        Well, I was saying that when my looks like a function, you shouldn't be surprised that my acts like a function. sub doesn't really look like a function.

        I don't know what this class is you are defining, nor how it relates to this conversation.

        Keyword?

        Yes, "my" is a keyword. It's quite a vague description, though.

        perlsyn uses the term "statement".

        While my $x would be a statement, my is not a statement itself.