New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Carp::croak doesn't quite replace die - missing <fh> line # phrase #11569
Comments
From root@overkill.sb.litts.netCreated by tlhackque@yahoo.comCarp::croak is documented in the camel book as "working like die does, except misdirecting blame to the caller." Perldoc says "croak - die of errors (from perspective of caller)". This is almost true, however careful inspection reveals that croak does not include file position information ( ", <fh> line $." ) as die does (when it can). It turns out that it *is* possible to determine the current file handle (although I wish it were easier). Carp should do this and add it to the traceback, as it really is useful information. Here's how to generate the missing phrase: $msg = join( '', @_ ) . " at $script line $n"; (add traceback) For extra credit, it would be nice to have an better mechanism to get the current file handle name bound to Test cases (type <cr> to get the output): a) die's behavior: xx at -e line 1, <STDIN> line 1. b) Carp::croak's behavior: xx at -e line 1 Thanks for your consideration. Perl Info
|
From @jkeenanOn Tue Aug 09 15:53:13 2011, root@overkill.sb.litts.net wrote:
Would it be possible for you to prepare this as a patch to Thank you very much. |
The RT System itself - Status changed from 'new' to 'open' |
From @doyOn Wed, Dec 14, 2011 at 03:45:10PM -0800, James E Keenan via RT wrote:
It is also not quite equivalent to die because it is missing the $ perl -E'die foo' I wonder if fixing this would be worth it or not. -doy |
From @cpansproutOn Wed Dec 14 15:55:20 2011, doy@tozt.net wrote:
That has bothered me, too. I think that could be considered a bug fix, -- Father Chrysostomos |
From tlhackque@yahoo.comOn Wed Dec 14 15:45:10 2011, jkeenan wrote:
Sure, attached. Against v5.12.4. It's as in the original report, Note this also fixed the missing '.'. # perl -MCarp -e 'my $x=<STDIN>; sub foo { croak "xx";}; foo();' xx at -e line 1, <STDIN> line 1. Enjoy. |
From tlhackque@yahoo.comCarpFH.patch--- /usr/share/perl5/Carp.pm~ 2011-10-05 12:22:51.000000000 -0400
+++ /usr/share/perl5/Carp.pm 2011-12-14 20:53:52.082610049 -0500
@@ -199,11 +199,21 @@
my $tid = threads->tid;
$tid_msg = " thread $tid" if $tid;
}
my %i = caller_info($i);
- $mess = "$err at $i{file} line $i{line}$tid_msg\n";
+ $mess = "$err at $i{file} line $i{line}$tid_msg";
+ if( defined $. ) {
+ local $@ = '';
+ eval {
+ die;
+ };
+ if($@ =~ /^Died at .*(, <.*?> line \d+).$/ ) {
+ $mess .= $1;
+ }
+ }
+ $mess .= ".\n";
while (my %i = caller_info(++$i)) {
$mess .= "\t$i{sub_name} called at $i{file} line $i{line}$tid_msg\n";
}
|
From @davidnicol
Conversely, "die" retains an unnecessary period which should be removed for |
From @cpansproutOn Wed Dec 14 21:11:34 2011, davidnicol@gmail.com wrote:
I notice you are not following the cleaner style in your prose -- Father Chrysostomos |
From @davidnicolOn Wed, Dec 14, 2011 at 11:32 PM, Father Chrysostomos via RT <
Am I the only one who has ever imagined that dot to be there to separate |
From tlhackque@yahoo.comI reported that croak doesn't replace die as documented. die has the dot; the patch that I provided actually matches on that dot As for croak; I really don't care about this nit. I put the dot into Personally, I think the energy spent arguing over this could be better I'll leave resolution of this in your hands; I've done what I can, and |
From [Unknown Contact. See original ticket]I reported that croak doesn't replace die as documented. die has the dot; the patch that I provided actually matches on that dot As for croak; I really don't care about this nit. I put the dot into Personally, I think the energy spent arguing over this could be better I'll leave resolution of this in your hands; I've done what I can, and |
From @cpansproutOn Thu Dec 15 01:20:45 2011, tlhackque wrote:
Believe me, I’ve been thinking about that. $$. can’t work, as that
I’ve applied your patch without the dot in the interest of getting this Thank you. I still think we should add the dot, but shouldn’t it apply to every BTW, we have a policy that every contributor be listed in the AUTHORS -- Father Chrysostomos |
From tlhackque@yahoo.comOn Thu Dec 15 22:20:38 2011, sprout wrote:
I guess it's a question of how much magic to apply. The thought behind ${^LAST_FH} or some such would work as a name. For efficiency, perhaps Obviously, once one has the name, it can be symbolically de-referenced Documentation for such a variable should probably note that the last FH
I guess that would be more consistent, but slightly riskier. I felt
I'm OK either way. My real time is Timothe Litt, and my stable e-mail |
From @cpansproutOn Fri Dec 16 03:06:17 2011, tlhackque wrote:
Because there is a variable named $$, punctuation variables need braces $. is magical, but it works like a tied variable (as do most [all?] (I know I’m being verbose, but I thought you might want to see my
I like that name.
I don’t understand that last sentence. If ${^LAST_FH} is a typeglob (not a problem, BTW, since typeglobs are
Not necessarily. open my $fh... where $fh is undef creates a new
Certainly.
Speaking of controversy, the deadline for controversial changes before
Added with commit 5aaeb4d.
Considering your use of underscores, I did not change the e-mail address -- Father Chrysostomos |
From tlhackque@yahoo.comOn Fri Dec 16 08:39:20 2011, sprout wrote:
Fair enough. You're doing the work, your reasoning wins.
Sorry, I'll expand. Since (as noted later), the LAST_FH can't be The alternative would be to make @{^LAST_FH} return something like (
|
From tlhackque@yahoo.comSorry, my reply escaped before I was done. On Fri Dec 16 09:12:46 2011, tlhackque wrote:
Fair enough. You're doing the work, your reasoning wins.
Sorry, I'll expand. Since (as noted later), the LAST_FH can't be The alternative would be to make @{^LAST_FH} return something like (
As noted above, you only have $. if the LAST_FH is select()ed.
Returning the FH as the third element of a list would handle all these.
OK, that's in your hands. I wasn't aware of the schedule. Maybe it
I was more worried about e-mail harvesting on this (rt) site than the I appreciate all your effort on this. |
From @doyOn Fri, Dec 16, 2011 at 09:26:01AM -0800, tlhackque via RT wrote:
Alternatively, ${^LAST_FH}->input_line_number. -doy |
From tlhackque@yahoo.comOne more thought. Returning an array would seem to allow using @. as the variable name. The use english version could be @{^LAST_FH}. |
From [Unknown Contact. See original ticket]One more thought. Returning an array would seem to allow using @. as the variable name. The use english version could be @{^LAST_FH}. |
From @cpansproutOn Fri Dec 16 09:26:01 2011, tlhackque wrote:
I’ll admit I had to check, but $. is bound to the last-read handle,
I’m afraid I have to disagree with you there. Having a special array
OK. I’ve changed the e-mail address with commit 9fcef2a. -- Father Chrysostomos |
From @apSo I’m seeing a whole rigmarole for designing a special magical variable Then I see this: * tlhackque via RT <perlbug-followup@perl.org> [2011-12-16 18:45]:
And my first thought is: so why not simply make a `lastread` function It could be a weak keyword (preferably) or a feature (and for once Regards, |
From @cpansproutOn Fri Dec 16 18:13:18 2011, aristotle wrote:
Would it be an lvalue keyword, like pos? -- Father Chrysostomos |
From @davidnicolOn Fri, Dec 16, 2011 at 11:40 PM, Father Chrysostomos via RT <
What possible circumstance could require changing the identity of the On the other hand, if you could localize it, you could hide it and restore |
From @ap* Father Chrysostomos via RT <perlbug-followup@perl.org> [2011-12-17 06:45]:
Definite maybe. And it can always be made lvalue later if someone finds * David Nicol <davidnicol@gmail.com> [2011-12-17 08:20]:
It sits very oddly in `caller`’s return. Do you get different values depending on whether you ask for `caller 0` I don’t think that is the right place for it. Regards, |
From @cpansproutOn Sat Dec 17 03:16:09 2011, aristotle wrote:
I meant that almost as an ironic question. Having a special keyword Do we want to follow the interface of select(), and have Another, more troubling question: I have just discovered that I was -- Father Chrysostomos |
From @cpansproutAttached is a proof of concept. It still needs tests and docs. It will With it applied: $ ./perl -Ilib -le '<foo>; print ${^LAST_FH}; print \${^LAST_FH}; print So with a single new variable, you can get a real typeglob that you can Who could ask for anything more? Later we could even allowed it to be assigned to, such that ${^LAST_FH} -- Father Chrysostomos |
From @cpansproutInline Patchdiff --git a/gv.c b/gv.c
index 3a978f2..9bfea53 100644
--- a/gv.c
+++ b/gv.c
@@ -1818,6 +1818,10 @@ Perl_gv_fetchpvn_flags(pTHX_ const char *nambeg, STRLEN full_len, I32 flags,
if (strEQ(name2, "LOBAL_PHASE"))
goto ro_magicalize;
break;
+ case '\014': /* $^LAST_FH */
+ if (strEQ(name2, "AST_FH"))
+ goto ro_magicalize;
+ break;
case '\015': /* $^MATCH */
if (strEQ(name2, "ATCH"))
goto magicalize;
diff --git a/mg.c b/mg.c
index c55ca63..b1865b7 100644
--- a/mg.c
+++ b/mg.c
@@ -882,6 +882,15 @@ Perl_magic_get(pTHX_ SV *sv, MAGIC *mg)
case '\011': /* ^I */ /* NOT \t in EBCDIC */
sv_setpv(sv, PL_inplace); /* Will undefine sv if PL_inplace is NULL */
break;
+ case '\014': /* ^LAST_FH */
+ if (strEQ(remaining, "AST_FH"))
+ sv_setsv_nomg(
+ sv,
+ PL_last_in_gv && isGV_with_GP(PL_last_in_gv)
+ ? (SV *)PL_last_in_gv
+ : NULL
+ );
+ break;
case '\017': /* ^O & ^OPEN */
if (nextchar == '\0') {
sv_setpv(sv, PL_osname); |
From @rjbs* Father Chrysostomos via RT <perlbug-followup@perl.org> [2011-12-16T11:39:21]
(I agree on both counts.) -- |
From @jkeenanOn Sun Dec 18 00:20:57 2011, sprout wrote:
Are we going to follow up on the approach taken in this patch? Tests? Thank you very much. |
From @cpansproutOn Fri Sep 14 18:29:29 2012, jkeenan wrote:
I had nearly forgotten about this. It was in the back of my mind, but I found out after writing that patch that I was mistaken in thinking my $globref = \*STDIN; But that only works if *STDIN{IO} is defined. Otherwise it dies. But So that leads me to ask? What is the best value for ${^LAST_FH}? My In either case, we are not following existing precedent. select() *${^LAST_FH}; # glob itself, possibly a strict error That third line is not pretty. If we always use a reference: *${^LAST_FH}; # glob itself # If ${^LAST_FH} is undef: If we make ${^LAST_FH} itself a typeglob: ${^LAST_FH}; # typeglob, but a copy of the # If ${^LAST_FH} is undef: Despite the errors with undef, I think I now prefer a reference. Any thoughts? I can probably cook up a proper patch once I have finished fixing the Er, when am I going to get to this? :-) -- Father Chrysostomos |
From @rjbs* Father Chrysostomos via RT <perlbug-followup@perl.org> [2012-09-15T01:28:10]
Same. -- |
From @cpansproutOn Mon Sep 17 17:59:19 2012, perl.p5p@rjbs.manxome.org wrote:
I have now applied it as b932b26. Father Chrysostomos |
@cpansprout - Status changed from 'open' to 'resolved' |
Migrated from rt.perl.org#96672 (status was 'resolved')
Searchable as RT96672$
The text was updated successfully, but these errors were encountered: