... I am trying to pass the value from the script.pl to the module.pm ... how can I send the value from the script.pl to the Foo.pm?

You can't, at least not with the OPed code. The private lexical (my) scalar variable  $hello in Foo.pm in the OPed code is assigned a value once and only once and print-ed once and only once, both events occurring during module inclusion. The  $hello scalar in the OPed Foo.pm has no getter or setter (accessor/mutator) function defined for it that would allow it ever again to be accessed or given another value; it is a private module variable that is not "exposed". Here's a variation of the OPed code with a getter (but still no setter!) for the lexical variable:

# Foo.pm package Foo; use warnings; use strict; use parent 'Exporter'; our @EXPORT_OK = qw(hello get_hello); my $hello = hello('evaluated during module inclusion'); sub hello { my $h = shift; return $h; } sub get_hello { return $hello; } print 'from package ', __PACKAGE__, " during use: '$hello' \n"; 1;
and:
# use_foo_1.pl use warnings; use strict; use Foo qw(hello get_hello); hello('test'); print 'from package ', __PACKAGE__, " during execution: '", get_hello, + "' \n";
Output:
c:\@Work\Perl\monks\Anonymous Monk\1207065>perl use_foo_1.pl from package Foo during use: 'evaluated during module inclusion' from package main during execution: 'evaluated during module inclusion +'

Update: Here's another example with both a getter and setter defined for the  $hello private module variable. Both the module and it's use-ing script have been changed.

# Foo.pm package Foo; use warnings; use strict; use parent 'Exporter'; our @EXPORT_OK = qw(hello set_hello get_hello); my $hello = hello('evaluated during module inclusion'); sub hello { my $h = shift; return $h; } sub get_hello { return $hello; } sub set_hello { return $hello = shift; } print 'from package ', __PACKAGE__, " during use: '$hello' \n"; 1;
and
# use_foo_2.pl use warnings; use strict; use Foo qw(hello set_hello get_hello); hello('test -- this goes nowhere (void context)'); printf "A: from package %s during execution: '%s' \n", __PACKAGE__, get_hello; set_hello("now we're getting somewhere!"); printf "B: from package %s during execution: '%s' \n", __PACKAGE__, get_hello;
Output:
c:\@Work\Perl\monks\Anonymous Monk\1207065>perl use_foo_2.pl from package Foo during use: 'evaluated during module inclusion' A: from package main during execution: 'evaluated during module inclus +ion' B: from package main during execution: 'now we're getting somewhere!'


Give a man a fish:  <%-{-{-{-<


In reply to Re^5: Passing value to a Perl module by AnomalousMonk
in thread Passing value to a Perl module by Anonymous Monk

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.