in reply to Please help me understand string eval better

Its the same as if you wrote 3 different files and used perl to run each file, exactly as if you wrote this

use Path::Tiny qw/ path /; path( 'first')->spew( "$$var$transf" ); do 'first'; path( 'second')->spew( $$var.$transf ); do 'second'; path( 'third')->spew( '$$var'.$transf ); do 'third';

If your purpose is calculator, see dont use eval for calculator , and see safest undumper

Replies are listed 'Best First'.
Re^2: Please help me understand string eval better
by haukex (Archbishop) on Jun 15, 2017 at 10:25 UTC
    exactly as if you wrote this

    I get the point you're trying to make and I made a very similar one in my post, but note that this part isn't accurate, since do is different from eval in that code run by do can't see lexicals in the enclosing scope. Specifically:

    use warnings; use strict; use Path::Tiny 'path'; my $var = \"09"; my $transf = "/3+6"; print eval('$$var'.$transf),"\n"; # prints 9 path('third')->spew('$$var'.$transf); print do('third'),"\n"; # prints 6

    Because in the file third, neither warnings or strict are in effect and it can't see my $var, it's the same as undef()/3+6. (Of course if you s/my/our/g, it does work.)