News for hackers. Stuff that matters.
Re:Why I’m Learning Perl 6 by Rjevski on Wednesday July 26, 2017 @08:01AM (#14855412)
My personal issue with Perl is how much of a mess the syntax is (10 different ways of doing the same thing), and the lack of a standard library (CPAN is not a viable replacement).
Re:Why I’m Learning Perl 6 by yellowapple on Wednesday July 26, 2017 @09:43AM (#14856102)
I'll take 10 different ways to do the same thing with different approaches over one "true" way with no flexibility any day.

Perl gives the programmer a lot of rope to work with. Whether one ties that rope into a drawbridge or a noose is the programmer's choice.

Also, CPAN is fantastic and widely supported. I really wish more languages would follow its example, especially when it comes to package namespaces.

Re:Why I’m Learning Perl 6 by IslaDeEncanta on Wednesday July 26, 2017 @09:19AM (#14855959)
Why is CPAN not a viable replacement? It has always been perfectly viable for me.
Re:Why I’m Learning Perl 6 by Rjevski on Wednesday July 26, 2017 @12:06PM (#14857449)
CPAN packages are written by thousands of different people with varying styles (cf. the issue with 10 different ways of doing the same thing), quality standards and maintenance schedules.

In Python I can expect the standard library to be consistent and be maintained as part of the core language. Now I don't mean that everything has to be in the standard library (I'm fine with the database access libraries being third-party for example), but in Perl even the most basic stuff like exceptions is only on CPAN and there are again multiple packages to choose from..

Re:Why I’m Learning Perl 6 by b2gills on Thursday July 27, 2017 @11:53PM (#14871834)
Unfortunately for Python is that many/most of the modules that are included were written when the language was fairly new, so at a time when nobody was an expert in the language.

Which means that perhaps for many of them there is a better module out in the ecosystem that almost nobody uses because they have one extra step to install them.

If you want something close to that in Perl 6, just install Inline::Python and you can use all of those “batteries included” with Python as if they were modules written in Perl 6.

Exceptions in Perl 6 are much easier to deal with than they are in Perl 5.

Re:Why I’m Learning Perl 6 by bloobloobloo on Wednesday July 26, 2017 @10:53AM (#14856654)
> 10 different ways of doing the same thing

Mostly an urban myth. Where it isn't, at least 8 of those ways are actually important.

Exercise: write a python program that prints Python's quoting documentation, without external files, without editing the documentation. Spoiler alert: it's impossible.

Re:Why I’m Learning Perl 6 by dmytrish on Wednesday July 26, 2017 @11:24AM (#14856977)
So, how exactly these different ways of getting sub arguments are useful?

    sub add1 {
        my ($arg1, $arg2) = @_;
        $arg1 + $arg2;
    }

    sub add2 {
        my $arg1 = shift;
        # possibly two screens of dense code here, then
        my $arg2 = shift;
        $arg1 + $arg2;
    }

    sub add3 {
        $_[0] + $_[1]
    }

    # There may be some other ways to extract arguments 
    #  that I am not aware of.
    # It's possible to combine any of the methods!
And this is just argument access, one of the core language primitives.
Re:Why I’m Learning Perl 6 by kbenson on Wednesday July 26, 2017 @11:35AM (#14857095)
> So, how exactly these different ways of getting sub arguments are useful?

Because in the second (add2), you might do something interesting with the second (or third) arguments depending on the first. You might have optional arguments. Your arguments might be a list of items you don't know the size of. It allows you to develop what you need out of the extensible core mechanism. Modules can and have built on that.

> # There may be some other ways to extract arguments

There are. Now you can just do

    sub add( $arg1, $arg2 ) {
        $arg1 + $arg2;
    }
My personal favorite is to use Function::Parameters[1]

    fun add( Num $arg1, Num $arg2 ) {
        $arg1 + $arg2;
    }
which allows named params, type constraints (runtime), default params, etc.

1: https://metacpan.org/pod/Function::Parameters

Re:Why I’m Learning Perl 6 by dmytrish on Wednesday July 26, 2017 @11:41AM (#14857171)
I cannot imagine any way of practical (ab)using this flexibility that could not be covered in Python with its only way of handling arguments.
Re:Why I’m Learning Perl 6 by mirchibajji on Thursday July 27, 2017 @06:47AM (#14864094)
To be a bit pedantic, these are not different ways to get a sub arguments, but accessing an array in different ways ("default array" in this case). If I understand this correctly, your criticism is aimed at how the core language makes arguments available to a sub body, in the default array.

The first example uses the explicitly named default array @_. This is a common pattern, and easy to read.

The second one omits the "default". Note that whilst it is possible to write some dense code between the $arg1 and $arg2, it won't work as expected if the dense code bit has array access - ie., the default array can change in the code.

The third example uses the typical sigils for accessing individual elements within an array (default or otherwise).

I wish the core language had saner defaults, but over time, I've seen some reasonable uses for the different styles:

1. General subs, the same as you example

2. shift removes the first element of the array and returns it. This can make the code more readable in certain cases:

  use Params::Validate 'validate';
  sub add2 {
    my $self = shift;
    my $args = validate (@_, { ... } ); 
  }
3. Slightly less verbose code, for simple one-liners and/or anonymous functions:

  my $calc_functions = {
    'add' => sub { $_[0] + $_[1] },
    'subtract' => sub { $_[0] - $_[1] },
    ...
  };
  my $func = 'add';
  $calc_functions->{$func}->($args);
Re:Why I’m Learning Perl 6 by daotoad on Wednesday July 26, 2017 @03:09PM (#14859188)
The significant part of argument handling in Perl5 is that @_ is a list of aliases to the arguments. You usually see add1 or add2 style argument processing because nice named arguments that are a copies of the function parameters.

add3 shows how to access the aliased arguments directly.

  # break the aliasing of @_ and make undef into 0.
  sub undef_to_zero { @_ = map { $_ // 0 } @_; }

  sub math_stuff {
    # call with identical @_
    &undef_to_zero;
    # Do math without undef warnings.
    my $sum = 0;
    $sum += $_ for @_;
  }

  # Alter all arguments in calls by repeating them
  sub fatten {
    $_ = "$_" x 2 for @_;
  }

  $foo = "abc";
  @bar = ( 1, 2, 4 );
  foo($foo, @bar);
  say $foo; # abcabc
  say for @bar # 11
               # 22
               # 44
Re:Why I’m Learning Perl 6 by kbp on Wednesday July 26, 2017 @12:41PM (#14857788)
You aren't really showing off different ways to get arguments in any of those examples. In all of them, you get the arguments in a list called @_; you're just showing off three different ways to get values out of a list, which Python has plenty of ways to do as well. Translating your examples into Python:

    def add1(*_):
        _ = list(_)

        arg1, arg2 = _
        return arg1 + arg2

    def add2(*_):
        _ = list(_)

        arg1 = _.pop(0)
        arg2 = _.pop(0)
        return arg1 + arg2

    def add3(*_):
        _ = list(_)

        return _[0] + _[1]
Re:Why I’m Learning Perl 6 by dmytrish on Wednesday July 26, 2017 @12:46PM (#14857832)
That does not change the fact that in Python everybody writes

    def add(x, y):
       return x + y
whereas the Perl codebase that I work with has every possible combination of these methods.
Re:Why I’m Learning Perl 6 by kbp on Wednesday July 26, 2017 @12:48PM (#14857857)
But people using Python do at different times use destructuring, pop, and indexing to get values out of a list, which are the different ways of doing the same thing that you demonstrated.

e: Just clarifying that your complaint here is that every sub in Perl 5 just receives a list of its arguments; this isn't a "more than one way to do it" issue.

Re:Why I’m Learning Perl 6 by raiph on Wednesday July 26, 2017 @02:30PM (#14858831)
Why does Python have other options if no one is using them? Are these other options cruft in the language design, the compiler, or just reasonable support for features intended for other, perhaps infrequent, usage?

The old P5 motto TIMTOWTDI was long ago updated to TIMTOWTDIBSCINABTE and P6 adopts the latter. While P6 supports most of the options shown for P5, most folk writing P6 will just write something like:

    sub add($x, $y) { $x + $y }
Re:Why I’m Learning Perl 6 by Diederich on Wednesday July 26, 2017 @04:05PM (#14859756)
I don't mean to detract from your point, but I have a related question:

How much actual cognitive overhead is there associated with three of four different ways of unpacking args?

Speaking for myself (as a long-time Perl/Python/GoLang/etc programmer), when I jump into a language I don't know, it doesn't take me long to get 'muscle memory' for how such things work.

Re:Why I’m Learning Perl 6 by daotoad on Wednesday July 26, 2017 @03:18PM (#14859260)
Come up with a style guide and stick to it. Failure to do that with any language, no matter how restrictive, leads to messy code.

It doesn't matter which way you unpack your args unless you are trying to do something fancy (the sub 1% cases). Just pick one.

Hell, it's easy enough to make a script to rewrite multiple shifts into destructured list assignment or vice versa.

Re:Why I’m Learning Perl 6 by petre on Thursday July 27, 2017 @03:06PM (#14868360)
The second example is just bad code. The third example one would rarely use in oneliners or very shorts subs, maybe in a dispatch table. You can write atrocious code in just about any language. Perl 5.20 finally has function signatures anyway.
Re:Why I’m Learning Perl 6 by liveoneggs on Wednesday July 26, 2017 @03:15PM (#14859228)
you'll be happy to know that the sigils (@) don't change in perl6 for access.
Re:Why I’m Learning Perl 6 by smcdow on Wednesday July 26, 2017 @06:01PM (#14860766)
I see no problem here.
Re:Why I’m Learning Perl 6 by gjm11 on Wednesday July 26, 2017 @11:09AM (#14856803)
I'm not sure I understand your exercise. What do you mean by "Python's quoting documentation", and what do you mean by "editing" it?
Re:Why I’m Learning Perl 6 by bloobloobloo on Thursday July 27, 2017 @08:34PM (#14871008)
> What do you mean by "Python's quoting documentation"

String literals are described by the following lexical definitions:

   stringliteral:   shortstring | longstring
   shortstring:     "'" shortstringitem* "'" | '"' shortstringitem* '"'
   longstring:      "'''" longstringitem* "'''" | '"""' longstringitem* '"""'
   shortstringitem: shortstringchar | escapeseq
   longstringitem:  longstringchar | escapeseq
   shortstringchar: <any ASCII character except "\" or newline or the quote>
   longstringchar:  <any ASCII character except "\">
   escapeseq:       "\" <any ASCII character>

You can't paste that text into a file and write a Python program around it that outputs the same text. You need to encode the quote characters.

More generally, there is no way in (many languages including) Python to treat an arbitrary (yet known) block of data as data.

Re:Why I’m Learning Perl 6 by b2gills on Friday July 28, 2017 @12:07AM (#14871890)
For those that don't know you can use `「` and `」` to quote anything that does not contain `」` and Perl 6 won't process it in any way other than storing it as a Str.

If you needed to have `」` in there you can fall back to `Q{{{{{{…}}}}}}` or `Q:to<END>;␤…␤END␤`

Re:Why I’m Learning Perl 6 by yorwba on Wednesday July 26, 2017 @12:16PM (#14857541)
I was wondering about that, too. Unless `"Python's quoting documentation"` is an easter-egg in "help()" that recursively contains a quoted version of itself, it should be possible to just escape all problematic characters. But maybe that counts as `"editing"`? Not sure.
Re:Why I’m Learning Perl 6 by bloobloobloo on Thursday July 27, 2017 @08:35PM (#14871010)
> maybe that counts as `"editing"`

how would it not