in reply to trouble with packages/eval/variable-scoping

Hmm which Perl version / OS did you use?

I don't have your problems, but only tested on my mobile with termux.

use v5.12; use warnings; say $]; my $t = 42; sub foo { say eval $_[0] } foo('$t'); -UUU:----F1 t_dyna_scope.pl All L4 (Perl) ---- -*- mode: compilation; default-directory: "~/perl/" \ -*- Compilation started at Thu Aug 29 18:00:33 perl t_dyna_scope.pl 5.036000 42 Compilation finished at Thu Aug 29 18:00:33

Cheers Rolf
(addicted to the Perl Programming Language :)
see Wikisyntax for the Monastery

Replies are listed 'Best First'.
Re^2: trouble with packages/eval/variable-scoping (caller's scope)
by LanX (Saint) on Aug 30, 2024 at 09:22 UTC
    Answering my own question, $t was still in the pad of the calling scope in my example. ¹

    Otherwise the problem persists:

    use v5.12; use warnings; say $]; { my $t = 42; sub foo { say eval $_[0] } } foo('$t'); -UUU:----F1 t_dyna_scope.pl All L13 (CPerl) --- -*- mode: compilation; default-directory: "~/perl/" \ -*- Compilation started at Fri Aug 30 11:25:02 perl t_dyna_scope.pl 5.036000 Variable "$t" is not available at (eval 1) line 1. Use of uninitialized value in say at t_dyna_scope.pl\ line 10. Compilation finished at Fri Aug 30 11:25:02

    Cheers Rolf
    (addicted to the Perl Programming Language :)
    see Wikisyntax for the Monastery

    Update

    ¹) interestingly a way to access private vars in the dynamic scope ... Quite surprising

    Update

    It's not dynamic scope either, probably only file scope... Needs further testing