Beefy Boxes and Bandwidth Generously Provided by pair Networks
We don't bite newbies here... much
 
PerlMonks  

[Perl 6]: Small discoveries VI, die

by holli (Monsignor)
on Oct 19, 2017 at 22:41 UTC ( #1201711=perlmeditation: print w/replies, xml ) Need Help??

In Perl 6, die still prints an error to STDERR and exits (unless caught), however adding a newline to the end of the error message will produce a stack trace.

The idiom for printing an error message and stopping the program in Perl 6 is:
note "Error Message" and exit 42; # or some other number (except 0)


holli

You can lead your users to water, but alas, you cannot drown them.

Replies are listed 'Best First'.
Re: [Perl 6]: Small discoveries VI, die
by kcott (Chancellor) on Oct 20, 2017 at 01:25 UTC

    G'day holli,

    This interested me because it seemed to be (sort of) the opposite of what a newline does with Perl5's die. Adding a newline to the message stops additional information such as line number and code source (not actually a stack trace):

    $ perl -e 'die "with info"' with info at -e line 1. $ perl -e 'die "without info\n"' without info $

    However, when I tried that with Perl6, it printed the message (with a newline where added) and the same information regardless of whether a newline had been used:

    $ perl6 -e 'die "with info"' with info in block <unit> at -e line 1 $ perl6 -e 'die "without info\n"' without info in block <unit> at -e line 1 $

    Putting the die in a sub, didn't change the Perl5 output:

    $ perl -e 'sub dead { die "with info" } dead()' with info at -e line 1. $ perl -e 'sub dead { die "without info\n" } dead()' without info $

    But, with Perl6, it gave the same information as before, plus info on the sub calling the die, with and without a newline:

    $ perl6 -e 'sub dead { die "with info" }; dead()' with info in sub dead at -e line 1 in block <unit> at -e line 1 $ perl6 -e 'sub dead { die "without info\n" }; dead()' without info in sub dead at -e line 1 in block <unit> at -e line 1 $

    I tried a further level of calling. The Perl5 output was unchanged. The Perl6 output added the extra level to the stack trace but was otherwise unchanged.

    $ perl -e 'sub drop { dead() } sub dead { die "with info" } drop()' with info at -e line 1. $ perl -e 'sub drop { dead() } sub dead { die "without info\n" } drop( +)' without info $ perl6 -e 'sub drop { dead() }; sub dead { die "with info" }; drop()' with info in sub dead at -e line 1 in sub drop at -e line 1 in block <unit> at -e line 1 $ perl6 -e 'sub drop { dead() }; sub dead { die "without info\n" }; dr +op()' without info in sub dead at -e line 1 in sub drop at -e line 1 in block <unit> at -e line 1 $

    I've tinkered with Perl6 every few years or so, just to see progress, but I'm very much a novice. Please provide some example code and output demonstrating what you're describing; it's entirely possible I've missed the point.

    Assuming the version number refers to a date (YYYY.MM), it looks like my copy of Perl6 is about 18 months old: perhaps the behaviour you describe is new.

    $ perl6 -v This is Rakudo version 2016.04 built on MoarVM version 2016.04 implementing Perl 6.c.

    I'm pretty sure the Perl5 behaviour has always been the same (such as I've shown); I certainly recall using it in v5.6. Just for completeness, my current version:

    $ perl -v | head -2 | tail -1 This is perl 5, version 26, subversion 0 (v5.26.0) built for darwin-th +read-multi-2level

    Update (cosmetic): I removed instances of "ken@ganymede: ~/tmp", which are part of my complete prompt, unrelated to anything I posted, and really just noise. I normally take these out before posting; a few had slipped through this time.

    — Ken

      Hi kcott,
      Assuming the version number refers to a date (YYYY.MM), it looks like my copy of Perl6 is about 18 months old: perhaps the behaviour you describe is new.
      Yes, the version number refers to the date (year and month).

      With the latest available version of Rakudo Star / Perl 6:

      This is Rakudo version 2017.07 built on MoarVM version 2017.07 implementing Perl 6.c.
      I obtain exactly the same results as yours, with the presence or absence of a new line apparently making no difference to the output; and with the subroutine call stack in the output when die is called from a subroutine.

      I may also have missed holli's point, but I can't see a difference.

      And the documentation (https://docs.perl6.org/routine/die) says nothing on that.

        However, when I tried that with Perl6, it printed the message (with a newline where added) and the same information regardless of whether a newline had been used:
        That *is* the point. Wether or not you put a newline at the end of the argument to die, it will print a stack trace. Unlike Perl 5.


        holli

        You can lead your users to water, but alas, you cannot drown them.

        G'day Laurent_R,

        "Yes, the version number refers to the date (year and month)."

        Thanks for the confirmation of the version number format and additional information.

        "I obtain exactly the same results as yours, ..."

        Thanks for taking the time to check that. With holli's clarification of the OP text, I'd say that issue's now resolved.

        — Ken

Re: [Perl 6]: Small discoveries VI, die
by b2gills (Initiate) on Oct 25, 2017 at 00:01 UTC

    In Perl 6 die when called with something other than an instance of an Exception will wrap it in an X::AdHoc and then throw it.

    If you really want it to work more like Perl 5 you could do the following.

    die "foo\n"; CATCH { when X::AdHoc { if .message.ends-with("\n") { note .message.chomp; # $*ERR.print( .message ) exit 1; } else { .rethrow; } } }

    I would recommend against doing this as it could cause problems. For example you could set the environment variable RAKUDO_EXCEPTIONS_HANDLER to JSON, but the above would prevent it from intercepting the exception and outputting JSON.

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlmeditation [id://1201711]
Approved by kcott
help
Chatterbox?
and snow settles gently...

How do I use this? | Other CB clients
Other Users?
Others scrutinizing the Monastery: (6)
As of 2018-07-20 02:49 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    It has been suggested to rename Perl 6 in order to boost its marketing potential. Which name would you prefer?















    Results (422 votes). Check out past polls.

    Notices?