develooper Front page | perl.perl5.porters | Postings from January 2018

Re: [perl #132744] bogus syntax errors with 'use subs'

Thread Previous | Thread Next
From:
Zefram
Date:
January 20, 2018 01:42
Subject:
Re: [perl #132744] bogus syntax errors with 'use subs'
Message ID:
20180120014159.GX22873@fysh.org
Sisyphus wrote:
>use subs ('FOO');
>
>if( FOO < 43) { print "a\n" }
...
>Unterminated <> operator at subs.pl line 5.

Both of your failures have the same cause: the "<" is interpreted
as the start of an argument expression, rather than as an operator.
This is not specific to "use subs", and actually happens with any kind
of predeclaration, unless you give an empty prototype:

$ perl -lwe 'sub FOO; if (FOO < 43) { print "a"; } sub FOO { return 42; }'
Unterminated <> operator at -e line 1.
$ perl -lwe 'sub FOO { return 42; } if (FOO < 43) { print "a"; }'
Unterminated <> operator at -e line 1.

The same problem applies to other characters that can be either the
start of a term or an infix operator:

$ perl -lwe 'sub FOO { return 42; } print FOO + 1; print FOO() + 1'
42
43

This isn't a bug, but an intentional grammatical feature.  Using explicit
parens to delimit the empty argument list is one solution, as you've
found.  The other solution is to give the sub an empty prototype, which
alters its grammatical role such that the ambiguous characters will be
interpreted as infix operators:

$ perl -lwe 'sub FOO () { return 42; } if (FOO < 43) { print "a"; } print FOO + 1'
a
43
$ perl -lwe 'sub FOO (); if (FOO < 43) { print "a"; } print FOO + 1; sub FOO () { return 42; }'
a
43

-zefram

Thread Previous | Thread Next


nntp.perl.org: Perl Programming lists via nntp and http.
Comments to Ask Bjørn Hansen at ask@perl.org | Group listing | About