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?


In reply to [SOLVED]: eval in perl package by MarcusE

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.