CGI/Perl Guide | Learning Center | Forums | Advertise | Login
Site Search: in

  Main Index MAIN
INDEX
Search Posts SEARCH
POSTS
Who's Online WHO'S
ONLINE
Log in LOG
IN

Home: Perl Programming Help: Beginner:
Problems setting $main::var in .pm from .pl

 



factorem-mauris
New User

Feb 11, 2014, 10:04 AM

Post #1 of 5 (851 views)
Problems setting $main::var in .pm from .pl Can't Post

I'm trying to set the $main::server_name variable in a module. When I call the .pl, it fails to set the $main::server_name and uses the default. The call to CGminer::summary() works and returns the data that I need.

call.pl

Code
#!/usr/bin/perl 
use strict;
use warnings;
use lib './CGminer/';
use CGminer;
use Data::Dumper;

#These three are what I've tried:
#$cgminer_server = "Worker00";
#our $cgminer_server = "Worker00";
#$main::cgminer_server = "Worker00";

print Dumper(CGminer::summary());


Here is the cgminer.pm relevant section, full source at this url:
https://github.com/cryptographrix/cgminer-api-perl-module/blob/master/CGminer/CGminer.pm


Code
use strict; 
use warnings;

package CGminer;

use JSON;
use IO::Socket::INET;
$| = 1;

# == Important variables
#
# === $cgminer_server
# defaults to 127.0.0.1 but can be overriden in $main::cgminer_server
# $cgminer_port - defaults to 4028 but can be overridden in $main::cgminer_port
#
my $cgminer_server = $main::cgminer_server || "127.0.0.1";
my $cgminer_port = $main::cgminer_port || "4028";



(This post was edited by factorem-mauris on Feb 11, 2014, 10:29 AM)


FishMonger
Veteran / Moderator

Feb 11, 2014, 11:45 AM

Post #2 of 5 (838 views)
Re: [factorem-mauris] Problems setting $main::var in .pm from .pl [In reply to] Can't Post

There are several ways to fix it. This is probably the easiest, but not necessarily the best. The best IMO would be to 1) fix the module then 2) adjust the script accordingly.


Code
#!/usr/bin/perl  
use strict;
use warnings;
use lib './CGminer/';
use CGminer;
use Data::Dumper;

$CGminer::cgminer_server = "Worker00";



factorem-mauris
New User

Feb 11, 2014, 1:28 PM

Post #3 of 5 (831 views)
Re: [FishMonger] Problems setting $main::var in .pm from .pl [In reply to] Can't Post

I updated accordingly and still not seeing the var as populated.
To further test, I removed the default option in the .pm and $main::cgminer_server returns empty.

How do you recommend fixing the module? I'm open to suggestions.


FishMonger
Veteran / Moderator

Feb 11, 2014, 1:52 PM

Post #4 of 5 (827 views)
Re: [factorem-mauris] Problems setting $main::var in .pm from .pl [In reply to] Can't Post

This a a quick and dirty correction. It would be better to export the vars and subs. An even better correction would be to make it an OO module.

Code
package CGminer; 

use strict;
use warnings;

our $cgminer_server = "127.0.0.1";
our $cgminer_port = 4028;



Code
#!/usr/bin/perl 

use strict;
use warnings;
use lib '.';
use CGminer;
use Data::Dumper;

print "Initial Setting\n", '=' x 15, $/,
"Server: $CGminer::cgminer_server\n",
"Port: $CGminer::cgminer_port\n\n";

$CGminer::cgminer_server = "Worker00";
$CGminer::cgminer_port = 4040;

print "After Resetting\n", '=' x 15, $/,
"Server: $CGminer::cgminer_server\n",
"Port: $CGminer::cgminer_port\n\n";


Outputs:
c:\test>call.pl

Quote
Initial Setting
===============
Server: 127.0.0.1
Port: 4028

After Resetting
===============
Server: Worker00
Port: 4040



factorem-mauris
New User

Feb 11, 2014, 2:16 PM

Post #5 of 5 (824 views)
Re: [FishMonger] Problems setting $main::var in .pm from .pl [In reply to] Can't Post

Thanks so much!
I may revisit this as time allows, for now it's functional. Thanks again. Wink

 
 


Search for (options) Powered by Gossamer Forum v.1.2.0

Web Applications & Managed Hosting Powered by Gossamer Threads
Visit our Mailing List Archives