Re^3: why doesn't "my ($a,$b)" return a list?
by moritz (Cardinal) on Aug 19, 2010 at 15:06 UTC
|
$ perl -wE 'sub list { 1, 2}; sub enum(\@@) { @_[1,2] = (1, 2)}; enum
+my @a, state ($x, $y); say $x, $y'
12
Or with no prototype:
$ perl -wE 'sub list { 1, 2}; sub enum { @_[0,1] = (1, 2)}; enum my @a
+, state ($x, $y); say $x, $y'
12
To me this looks like the scalar context from the prototype was the problem - our maybe you meant a different problem? If so, care to elaborate?
Perl 6 - links to (nearly) everything that is Perl 6.
| [reply] [d/l] [select] |
|
|
DB<22> sub tst3(\@;@) {print scalar @_}
DB<23> use feature 'state'; tst3 @a, state ($x,$y)
3
Thanks :)
| [reply] [d/l] |
Re^3: why doesn't "my ($a,$b)" return a list?
by ikegami (Patriarch) on Aug 19, 2010 at 15:25 UTC
|
My point was that my, our and state aren't subs but operators and shouldn't be handled as subs.
The named operators (my, print, substr, time, etc) are just like subs in many regards, to the point of being called (builtin) functions.
my happens to have a compile-time effect, but it still behaves like a sub at run-time.
| [reply] [d/l] [select] |
|
|
It might be handy and orthogonal to implement my as a function, but I dare to question this design decision.
I can't possibly imagine any use of calling my in scalar context, since the list must be statically fixed at compile time.
Furthermore it's not intuitive, if shortening the number of my's result in these side effects. I searched for an hour to find the problem...
Perl already has the disadvantage to default to global vars while other languages automatically restrict to a local scope without extra operator...
... one shouldn't be punished for trying to economize keystrokes!
Anyway it's too late... but IMHO at least a warning should be emitted when using a my(LIST) in scalar context.
| [reply] [d/l] |
|
|
if (my $x = ...) { ... }
open(my $fh, ...)
read(..., my $buf, ...)
my $r = \my $s;
Perl already has the disadvantage to default to global vars while other languages automatically restrict to a local scope
What language are you talking about? C, C++, Java, JavaScript and VB all require local declarations.
a warning should be emitted when using a my(LIST) in scalar context.
More precisely, my LIST with a list of more than one variables.
Because of its side-effects, my LIST could legitimately be used in scalar context. This job may be more appropriate for a linter (Perl::Critic). But since that use is rare and easy to rewrite (into something clearer), a warning could be appropriate. Feel free to create a ticket (just need to email perlbug@perl.org).
I only looked quickly, but my and friends might be the only builtins to return something of questionable use in scalar context.
| [reply] [d/l] [select] |
|
|
|
|
| [reply] [d/l] [select] |
|
|
Well, my behaves different in several aspect to other buildins.
A lot of builtins have unique parsing rules. (print, map, sort, system, our, ...) And?
It also doesn't flatten its argument list.
I don't see how you could pass something that could be flattened.
I find consider my to behave like a sub quite a stretch of my imagination.
Sure, typically my is typically used as a directive, but we're talking about using it inside an expression. I don't see any difference between
foo(bar(ARGS));
foo(my(ARGS));
Why do you think my should behave differently than bar?
| [reply] [d/l] [select] |
|
|
|
|
|
|
|
|
|
|