Hi All,

I have some issues with exporting the variable from caller perl script to the package. I have written a small print function which will print the passed message on screen as well as log it to a file. This function is in a package named secondPackage.pm, the code looks like

package secondPakage; use strict; use vars qw($VERSION @ISA @EXPORT @EXPORT_OK %EXPORT_TAGS); @ISA = qw(Exporter); @EXPORT = qw( &WriteLOG ); @EXPORT_OK = qw(); %EXPORT_TAGS = ( ); our $LOGFile= ""; sub WriteLOG { + my ($msg) = @_; my $ltime=localtime; $ltime=~s/\s/_/g; $ltime=~s/_+/_/g; $ltime=~s/^_|_$//; open my $lFD, '>>', "$LOGFile" or die "Unable to open LOG file |$L +OGFile| for logging...$!"; printf $lFD "\n=== __FILE__ === $ltime -> $msg\n"; print "\n=== __FILE__ === $ltime -> $msg\n"; close($lFD); + } 1;

In the above package, $LOGFile is an global variable.

now there is a caller script called firstCaller.pl which will call the above function to write the logs like this,

#script firstCaller.pl use secondPackage; our $LOGFile=$ARGV[0]; WriteLOG("...Script is started...\n";
I am trying to initialize that variable like this also our $secondPackage::LOGFile=$ARGV[0]; but still no luck, it is not initializing the LOGFile variable. Also, i have one more package called masterPackage.pm which will also need to use above writeLOG function.
package masterPakage; use strict; use vars qw($VERSION @ISA @EXPORT @EXPORT_OK %EXPORT_TAGS); @ISA = qw(Exporter); @EXPORT = qw( &functions_to_be_exported ); @EXPORT_OK = qw(); + %EXPORT_TAGS = ( ); blah.. blah.. WriteLOG(" i am inside masterPackage...\n"; blah.. blah.. 1;
I am not sure is this the right way of exposing the variables for different packages, but instead of everytime passing the LOGFile variable to WriteLOG function, i was looking at somewhere making it global and whenever i need to write new scripts i can use this package to make sure i don't re write log function.

Please suggest...

Thanks,
ShekarKCB

In reply to How to export variables from caller file to packages by shekarkcb

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.