MarcusE has asked for the wisdom of the Perl Monks concerning the following question:
Hi PerlMonks,
Instead of exporting a package variable I intended to return the value via 'eval' in a module function. Surprisingly the value is just returned, if I also access the variable 'natively'
Module:
package MyModule2; use strict; use Exporter; use vars qw($VERSION @ISA @EXPORT @EXPORT_OK %EXPORT_TAGS); $VERSION = 1.00; @ISA = qw(Exporter); @EXPORT = (); @EXPORT_OK = qw(func3 func4); %EXPORT_TAGS = ( DEFAULT => [qw(&func3)], All => [qw(&func3 &func4)]); my $myscalar = "MyScalarValue"; sub func3 { my $x; my $y; printf "MyModule2/func3: >%s<\n",$_[0]; $x = eval "$_[0]"; return $x; } sub func4 { my $x; my $y; printf "MyModule2/func4: >%s<\n",$_[0]; $x = eval "$_[0]"; $y = $myscalar; return $x; } 1;
script:
#!/usr/bin/perl -w use strict; use MyModule2 qw(:All); printf "MyScript2/func3: >%s<\n",func3('$myscalar'); printf "MyScript2/func4: >%s<\n",func4('$myscalar');
output:
perl MyScript2.pl MyModule2/func3: >$myscalar< Use of uninitialized value in printf at MyScript2.pl line 7. MyScript2/func3: >< MyModule2/func4: >$myscalar< MyScript2/func4: >MyScalarValue<
is there any explanation for this behaviour?
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: eval in perl package
by Eily (Monsignor) on Mar 01, 2018 at 14:44 UTC | |
by LanX (Saint) on Mar 01, 2018 at 15:14 UTC | |
|
Re: eval in perl package
by Eily (Monsignor) on Mar 01, 2018 at 15:12 UTC | |
|
Re: eval in perl package
by hippo (Archbishop) on Mar 01, 2018 at 14:47 UTC | |
|
Re: eval in perl package (updated getter %exported_lex) )
by LanX (Saint) on Mar 01, 2018 at 14:20 UTC | |
by MarcusE (Initiate) on Mar 01, 2018 at 16:12 UTC | |
by LanX (Saint) on Mar 01, 2018 at 16:18 UTC |