develooper Front page | perl.perl5.porters | Postings from July 2012

Re: [perl #113554] my() with empty list causes weird error

Thread Previous | Thread Next
July 9, 2012 08:00
Re: [perl #113554] my() with empty list causes weird error
Message ID:
On Mon, Jul 09, 2012 at 02:26:31PM +0200, Aristotle Pagaltzis wrote:
> * Darin McBride <> [2012-07-08 16:15]:
> No it isn’t, and allowing the latter does not necessitate allowing the
> former. In fact, other than on the RHS of an assignment when clearing
> a list, there is practically nowhere that writing `()` is ever sensible
> in Perl (discounting the use of parens to call a sub when not passing
> arguments; and in fact they are only required for method calls). Yet you
> can sprinkle it just about everywhere. Shouldn’t
>     ($foo, (), $bar, (), $baz) = 1..3;
> be an error?
> > And, Eric, not that I have much say in the matter, but I'd find
> > a simple comment "# no arguments" or even an empty prototype, to be
> > a clearer way to document a lack of arguments ;-)
> I agree that seems a way to confound (novices especially) more than to
> enlighten. Also please do not use prototypes for that.

Prototypes don't help in the case a sub takes optional arguments:

    sub f {say "Got ", @_ ? "" : "no ", "arguments"}
    f + 1;

is different from

    sub f {say "Got ", @_ ? "" : "no ", "arguments"}
    f () + 1;

And it's not just user defined subs, core functions suffer from the same:

    $ perl -wE 'defined ? 1 : 0'
    Warning: Use of "defined" without parentheses is ambiguous at -e line 1.
    Search pattern not terminated or ternary operator parsed as search pattern at -e line 1.
    $ perl -wE 'defined () ? 1 : 0'

Another example where using () makes sense in Perl:

    my $nr_of_matches = () = $str =~ /pat/g;

Not that these examples are arguments in whether or not 'my ()' should
be allowed. (I tend to agree with Karls viewpoint, but I also agree with
the pumpking that in the grand scheme of things, it matters little).
But I do think that if 'my ()' remains to be disallowed, the error message
can be improved. While the use of "stub" may be correct, few programmers
that don't follow p5p will understand what is meant. 


Thread Previous | Thread Next Perl Programming lists via nntp and http.
Comments to Ask Bjørn Hansen at | Group listing | About