in reply to Re: Indirect variable expansion
in thread Indirect variable expansion

UPDATE

Don't use this code, the correct $M is only visible inside the subprocess, hence the use has to happen inside.


FWIW: you don't need to export explicitly if you use this special bash syntax. Note the missing ;

~$ M="Data::Dumper" perl -M$M -E'say $ENV{M}->VERSION' 2.188

Edit

Furthermore, -M doesn't seem to throw an error for unknown modules.

So adding warnings -w to your execution will help you catch typos

~$ M="Data::Dumb" perl -M$M -wE'say $ENV{M}->VERSION' Use of uninitialized value in say at -e line 1.

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

Replies are listed 'Best First'.
Re^3: Indirect variable expansion
by etj (Priest) on Nov 18, 2024 at 12:55 UTC
    Furthermore, -M doesn't seem to throw an error for unknown modules.
    Yes, it does:
    perl -MNotRight -e 0 Can't locate NotRight.pm in @INC (you may need to install the NotRight + module) (@INC contains: ...
      > Yes, it does:

      Well obviously not for me and the code I posted!

      update

      the problem seems to be bash related, the -M$M doesn't expand the $M I've just set. But Perl's sub-process has the correct ENV.

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

        Well, yes. At the time the variable substitution occurs, the variable has not been set yet. eg.:

        $ quux=foo echo $quux $

        "Fix" it by separating the one command into 2:

        $ quux=foo; echo $quux foo $

        It isn't just bash, BTW. Any Bourne-compatible shell should behave like this.


        🦛