Beefy Boxes and Bandwidth Generously Provided by pair Networks
more useful options
 
PerlMonks  

Re: many files one log file

by Eliya (Vicar)
on Dec 04, 2013 at 00:49 UTC ( [id://1065514]=note: print w/replies, xml ) Need Help??


in reply to many files one log file

I think one problem is the libraries are loaded before the variables are made available but putting them in BEGIN breaks stuff.

I wonder what exactly you have tried, and how BEGIN did break stuff. As you suspected, initialising the variables in a BEGIN block should work to get the values passed to the lib.

A heavily simplified snippet might help ($foo represents $LOGFH etc. that you want to pass to the lib):

#!/usr/bin/perl -w use strict; my $foo; BEGIN { $foo = "bar" } use License ($foo);

with License.pm being

package License; use strict; my $foo; sub import { shift; $foo = $_[0]; print "foo = $foo\n"; } 1;

should print "foo = bar" when the main program is being run.

Note that $foo has to be declared outside of the BEGIN { }, because the latter implies an extra block scope.

Another way would be to use require instead of use and call the import method explicitly:

my $foo = "bar"; require License; License->import($foo);

In this case, the three statements would be executed at runtime in the sequence they appear, so $foo would be properly initialised before the import method is being called.

Replies are listed 'Best First'.
Re^2: many files one log file
by glenn (Scribe) on Dec 04, 2013 at 14:28 UTC
    Just tried it, like i posted, and it worked like a champ thank you so much.
    Lib template
    MAIN:
    sharedFunctions:
Re^2: many files one log file
by glenn (Scribe) on Dec 04, 2013 at 14:03 UTC
    Thank you. So it broke using the variables in main stating that it must be called main::$LOGFH.

    As for using require do you think this would work?
    Orignal:
    use diagConfig; #$sourcepath, %files, trimwhitespace, cleanupWincuri, +$WINCURI, $PLINK, $CLI open my $LOGFH ,">", $files{log}{file}; #disable write buffer my $stdout = select($LOGFH); $| = 1; select($stdout); use VolumeManager (\$LOGFH, \$writelog); use Initiator (\$LOGFH, \$writelog); my $vm = VolumeManager->new(); my $init = Initiator->new();

    Require:
    use diagConfig; #$sourcepath, %files, trimwhitespace, cleanupWincuri, +$WINCURI, $PLINK, $CLI open my $LOGFH ,">", $files{log}{file}; #disable write buffer my $stdout = select($LOGFH); $| = 1; select($stdout); require VolumeManager; VolumeManager->import(\$LOGFH, \$writelog); my $vm = VolumeManager->new(); require Initiator; Initiator->import(\$LOGFH, \$writelog); my $init = Initiator->new();

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://1065514]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others wandering the Monastery: (6)
As of 2024-04-23 07:09 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found