Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Rationale for $!

17 views
Skip to first unread message

Felipe Gasper

unread,
Jan 27, 2016, 1:30:09 AM1/27/16
to perl6-l...@perl.org
Hello,

What is the purpose of having $! in Perl 6?

The global variables in Perl 5 are a constant headache, prompting us to
need to local()ize variables like $@, $!, and $? to avoid unforeseen
consequences like RT #127386 and those documented in Try::Tiny’s POD.

Perl 6 seems to give us both the “right” and the “wrong” solution to
accessing exception variables: we get $_ within a CATCH block (right),
but we also get that global $!--which, to me, seems pathologically wrong.

The need to access the exception outside of a catch() block is not
nonexistent, but why this $! syntactic sugar rather than just expecting
people to do like: http://perl6maven.com/how-to-catch-an-exception-in-perl6

Thank you!

-Felipe Gasper
Houston, TX

Moritz Lenz

unread,
Jan 27, 2016, 7:30:02 AM1/27/16
to perl6-l...@perl.org
Hi,

On 01/27/2016 07:17 AM, Felipe Gasper wrote:
> Hello,
>
> What is the purpose of having $! in Perl 6?
>
> The global variables in Perl 5 are a constant headache, prompting
> us to need to local()ize variables like $@, $!, and $? to avoid
> unforeseen consequences like RT #127386 and those documented in
> Try::Tiny’s POD.
>
> Perl 6 seems to give us both the “right” and the “wrong” solution
> to accessing exception variables: we get $_ within a CATCH block
> (right), but we also get that global $!--which, to me, seems
> pathologically wrong.

But it's not global! None of $_, $/, $! are global.

Cheers,
Moritz

Felipe Gasper

unread,
Jan 27, 2016, 9:30:02 AM1/27/16
to perl6-l...@perl.org
I modified the example I posted before to test that:

---------------
use v6;

my $x = 10;
my $y = 0;

my $z = $x / $y;

my $exception;
{
{
say $z;
CATCH {
default {
$exception = $_;
}
}
}
}

if ($exception) {
say "There was an exception: $exception ($!)";
}

say "still running";
---------------

If you run this, $! is populated even when we’ve gone back a block level
from where the exception was thrown.

So, what *is* the scoping of $!?

-FG

Moritz Lenz

unread,
Jan 27, 2016, 10:30:02 AM1/27/16
to perl6-l...@perl.org

On 01/27/2016 03:15 PM, Felipe Gasper wrote:
> So, what *is* the scoping of $!?

Scoped to a routine, iirc (sub, method, regex)

Felipe Gasper

unread,
Jan 27, 2016, 10:45:02 AM1/27/16
to perl6-l...@perl.org
Interesting. JavaScript programmers that I’ve known bemoan that their
language uses function scoping rather than block scoping.

That also seems incongruent with the built-in block scoping for try/CATCH.

Has Perl 6 embraced function scoping as a major paradigm, then?

But, what is the point of $! at all? The exception is given to the CATCH
block as $_. If I want access to it outside CATCH, isn’t the expected
workflow to save CATCH{$_} to a variable, the way my example does it?

$! is also not mentioned here: http://perl6intro.com/#_exception_handling

-FG

Peter Pentchev

unread,
Jan 27, 2016, 11:00:02 AM1/27/16
to perl6-l...@perl.org
On Wed, Jan 27, 2016 at 10:32:46AM -0500, Felipe Gasper wrote:
[snip]
> But, what is the point of $! at all? The exception is given to the CATCH
> block as $_. If I want access to it outside CATCH, isn’t the expected
> workflow to save CATCH{$_} to a variable, the way my example does it?

Well, it makes it possible to write this concise thing:

try my $f = open ...;
die "Could not open $fname: $!" if $!;

G'luck,
Peter

--
Peter Pentchev ro...@ringlet.net ro...@FreeBSD.org p...@storpool.com
PGP key: http://people.FreeBSD.org/~roam/roam.key.asc
Key fingerprint 2EE7 A7A5 17FC 124C F115 C354 651E EFB0 2527 DF13
signature.asc

Felipe Gasper

unread,
Jan 27, 2016, 11:15:02 AM1/27/16
to perl6-l...@perl.org
On 27 Jan 2016 10:44 AM, Peter Pentchev wrote:
> On Wed, Jan 27, 2016 at 10:32:46AM -0500, Felipe Gasper wrote:
> [snip]
>> But, what is the point of $! at all? The exception is given to the CATCH
>> block as $_. If I want access to it outside CATCH, isn’t the expected
>> workflow to save CATCH{$_} to a variable, the way my example does it?
>
> Well, it makes it possible to write this concise thing:
>
> try my $f = open ...;
> die "Could not open $fname: $!" if $!;
>

Well, the problem there is that we shouldn’t have to check for failures;
failures should check whether we handled them!

Could it not be:

try my $f = open(...) or die …

??

Personally, I don’t mind eval {} in Perl 5; I just $@ were
dynamically-scoped the way $1, $2, etc. are. That would solve most of
the issues with it.

Unrelated, but, does open() not throw on failures anyway? (Noodling with
the perl6 REPL just now seems inconclusive.)

-F

Felipe Gasper

unread,
Jan 27, 2016, 11:15:02 AM1/27/16
to perl6-l...@perl.org
On 27 Jan 2016 10:56 AM, Moritz Lenz wrote:

>> But, what is the point of $! at all?
>
> Convenience. It makes it easy to write commonly-used constructs much
> faster.
>
> My mostly unscientific approach to gather usage of try vs. CATCH in the
> ecosystem:
> moritz@hack:~/p6/perl6-all-modules$ git grep --word CATCH | wc -l
> 461
> moritz@hack:~/p6/perl6-all-modules$ git grep --word try | wc -l
> 864
>
> CATCH is also rather clunky by comparison (requires an explicit block,
> whereas 'try' can be used as a statement prefix; requires a "when" or
> "default" blocks).

Yeah … obviously I wasn’t part of the discussions, but ISTM a more
straightforward syntax could have been found. <shrug>

-F

Moritz Lenz

unread,
Jan 27, 2016, 11:15:02 AM1/27/16
to perl6-l...@perl.org
On 01/27/2016 04:32 PM, Felipe Gasper wrote:
> On 27 Jan 2016 10:15 AM, Moritz Lenz wrote:
>>
>> On 01/27/2016 03:15 PM, Felipe Gasper wrote:
>>> So, what *is* the scoping of $!?
>>
>> Scoped to a routine, iirc (sub, method, regex)
>
> Interesting. JavaScript programmers that I’ve known bemoan that their
> language uses function scoping rather than block scoping.
>
> That also seems incongruent with the built-in block scoping for try/CATCH.
>
> Has Perl 6 embraced function scoping as a major paradigm, then?

I wouldn't say "major paradigma", but it's not the only construct that
uses routine scoping. return() and fail() come to mind, which are also
routine-scoped.

> But, what is the point of $! at all?

Convenience. It makes it easy to write commonly-used constructs much faster.

My mostly unscientific approach to gather usage of try vs. CATCH in the
ecosystem:
moritz@hack:~/p6/perl6-all-modules$ git grep --word CATCH | wc -l

461
moritz@hack:~/p6/perl6-all-modules$ git grep --word try | wc -l
864

CATCH is also rather clunky by comparison (requires an explicit block,
whereas 'try' can be used as a statement prefix; requires a "when" or
"default" blocks).

> $! is also not mentioned here: http://perl6intro.com/#_exception_handling

Feel free to fix that by submitting a pull request :-)

Cheers,
Moritz

Moritz Lenz

unread,
Jan 28, 2016, 4:30:02 PM1/28/16
to perl6-l...@perl.org
Hi,

On 01/28/2016 04:06 PM, Todd C. Olson wrote:
> Is there a way to make the exception be thrown eagerly, at the devision statement rather than waiting until use, at the say statement?

Yes, 'use fatal;'

Cheers,
Moritz
0 new messages