$ bash $> perl -e 'use strict; use warnings; my @v = ( a=> b => 1 )'actually in a function; actually in a constructor for an object (blessed hash), so I was thinking {} when it was new( a=>b=>1).
✓
$ bash $> perl -e 'Obviously I found the bug fairly quickly - but I would prefer to have had a compile-time error or warning rather than a run-time error.
use strict; use warnings;
sub kwargs_func{ print "inside\n"; my %kw = $_[0] ;};
kwargs_func( a=> b => 1 )
'
inside
Odd number of elements in hash assignment at -e line ##.
✓
Q: are there any good uses for fat commas in series?
I am surprised that there was not a 'use warnings' warning for this.
---
I like functions with keyword arguments. In Perl there are two main ways to do this:
func_hash_as_array_arg( kwarg1=>kwval1, kwarg2=>kwval2 )
func_hashref_as_scalar_arg( { kwarg1=>kwval1, kwarg2=>kwval2 } )
which can be mixed with positional in a reasonably nice way
func( posarg1, posarg2, kwarg1=>kwval1, kwarg2=>kwval2 )
func( posarg1, posarg2, { kwarg1=>kwval1, kwarg2=>kwval2 } )
and also in less nice ways
func( { kwarg1=>kwval1, kwarg2=>kwval2 }, varargs1, vargags2, ... )Although I prefer f(k1=>v1) to f({k1=>v1}) - less clutter - the fact that the hashref "keyword argument group" gives a slight bit more compile-time check is interesting. I may flip.
Of course, the real problem is that Perl needs a proper syntax for keyword arguments.
Perl6 does it better.
---
For grins, some related code examples with 2 fat commas in series.
$ bash $> perl -e 'use strict; use warnings; my %v = ( a=> b => 1 )'
Odd number of elements in hash assignment at -e line 1.
✓
$ bash $> perl -e 'use strict; use warnings; my $e = { a=> b => 1 }'
Odd number of elements in anonymous hash at -e line 1.
✓
$ bash $> perl -e 'use strict; use warnings; my $e = [ a=> b => 1 ]'
✓
$ bash $> perl -e '
use strict; use warnings;
sub kwargs_func{ print "inside\n"; my %kw = $_[0] ;};
kwargs_func( a=> b => 1 )
'
inside
Odd number of elements in hash assignment at -e line ##.
✓
$ bash $> perl -e '
use strict; use warnings;
sub kwargs_func{ print "inside\n"; my %kw = %{$_[0]} ;};
kwargs_func( {a=> b => 1} )
'
Odd number of elements in anonymous hash at -e line ##.
inside
✓
---
Not the same problem, but along the same lines: When a fat comma is confusing | Samuel Kaufman [blogs.perl.org]
'via Blog this'