[28589] in Perl-Users-Digest
Perl-Users Digest, Issue: 9953 Volume: 10
daemon@ATHENA.MIT.EDU (Perl-Users Digest)
Fri Nov 10 18:10:24 2006
Date: Fri, 10 Nov 2006 15:10:15 -0800 (PST)
From: Perl-Users Digest <Perl-Users-Request@ruby.OCE.ORST.EDU>
To: Perl-Users@ruby.OCE.ORST.EDU (Perl-Users Digest)
Perl-Users Digest Fri, 10 Nov 2006 Volume: 10 Number: 9953
Today's topics:
Sharing variables between modules <balrog2000@o2.pl>
Re: Sharing variables between modules usenet@DavidFilmer.com
Re: Sharing variables between modules (reading news)
Re: Sharing variables between modules usenet@DavidFilmer.com
Re: Sharing variables between modules <benmorrow@tiscali.co.uk>
Re: Sharing variables between modules <benmorrow@tiscali.co.uk>
Re: Win32::Internet fetch url question <hoffmanamps@citcom.net>
Re: Win32::Internet fetch url question <hoffmanamps@citcom.net>
Re: Win32::Internet fetch url question <glex_no-spam@qwest-spam-no.invalid>
Re: Win32::Internet fetch url question <glex_no-spam@qwest-spam-no.invalid>
Digest Administrivia (Last modified: 6 Apr 01) (Perl-Users-Digest Admin)
----------------------------------------------------------------------
Date: Fri, 10 Nov 2006 20:43:16 +0100
From: Tomek <balrog2000@o2.pl>
Subject: Sharing variables between modules
Message-Id: <op.titfqey5bdw0a5@balrog.tkraus.com>
Hello!
I am using Perl 5.8.8 on FreeBSD. I have to admit that I already lost 2
hours on something what should be obvious, but for me it isn't :(
I have 2 files, 1st is a parenting script, and 2nd is an included module.
I want to read from 2nd module a variable set in parenting script.
I have tried:
File 'a':
#!/usr/local/bin/perl
use b;
our $a='a';
$b::a=$a;
File 'b.pm':
package b;
print $a;
Executing 'a' prints just nothing :(
-----------------------------------------------
I also tried the second variant:
File 'a':
#!/usr/local/bin/perl
use b;
our $a='a';
File 'b.pm':
package b;
use base 'Exporter';
@EXPORT=qw{$a};
our $a;
print $a;
Effect is the same :( No data printed :( I am now clueless, what am I
doing wrong ?
Regards, Tomasz Kraus
------------------------------
Date: 10 Nov 2006 12:39:45 -0800
From: usenet@DavidFilmer.com
Subject: Re: Sharing variables between modules
Message-Id: <1163191184.971293.118140@b28g2000cwb.googlegroups.com>
Tomek wrote:
> Executing 'a' prints just nothing :(
That's because (among other reasons) 'a' has no print command.
I don't think you're clear on what modules are or what they do.
use()ing a module isn't the same thing as inserting and executing the
code. You don't export variables from modules; you export subroutines.
You call the subroutine and pass data to it. For example:
#!/usr/bin/perl
use strict; use warnings;
use b qw{ print_stuff }; #this is a subroutine we'll call
my ($one, $two, $three) = (1,2,3);
print "In the parent program: $one $two $three \n";
print_stuff($one, $two, $three); #call the module's sub, pass data
__END__
The 'b' module looks like this:
sub print_stuff {
my ($foo, $bar, $baz) = @_;
print "Module got this info: $foo $bar $baz \n"
}
1;
--
The best way to get a good answer is to ask a good question.
David Filmer (http://DavidFilmer.com)
------------------------------
Date: Fri, 10 Nov 2006 20:54:18 GMT
From: "Mumia W. (reading news)" <paduille.4060.mumia.w@earthlink.net>
Subject: Re: Sharing variables between modules
Message-Id: <_F55h.4881$l25.4826@newsread4.news.pas.earthlink.net>
On 11/10/2006 01:43 PM, Tomek wrote:
> Hello!
> I am using Perl 5.8.8 on FreeBSD. I have to admit that I already lost 2
> hours on something what should be obvious, but for me it isn't :(
> I have 2 files, 1st is a parenting script, and 2nd is an included
> module. I want to read from 2nd module a variable set in parenting script.
>
> I have tried:
>
> File 'a':
>
> #!/usr/local/bin/perl
> use b;
> our $a='a';
> $b::a=$a;
>
> File 'b.pm':
>
> package b;
> print $a;
>
> Executing 'a' prints just nothing :(
> -----------------------------------------------
> I also tried the second variant:
> File 'a':
>
> #!/usr/local/bin/perl
> use b;
> our $a='a';
>
> File 'b.pm':
>
> package b;
> use base 'Exporter';
> @EXPORT=qw{$a};
> our $a;
> print $a;
>
> Effect is the same :( No data printed :( I am now clueless, what am I
> doing wrong ?
>
> Regards, Tomasz Kraus
Shouldn't that be the other way around? The module that you're use-ing
defines the variables, and the other module prints those values.
--
paduille.4060.mumia.w@earthlink.net
------------------------------
Date: 10 Nov 2006 13:55:08 -0800
From: usenet@DavidFilmer.com
Subject: Re: Sharing variables between modules
Message-Id: <1163195708.464886.139950@e3g2000cwe.googlegroups.com>
Mumia W. (reading news) wrote:
> Shouldn't that be the other way around? The module that you're use-ing
> defines the variables, and the other module prints those values.
I think the OP is confusing use() and do().
--
The best way to get a good answer is to ask a good question.
David Filmer (http://DavidFilmer.com)
------------------------------
Date: Fri, 10 Nov 2006 22:00:34 +0000
From: Ben Morrow <benmorrow@tiscali.co.uk>
Subject: Re: Sharing variables between modules
Message-Id: <2d9f24-7rp.ln1@osiris.mauzo.dyndns.org>
Quoth Tomek <balrog2000@o2.pl>:
> Hello!
> I am using Perl 5.8.8 on FreeBSD. I have to admit that I already lost 2
> hours on something what should be obvious, but for me it isn't :(
> I have 2 files, 1st is a parenting script, and 2nd is an included module.
> I want to read from 2nd module a variable set in parenting script.
>
> I have tried:
>
> File 'a':
>
> #!/usr/local/bin/perl
You *always* want
use strict;
use warnings;
here.
> use b;
Don't use lowercase module names. They are reserved for pragmata
(modules that affect how perl parses your program).
> our $a='a';
Don't use the variables $a and $b. They are slightly magic (they are
used by the sort function) and don't need to be declared. This makes
things harder to debug.
> $b::a=$a;
This is occuring too late. The 'print $a;' below happens as soon as the
'use b;' line is compiled, which is before any of the code in 'a' is
run.
> File 'b.pm':
>
> package b;
You need
use strict;
use warnings;
here, as well, as their effects are limited to the lexical scope they
are used in: in this case, the file.
> print $a;
If you'd used warn instead of print here, it would have been clear that
the statement was in fact being executed but the variable was not set
yet.
If you really want to do something like this, you want
#!/usr/bin/perl
use warnings;
use strict;
# The BEGIN makes the assignment happen at compile time.
# Note that this must also come *before* the use Foo line.
BEGIN { $Foo::foo = 'foo' }
use Foo;
__END__
package Foo;
use warnings;
use strict;
print $foo;
1;
> I also tried the second variant:
> File 'a':
>
> #!/usr/local/bin/perl
> use b;
> our $a='a';
What is the 'our' there for? Do you know what it does?
> File 'b.pm':
>
> package b;
> use base 'Exporter';
> @EXPORT=qw{$a};
> our $a;
> print $a;
Here, again, $b::a is being set too late, after the print command has
already happened.
What are you actually trying to acheive here? Doing real work (as
opposed to initialisation) in the body of a module is generally
considered bad practice: it is much better to export subs from the
module. If your intention was that $a be some sort of setting for your
module, then it is usual *not* to export it but to set it by its full
name in the calling program. If the module's actual work were to be put
in a sub, then you can set the variable before you call the sub,
something like
package Foo;
use strict;
use warnings;
# 'use base' does some odd things to make 'fields' module work.
# It's generally considered better not to use it for Exporter.
require Exporter;
our @ISA = 'Exporter';
# Putting stuff in @EXPORT is usually considered bad style, as
# @EXPORT_OK makes it clearer where things are imported from.
our @EXPORT_OK = qw/do_stuff/;
# It's usual to use Titlecase for package globals, so they stand out
# in the code. This is similar to using ALL_CAPS for constants.
our $Setting;
sub do_stuff {
warn $Setting;
}
1;
__END__
#!/usr/bin/perl
use strict;
use warnings;
use Foo qw/do_stuff/;
$Foo::Setting = 'foo';
do_stuff;
__END__
Ben
--
Like all men in Babylon I have been a proconsul; like all, a slave ... During
one lunar year, I have been declared invisible; I shrieked and was not heard,
I stole my bread and was not decapitated.
~ benmorrow@tiscali.co.uk ~ Jorge Luis Borges, 'The Babylon Lottery'
------------------------------
Date: Fri, 10 Nov 2006 22:08:15 +0000
From: Ben Morrow <benmorrow@tiscali.co.uk>
Subject: Re: Sharing variables between modules
Message-Id: <fr9f24-7rp.ln1@osiris.mauzo.dyndns.org>
Quoth usenet@DavidFilmer.com:
> Tomek wrote:
> > Executing 'a' prints just nothing :(
>
> That's because (among other reasons) 'a' has no print command.
This is not the problem. The print statement is executed, just earlier
than the OP thought.
> I don't think you're clear on what modules are or what they do.
> use()ing a module isn't the same thing as inserting and executing the
> code. You don't export variables from modules; you export subroutines.
While it is not common, you most certainly *can* export variables from
modules. The 'vars' module, for instance, exists solely to export
variables. Other examples are the Socket module, which exports $LF, $CR,
and $CRLF, and the FindBin module, which exports $Bin.
You are correct, of course, that it is generally better practice to
export subs and pass arguments to them, but TMTOWTDI :).
Ben
--
We do not stop playing because we grow old;
we grow old because we stop playing.
benmorrow@tiscali.co.uk
------------------------------
Date: 10 Nov 2006 11:54:24 -0800
From: "EL34" <hoffmanamps@citcom.net>
Subject: Re: Win32::Internet fetch url question
Message-Id: <1163188464.186419.278880@b28g2000cwb.googlegroups.com>
Thanks, but that one line does it all, there is no other code exept
Use::win32::internet.
I was kind of looking for someone who was familiar with the module and
an example, as I stated above.
Please don't get Po'd but your answer was of no help.
J. Gleixner wrote:
> EL34 wrote:
> [...]
> > I am using fetch to open the file.
> > $INET->FetchURL("$FileOnServer");
>
> Next time, show the code that creates $INET.
>
>
> > Is there a way to
> > see if a files exist on a server or if,
> > if the internet connection is down or,
> > any sort of reason why Perl cannot fetch the file within say 10
> > seconds?
> >
> >
> > I looked over the win32::internet docs but I don't quite understand how
> >
> > to go about doing this.
> > a short example would be much appreciated.
>
> Maybe you should look through the documentation for Win32::Internet,
> instead. :-)
>
> I've never used it, however I took a few seconds and searched the
> documentation for 'timeout', and found a 'ConnectTimeout' method. That
> should provide you with a solution.
------------------------------
Date: 10 Nov 2006 12:13:00 -0800
From: "EL34" <hoffmanamps@citcom.net>
Subject: Re: Win32::Internet fetch url question
Message-Id: <1163189580.001496.178050@f16g2000cwb.googlegroups.com>
Use::win32::internet.
$INET = new Win32::Internet();
$INET->FetchURL("$FileOnServer");
Actually, here's all that's required to fetch an URL.
Anyone have a simple working example of using the http session seesion
code and a way to test if the page can be fetched?
I'm not sure what I am looking at in the doc's as I stated above.
EL34 wrote:
> Thanks, but that one line does it all, there is no other code exept
> Use::win32::internet.
>
> I was kind of looking for someone who was familiar with the module and
> an example, as I stated above.
>
> Please don't get Po'd but your answer was of no help.
>
>
> J. Gleixner wrote:
> > EL34 wrote:
> > [...]
> > > I am using fetch to open the file.
> > >
> >
> > Next time, show the code that creates $INET.
> >
> >
> > > Is there a way to
> > > see if a files exist on a server or if,
> > > if the internet connection is down or,
> > > any sort of reason why Perl cannot fetch the file within say 10
> > > seconds?
> > >
> > >
> > > I looked over the win32::internet docs but I don't quite understand how
> > >
> > > to go about doing this.
> > > a short example would be much appreciated.
> >
> > Maybe you should look through the documentation for Win32::Internet,
> > instead. :-)
> >
> > I've never used it, however I took a few seconds and searched the
> > documentation for 'timeout', and found a 'ConnectTimeout' method. That
> > should provide you with a solution.
------------------------------
Date: Fri, 10 Nov 2006 14:59:11 -0600
From: "J. Gleixner" <glex_no-spam@qwest-spam-no.invalid>
Subject: Re: Win32::Internet fetch url question
Message-Id: <4554e82c$0$506$815e3792@news.qwest.net>
EL34 wrote:
Please stop top-posting....
>> J. Gleixner wrote:
>>> EL34 wrote:
>>> [...]
>>>> I am using fetch to open the file.
>>>>
>>> Next time, show the code that creates $INET.
>>>
>>>
>>>> Is there a way to
>>>> see if a files exist on a server or if,
>>>> if the internet connection is down or,
>>>> any sort of reason why Perl cannot fetch the file within say 10
>>>> seconds?
>>>>
>>>>
>>>> I looked over the win32::internet docs but I don't quite understand how
>>>>
>>>> to go about doing this.
>>>> a short example would be much appreciated.
>>> Maybe you should look through the documentation for Win32::Internet,
>>> instead. :-)
>>>
>>> I've never used it, however I took a few seconds and searched the
>>> documentation for 'timeout', and found a 'ConnectTimeout' method. That
>>> should provide you with a solution.
>
> EL34 wrote:
>> Thanks, but that one line does it all, there is no other code exept
>> Use::win32::internet.
Well, there's your problem.
>>
>> I was kind of looking for someone who was familiar with the module and
>> an example, as I stated above.
It seems to be clearly spelled out in the documentation, I don't
need to be familiar with the particular module, if it's well
documented.
>> Please don't get Po'd but your answer was of no help.
Why would I be "Po'd" that I point you right to the method you
need to resolve your stated issue and you say I was no help?
> Use::win32::internet.
use Win32::Internet;
See, this stuff is important. If that's wrong then the one
line from you're script doesn't help anyone figure out
the problem.
> $INET = new Win32::Internet();
> $INET->FetchURL("$FileOnServer");
>
> Actually, here's all that's required to fetch an URL.
> Anyone have a simple working example of using the http session seesion
> code and a way to test if the page can be fetched?
> I'm not sure what I am looking at in the doc's as I stated above.
So you looked at the documentation and found ConnectionTimeout and that
didn't help disconnect the connection after a certain amount of time
waiting? I don't see that in your code.
You could also look at the test code that comes with that module,
which I found conveniently located on CPAN.
http://search.cpan.org/src/JDB/libwin32-0.26/Internet/test.pl
The "GENERIC URL FETCHER" part...
It's amazing the helpful things you find in the documentation.. :-)
Now, if you come back for more, after that, and top-post your 3-line
script saying "It didn't work", then I'll get "Po'd".
Thank You
------------------------------
Date: Fri, 10 Nov 2006 15:03:28 -0600
From: "J. Gleixner" <glex_no-spam@qwest-spam-no.invalid>
Subject: Re: Win32::Internet fetch url question
Message-Id: <4554e92c$0$506$815e3792@news.qwest.net>
J. Gleixner wrote:
> See, this stuff is important. If that's wrong then the one
> line from you're script doesn't help anyone figure out
^^^^^^ your
pet-peeve.. to correct my own grammar. :-)
------------------------------
Date: 6 Apr 2001 21:33:47 GMT (Last modified)
From: Perl-Users-Request@ruby.oce.orst.edu (Perl-Users-Digest Admin)
Subject: Digest Administrivia (Last modified: 6 Apr 01)
Message-Id: <null>
Administrivia:
#The Perl-Users Digest is a retransmission of the USENET newsgroup
#comp.lang.perl.misc. For subscription or unsubscription requests, send
#the single line:
#
# subscribe perl-users
#or:
# unsubscribe perl-users
#
#to almanac@ruby.oce.orst.edu.
NOTE: due to the current flood of worm email banging on ruby, the smtp
server on ruby has been shut off until further notice.
To submit articles to comp.lang.perl.announce, send your article to
clpa@perl.com.
#To request back copies (available for a week or so), send your request
#to almanac@ruby.oce.orst.edu with the command "send perl-users x.y",
#where x is the volume number and y is the issue number.
#For other requests pertaining to the digest, send mail to
#perl-users-request@ruby.oce.orst.edu. Do not waste your time or mine
#sending perl questions to the -request address, I don't have time to
#answer them even if I did know the answer.
------------------------------
End of Perl-Users Digest V10 Issue 9953
***************************************