shekarkcb has asked for the wisdom of the Perl Monks concerning the following question:

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

Replies are listed 'Best First'.
Re: How to export variables from caller file to packages
by thenaz (Beadle) on Aug 08, 2011 at 16:25 UTC

    You said you tried our $secondPackage::LOGFile=$ARGV[0];. This is almost correct--just drop the "our".

    $secondPackage::LOGFILE = $ARGV[0];
      Thanks for the reply.

      Still i am unable to get the variable value passed to the masterPacake/secondPackage. I did print the value on the caller package, i can see the value but in the secondPackage/masterPackage i don't see any value for LOGFile passed from caller :( ,
      Is it because , -> in secondPackage, i made our $LOGFile; this was required, since in secondPackage under WriteLOG function, the variable LOGFile is global and when running the script it complained use of un initialized function etc... so i made our $LOGFile in secondPackage, and in caller.pl (as suggested) i made

      $secondPackage::LOGFile = "LOGS"."_"."$timestamp".".txt"; print "\n-- i have |$secondPackage::LOGFile| as log file name --!\n";
      This showed me the value "LOGS_some_time_stamp.txt" it is actually correct.But i got error on the WriteLOG function saying no value is initialized for |$LOGFile| ...
      I am totally stuck... can anybody share their thoughts? Any idea i can use built in module which does similar work of logging...


      Thanks,
      ShekarKCB.

        I think the problem may be a misspelling of "secondPackage" in the package declaration line. (Missing the "c".)