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;
now there is a caller script called firstCaller.pl which will call the above function to write the logs like this,
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.#script firstCaller.pl use secondPackage; our $LOGFile=$ARGV[0]; WriteLOG("...Script is started...\n";
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.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;
In reply to How to export variables from caller file to packages by shekarkcb
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |