This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
${^LAST_FH}
authorFather Chrysostomos <sprout@cpan.org>
Tue, 18 Sep 2012 06:18:08 +0000 (23:18 -0700)
committerFather Chrysostomos <sprout@cpan.org>
Tue, 18 Sep 2012 06:20:26 +0000 (23:20 -0700)
This was brought up in ticket #96672.

This variable gives access to the last-read filehandle that Perl uses
when it appends ", <STDIN> line 1" to a warning or error message.

gv.c
mg.c
pod/perlvar.pod
t/op/magic.t

diff --git a/gv.c b/gv.c
index 55666f4..08d41db 100644 (file)
--- a/gv.c
+++ b/gv.c
@@ -1787,6 +1787,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 26cabbe..cbae421 100644 (file)
--- a/mg.c
+++ b/mg.c
@@ -906,6 +906,20 @@ 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")) {
+           if (PL_last_in_gv) {
+               assert(isGV_with_GP(PL_last_in_gv));
+               SV_CHECK_THINKFIRST_COW_DROP(sv);
+               prepare_SV_for_RV(sv);
+               SvOK_off(sv);
+               SvRV_set(sv, SvREFCNT_inc_simple_NN(PL_last_in_gv));
+               SvROK_on(sv);
+               sv_rvweaken(sv);
+           }
+           else sv_setsv_nomg(sv, NULL);
+       }
+       break;
     case '\017':               /* ^O & ^OPEN */
        if (nextchar == '\0') {
            sv_setpv(sv, PL_osname);
index 69e18ce..fc99b8e 100644 (file)
@@ -1401,6 +1401,17 @@ how to select the output channel.  See also L<IO::Handle>.
 
 Mnemonic: when you want your pipes to be piping hot.
 
+=item ${^LAST_FH}
+X<${^LAST_FH}>
+
+This read-only variable contains a reference to the last-read filehandle.
+This is set by C<< <HANDLE> >>, C<readline>, C<tell>, C<eof> and C<seek>.
+This is the same handle that C<$.> and C<tell> and C<eof> without arguments
+use.  It is also the handle used when Perl appends ", <STDIN> line 1" to
+an error or warning message.
+
+This variable was added in Perl v5.18.0.
+
 =back
 
 =head3 Variables related to formats
index 5ddba3c..d3f6a48 100644 (file)
@@ -5,7 +5,7 @@ BEGIN {
     chdir 't' if -d 't';
     @INC = '../lib';
     require './test.pl';
-    plan (tests => 171);
+    plan (tests => 176);
 }
 
 # Test that defined() returns true for magic variables created on the fly,
@@ -20,6 +20,7 @@ BEGIN {
        SIG ^OPEN ^TAINT ^UNICODE ^UTF8LOCALE ^WARNING_BITS 1 2 3 4 5 6 7 8
        9 42 & ` ' : ? ! _ - [ ^ ~ = % . ( ) < > \ / $ | + ; ] ^A ^C ^D
        ^E ^F ^H ^I ^L ^N ^O ^P ^S ^T ^V ^W ^UTF8CACHE ::12345 main::98732
+       ^LAST_FH
     )) {
        my $v = $_;
        # avoid using any global vars here:
@@ -604,6 +605,20 @@ SKIP: {
     }
 }
 
+# ${^LAST_FH}
+() = tell STDOUT;
+is ${^LAST_FH}, \*STDOUT, '${^LAST_FH} after tell';
+() = tell STDIN;
+is ${^LAST_FH}, \*STDIN, '${^LAST_FH} after another tell';
+{
+    my $fh = *STDOUT;
+    () = tell $fh;
+    is ${^LAST_FH}, \$fh, '${^LAST_FH} referencing lexical coercible glob';
+}
+# This also tests that ${^LAST_FH} is a weak reference:
+is ${^LAST_FH}, undef, '${^LAST_FH} is undef when PL_last_in_gv is NULL';
+
+
 # ^^^^^^^^^ New tests go here ^^^^^^^^^
 
 SKIP: {