[24387] in Perl-Users-Digest

home help back first fref pref prev next nref lref last post

Perl-Users Digest, Issue: 6575 Volume: 10

daemon@ATHENA.MIT.EDU (Perl-Users Digest)
Mon May 17 21:05:39 2004

Date: Mon, 17 May 2004 18:05:07 -0700 (PDT)
From: Perl-Users Digest <Perl-Users-Request@ruby.OCE.ORST.EDU>
To: Perl-Users@ruby.OCE.ORST.EDU (Perl-Users Digest)

Perl-Users Digest           Mon, 17 May 2004     Volume: 10 Number: 6575

Today's topics:
        Easier test for "regular file"? <please_post@nomail.edu>
        Global variables in mod_perl <noreply@gunnar.cc>
    Re: Global variables in mod_perl <xx087@freenet.carleton.ca>
    Re: Global variables in mod_perl <mark.clements@kcl.ac.uk>
    Re: Global variables in mod_perl <noreply@gunnar.cc>
    Re: Global variables in mod_perl <noreply@gunnar.cc>
        How to open a socket through a proxy server? (Marcelo)
    Re: How to open a socket through a proxy server? <usenet@morrow.me.uk>
        Net::DNS <bob.lockie.NOSPAM@mail.com>
    Re: Net::DNS <usenet@morrow.me.uk>
        Perl hang on executing remsh <noone@lucent.com>
    Re: Perl hang on executing remsh <vladimir@NoSpamPLZ.net>
    Re: Perl hang on executing remsh <nospam@bigpond.com>
    Re: Perl/CGI IIS setup issues and problems <gmiller@NOTforSPAM.gregmiller.net>
    Re: print all combinations <jidanni@jidanni.org>
    Re: Random Integer <mgjv@tradingpost.com.au>
        reading all process numbers into an array (dn_perl@hotmail.com)
    Re: reading all process numbers into an array <usenet@morrow.me.uk>
    Re: reading all process numbers into an array <nospam@bigpond.com>
    Re: Regular Expression Help? - Newbie Question <vikr@mindspring.com.invalid>
    Re: Style question. <tadmc@augustmail.com>
    Re: Style question. <usenet@morrow.me.uk>
        Understanding Scope with 'my' <roger1023@yahoo.com>
    Re: Understanding Scope with 'my' <usenet@morrow.me.uk>
    Re: Writing a module comprised of several files? <usenet@morrow.me.uk>
    Re: XML::Simple Example perl help needed <davezx1@yahoo.com>
    Re: XML::Simple Example perl help needed (Kevin Collins)
    Re: XML::Simple Example perl help needed <usenet@morrow.me.uk>
        Digest Administrivia (Last modified: 6 Apr 01) (Perl-Users-Digest Admin)

----------------------------------------------------------------------

Date: Tue, 18 May 2004 01:00:48 +0000 (UTC)
From: bill <please_post@nomail.edu>
Subject: Easier test for "regular file"?
Message-Id: <c8bn7v$8ej$1@reader2.panix.com>


I'm writing a package that needs to interact with cvs.  For what
I want to do I need the ability to determine whether or not a file
is considered "regular" by cvs.  According to the cvs docs:

  Special Files

  In normal circumstances, CVS works only with regular files.
  Every file in a project is assumed to be persistent; it must be
  possible to open, read and close them; and so on...

Therefore, I have

  sub is_regular {
    local $_ = shift;  # argument is a filename

    # brute-force elimination
    !(-b||-c||-d||-l||-p||-S||-t);
  }

(Note: having is_regular return -f --instead of the above--would
be no good; for example -f evaluates to 1 if $_ is the name of a
symlink.)

Is there an easier way (at least easier on the eyes) to test for
"regularity" than the brute-force elimination expression above?

Thanks a bunch,

	-bill


------------------------------

Date: Mon, 17 May 2004 20:08:51 +0200
From: Gunnar Hjalmarsson <noreply@gunnar.cc>
Subject: Global variables in mod_perl
Message-Id: <2gsdd5F692l8U1@uni-berlin.de>

If I run this script:

     $count++;
     print "Content-type: text/plain\n\n";
     print $count;

20 times under mod_perl, it outputs:

     1 1 1 1 2 1 1 1 2 2 1 3 2 1 3 2 2 3 4 4

while I would have expected it to output:

     1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20

System:
     Linux Fedora Core 1
     Apache 2.0.47
     mod_perl 1.99_12
     perl v5.8.1

Anybody who has an idea of what I'm missing?

-- 
Gunnar Hjalmarsson
Email: http://www.gunnar.cc/cgi-bin/contact.pl



------------------------------

Date: 17 May 2004 18:22:12 GMT
From: Glenn Jackman <xx087@freenet.carleton.ca>
Subject: Re: Global variables in mod_perl
Message-Id: <slrncai0mk.g18.xx087@smeagol.ncf.ca>

Gunnar Hjalmarsson <noreply@gunnar.cc> wrote:
>  If I run this script:
[...]
>  20 times under mod_perl, it outputs:
>       1 1 1 1 2 1 1 1 2 2 1 3 2 1 3 2 2 3 4 4
>  
>  while I would have expected it to output:
>       1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
[...]
>  Anybody who has an idea of what I'm missing?

http://perl.apache.org/docs/2.0/user/coding/coding.html#Shared_Variables

-- 
Glenn Jackman
NCF Sysadmin
glennj@ncf.ca


------------------------------

Date: Mon, 17 May 2004 20:14:54 +0100
From: Mark Clements <mark.clements@kcl.ac.uk>
Subject: Re: Global variables in mod_perl
Message-Id: <40a90f2e$1@news.kcl.ac.uk>

Gunnar Hjalmarsson wrote:
> If I run this script:
> 
>     $count++;
>     print "Content-type: text/plain\n\n";
>     print $count;
> 
> 20 times under mod_perl, it outputs:
> 
>     1 1 1 1 2 1 1 1 2 2 1 3 2 1 3 2 2 3 4 4
> 
> while I would have expected it to output:
> 
>     1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
apache is traditionally multiprocessed - there is no guarantee that a 
second or subsequent request will be served by the same child. Try

print "$$ $count\n";

Mark


------------------------------

Date: Mon, 17 May 2004 22:58:42 +0200
From: Gunnar Hjalmarsson <noreply@gunnar.cc>
Subject: Re: Global variables in mod_perl
Message-Id: <2gsnblF6gpujU1@uni-berlin.de>

Mark Clements wrote:
> Gunnar Hjalmarsson wrote:
>> If I run this script:
>> 
>>     $count++;
>>     print "Content-type: text/plain\n\n";
>>     print $count;
>>
>> 20 times under mod_perl, it outputs:
>>
>>     1 1 1 1 2 1 1 1 2 2 1 3 2 1 3 2 2 3 4 4
>>
>> while I would have expected it to output:
>>
>>     1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
> 
> apache is traditionally multiprocessed - there is no guarantee that
> a second or subsequent request will be served by the same child.
> Try
> 
> print "$$ $count\n";

Aha, thanks!

As an exercise, just to convince myself, I put together the following
code:

     use SDBM_File;
     use Fcntl;
     print "Content-type: text/plain\n\n";
     my $file = '/path/to/file';
     my $total;

     $count ++;

     tie my %counts, 'SDBM_File', $file, O_RDWR|O_CREAT, 0644;
     $counts{$$} = $count;
     for (sort keys %counts) {
         print "$_: $counts{$_}\n";
         $total += $counts{$_};
     }
     untie %counts;

     print "\nTotal: $total\n";

Now the result is as expected:

     10569: 2
     10570: 3
     10571: 2
     10572: 2
     10573: 3
     10574: 2
     10575: 4
     10576: 2

     Total: 20

Previously I have only played with mod_perl using my IndigoPerl 
installation on Windows 98, which (I suppose) runs as one single process.

-- 
Gunnar Hjalmarsson
Email: http://www.gunnar.cc/cgi-bin/contact.pl



------------------------------

Date: Mon, 17 May 2004 22:58:44 +0200
From: Gunnar Hjalmarsson <noreply@gunnar.cc>
Subject: Re: Global variables in mod_perl
Message-Id: <2gsnboF6gpujU2@uni-berlin.de>

Glenn Jackman wrote:
> http://perl.apache.org/docs/2.0/user/coding/coding.html#Shared_Variables

Hmm.. Mark convinced me that it was caused by multiple processes 
rather than threads. Can you please explain?

-- 
Gunnar Hjalmarsson
Email: http://www.gunnar.cc/cgi-bin/contact.pl



------------------------------

Date: 17 May 2004 12:08:50 -0700
From: mnarvaja@yahoo.com (Marcelo)
Subject: How to open a socket through a proxy server?
Message-Id: <b1570347.0405171108.33d157e7@posting.google.com>

Hi all,
I'm trying to modify a module (Net::YahooMessenger) to make it work
from behind a proxy server. It opens sockets to the server, and I
don't know how to wrap those connections so they go through a proxy
(or a socket) server.

Does anyone know a way or workaround?

Thanks in advance,
Marcelo


------------------------------

Date: Mon, 17 May 2004 20:26:43 +0000 (UTC)
From: Ben Morrow <usenet@morrow.me.uk>
Subject: Re: How to open a socket through a proxy server?
Message-Id: <c8b763$7ki$1@wisteria.csv.warwick.ac.uk>


Quoth mnarvaja@yahoo.com (Marcelo):
> Hi all,
> I'm trying to modify a module (Net::YahooMessenger) to make it work
> from behind a proxy server. It opens sockets to the server, and I
> don't know how to wrap those connections so they go through a proxy
> (or a socket) server.
> 
> Does anyone know a way or workaround?

What sort of proxy server? HTTP? SOCKS?

If you're talking about SOCKS, you can use IO::Socket::Socks. If HTTP,
then you need to make a CONNECT request.

Ben

-- 
It will be seen that the Erwhonians are a meek and long-suffering people,
easily led by the nose, and quick to offer up common sense at the shrine of
logic, when a philosopher convinces them that their institutions are not based 
on the strictest morality.  [Samuel Butler, paraphrased]       ben@morrow.me.uk


------------------------------

Date: Mon, 17 May 2004 19:54:42 -0400
From: Bob <bob.lockie.NOSPAM@mail.com>
Subject: Net::DNS
Message-Id: <2hcqc.13225$qJ5.353150@news20.bellglobal.com>


How do I specify the port for DNS queries in the Net::DNS module?


-- 
-------------------------------------------------------
Remove .NOSPAM from my email address to reply directly.


------------------------------

Date: Tue, 18 May 2004 00:15:22 +0000 (UTC)
From: Ben Morrow <usenet@morrow.me.uk>
Subject: Re: Net::DNS
Message-Id: <c8bkiq$dak$4@wisteria.csv.warwick.ac.uk>


Quoth Bob <bob.lockie.NOSPAM@mail.com>:
> 
> How do I specify the port for DNS queries in the Net::DNS module?

Try perldoc Net::DNS::Resolver:

| port
|    Gets or sets the port to which we send queries.

Ben

-- 
I must not fear. Fear is the mind-killer. I will face my fear and it will pass
through me, and there will be nothing. Only I will remain.
ben@morrow.me.uk                                          Frank Herbert, 'Dune'


------------------------------

Date: Mon, 17 May 2004 13:47:39 -0500
From: "Jeff Shao" <noone@lucent.com>
Subject: Perl hang on executing remsh
Message-Id: <c8b1ce$cl7@netnews.proxy.lucent.com>

SSBhbSB3cml0aW5nIGEgc2NyaXB0IHRvIGRvICJyZW1zaCIgb24gYSBsaXN0IG9mIGhvc3RzLA0K
dGhlIHBlcmwgaGFuZ3MgYXQgdGhlIHNvbWUgcG9pbnQuIEkgc2VhcmNoZWQgYXJvdW5kIHRoZQ0K
d2ViLCBwZW9wbGUgc2VlbSB0byBpbmRpY2F0ZSB0aGVyZSBpcyBhIHByb2JsZW0gd2l0aA0KU1RE
SUQsIFNURE9VVCwgU1RERVJSLg0KDQpBbnlvbmUgaGFzIGEgZ29vZCBzb2x1dGlvbj8NCg0KVGhh
bmtzLA0KDQpKZWZmDQoNCi0tIA0KR3VvcWluZyBTaGFvIChKZWZmKQ0KRW1haWw6IGplZmZzaGFv
QGx1Y2VudC5jb20gIFRlbDogKDYzMCkgNzEzLTEyODkgIEZheDogKDYzMCkgMjI0LTM3OTkNCkx1
Y2VudCBUZWNobm9sb2dpZXMgICAgN0QtNTI1LCAgMjcwMSBMdWNlbnQgTGFuZSwgICBMaXNsZSwg
SUwgNjA1MzINCkhvbWUgUGFnZTogaHR0cDovL253c3d3dy5paC5sdWNlbnQuY29tL35qZWZmc2hh
bw0KDQo=



------------------------------

Date: Mon, 17 May 2004 22:35:49 GMT
From: lostriver <vladimir@NoSpamPLZ.net>
Subject: Re: Perl hang on executing remsh
Message-Id: <97bqc.55234$PJ1.519124@wagner.videotron.net>

On Mon, 17 May 2004 13:47:39 -0500, Jeff Shao wrote:
>
> Anyone has a good solution?

Use ssh.
Net::SSH::Perl is your friend.

-- 
 .signature: No such file or directory


------------------------------

Date: Tue, 18 May 2004 08:40:09 +1000
From: Gregory Toomey <nospam@bigpond.com>
Subject: Re: Perl hang on executing remsh
Message-Id: <2854805.qVi1iz4GYD@GMT-hosting-and-pickle-farming>

lostriver wrote:

> On Mon, 17 May 2004 13:47:39 -0500, Jeff Shao wrote:
>>
>> Anyone has a good solution?
> 
> Use ssh.
> Net::SSH::Perl is your friend.
> 
or "expect' under linux.

gtoomey


------------------------------

Date: Mon, 17 May 2004 21:56:31 GMT
From: Greg Miller <gmiller@NOTforSPAM.gregmiller.net>
Subject: Re: Perl/CGI IIS setup issues and problems
Message-Id: <mtcia0dggu95df972imc5lpbuj3mu4a422@4ax.com>

On 17 May 2004 06:36:16 -0700, photochop@comcast.net (Stephen) wrote:

>This leads me to believe that there's a problem with the application
>mapping for the .pl and .cgi extensions. Any idea of where I need to
>start looking?

	It's buried in the website properties inside of Internet
Services Manager.  It's actually in different places for different
versions of Windows.  For Win2003 server, expand the "Web Sites",
right click on your website, click Properties, click the "Home
Directory" tab, click "Configuration", and you'll see the list of
associations.  It should be in similar places on other versions, but
you might have to poke around a little.
	For the newest versions there's somewhere where you have to
enable something like "Enable executable content for unknown types",
"unknown" meaning anything other than Microsoft's stuff.


------------------------------

Date: Tue, 18 May 2004 05:47:20 +0800
From: Dan Jacobson <jidanni@jidanni.org>
Subject: Re: print all combinations
Message-Id: <87vfiulo9j.fsf@jidanni.org>

Thanks. It seems these
perl -wle '$"=",";print for      <B{V,X}{@{[1..8]}}{@{[A..Z]}}>'
perl -wle '$"=",";print for glob "B{V,X}{@{[1..8]}}{@{[A..Z]}}"'
are about as simple as it gets for me to print out that section of
Taiwan amateur radio callsigns.  <> it turns out is merely the
filename expander described on man perlop I/O Operators section.
I find ranges needn't have quotes for capital letters.


------------------------------

Date: 17 May 2004 23:00:17 GMT
From: Martien Verbruggen <mgjv@tradingpost.com.au>
Subject: Re: Random Integer
Message-Id: <slrncaih00.mhn.mgjv@verbruggen.comdyn.com.au>

On Fri, 14 May 2004 17:56:41 -0400,
	Jeff 'japhy' Pinyan <pinyaj@rpi.edu> wrote:
> On 14 May 2004, Walter Roberson wrote:
> 
>>In article <qz7pc.67201$P35.13308@newssvr25.news.prodigy.com>,
>>Ala Qumsieh  <notvalid@email.com> wrote:
>>:Iain Chalmers wrote:
>>
>>:> $num=sprintf "%u",rand($n+1);
>>
>>:Hmmm .. what's wrong with int()?
>>
>>int() truncates. sprintf rounds. See the perl documentation for int,
>>which suggests "usually" sprintf over int.
> 
> Um, I've seen %.0f used to round, but never %u.  That just truncates.  And
> %.0f is imperfect:
> 
>   printf "%.0f %.0f", 1.5, 2.5;  # 2 2

Not imperfect. This is just one of the possible rounding strategies
that can be chosen. I believe this one's ratified by IEEE, and is
often called "round to even".

Martien
-- 
                        | 
Martien Verbruggen      | Conservatives are not necessarily stupid, but
Trading Post Australia  | most stupid people are conservatives. -- John
                        | Stuart Mill


------------------------------

Date: 17 May 2004 14:45:07 -0700
From: dn_perl@hotmail.com (dn_perl@hotmail.com)
Subject: reading all process numbers into an array
Message-Id: <97314b5b.0405171345.14802e57@posting.google.com>

From command line, the following command gives IDs of all the
processes.
ps -ef | awk -F" "    '{print $2}'

But if I use the command in a perl script within backticks, I get full
listing in the array instead of just the process IDs.

my @list_of_procs ;
@list_of_procs = ` ps -ef | awk -F" "   '{print $2}' ` ;

Why is this so and how do I create an array with just the process IDs?


Thanks in advance.


------------------------------

Date: Mon, 17 May 2004 22:23:37 +0000 (UTC)
From: Ben Morrow <usenet@morrow.me.uk>
Subject: Re: reading all process numbers into an array
Message-Id: <c8be19$ahi$1@wisteria.csv.warwick.ac.uk>


Quoth dn_perl@hotmail.com (dn_perl@hotmail.com):
> From command line, the following command gives IDs of all the
> processes.
> ps -ef | awk -F" "    '{print $2}'
> 
> But if I use the command in a perl script within backticks, I get full
> listing in the array instead of just the process IDs.
> 
> my @list_of_procs ;
> @list_of_procs = ` ps -ef | awk -F" "   '{print $2}' ` ;
> 
> Why is this so and how do I create an array with just the process IDs?

Use Proc::ProcessTable instead, or at least do the split in perl:

my @list_of_procs = map { (split)[1] } `ps -ef`;

The reason the awk fails is because `` do double-quotey interpolation,
so you need

@list_of_procs = `ps -ef | awk -F" " '{print \$2}'`;

or

my $cmd = q/ps -ef | awk -F" " '{print $2}'/;
my @list_of_procs = qx/$cmd/;

Ben

-- 
'Deserve [death]? I daresay he did. Many live that deserve death. And some die
that deserve life. Can you give it to them? Then do not be too eager to deal
out death in judgement. For even the very wise cannot see all ends.'
                                                               ben@morrow.me.uk


------------------------------

Date: Tue, 18 May 2004 08:29:49 +1000
From: Gregory Toomey <nospam@bigpond.com>
Subject: Re: reading all process numbers into an array
Message-Id: <1980856.DG6OIlkEPC@GMT-hosting-and-pickle-farming>

dn_perl@hotmail.com wrote:

> From command line, the following command gives IDs of all the
> processes.
> ps -ef | awk -F" "    '{print $2}'
> 
> But if I use the command in a perl script within backticks, I get full
> listing in the array instead of just the process IDs.
> 
> my @list_of_procs ;
> @list_of_procs = ` ps -ef | awk -F" "   '{print $2}' ` ;
> 
> Why is this so and how do I create an array with just the process IDs?
> 
> 
> Thanks in advance.

You probably want (under linux)

       for (split '\n', qx(ps -o pid --no-headers)) {
        ...
        }

gtoomey


------------------------------

Date: Mon, 17 May 2004 18:43:17 GMT
From: Vik Rubenfeld <vikr@mindspring.com.invalid>
Subject: Re: Regular Expression Help? - Newbie Question
Message-Id: <vikr-A1B59A.11431717052004@news2.west.earthlink.net>

 Chris Mattern <matternc@comcast.net> wrote:

> The deal here is that many of the people replying to your question code
> perl for a living.  They have seen far too much perl code that tries
> to make shortcuts in interpreting HTML by making assumptions about it--
> and having that code break when it is suddenly subjected to HTML that
> doesn't conform to those assumptions.  Such bugs are often hard to 
> dig out.  As a result, they're (rightfully, IMHO) allergic to code
> that assumes anything about HTML input other than that it *is* legal
> HTML.  And considering the amount of broken HTML out there that survives
> because browsers are willing to render it, even that is sometimes 
> assuming too much...

Thanks for the info, Chris. I understand.


------------------------------

Date: Mon, 17 May 2004 17:33:04 -0500
From: Tad McClellan <tadmc@augustmail.com>
Subject: Re: Style question.
Message-Id: <slrncaifd0.1kf.tadmc@magna.augustmail.com>

moller@notvalid.se <moller@notvalid.se> wrote:
> 
> What is the best practice regarding 'use module' statments.
> 
> Should they be put at the top of the file


Yes.

Putting compile-time stuff "way down" in the code is misleading,
you might end up fooling yourself that way.


-- 
    Tad McClellan                          SGML consulting
    tadmc@augustmail.com                   Perl programming
    Fort Worth, Texas


------------------------------

Date: Tue, 18 May 2004 00:12:24 +0000 (UTC)
From: Ben Morrow <usenet@morrow.me.uk>
Subject: Re: Style question.
Message-Id: <c8bkd8$dak$3@wisteria.csv.warwick.ac.uk>


Quoth tadmc@augustmail.com:
> moller@notvalid.se <moller@notvalid.se> wrote:
> > 
> > What is the best practice regarding 'use module' statments.
> > 
> > Should they be put at the top of the file
> 
> Yes.

Well, s/file/lexical scope in which they apply/, especially for pragmas.

Note in particular that if you have several 'package' statements in a
file you will need to re-use any modules you want available from both
namespaces.

> Putting compile-time stuff "way down" in the code is misleading,
> you might end up fooling yourself that way.

Yes.

Ben

-- 
  The cosmos, at best, is like a rubbish heap scattered at random.
                                                         - Heraclitus
  ben@morrow.me.uk


------------------------------

Date: 17 May 2004 19:32:15 GMT
From: Roger <roger1023@yahoo.com>
Subject: Understanding Scope with 'my'
Message-Id: <2004517-213215-640066@foorum.com>


My code is this:

use warnings;
use strict;

    my $number = my_rand();
    print "The number is ".$number;
             {  my $seed = 1;
          sub my_rand (){

          $seed = int(($seed * 1103515245 + 12345) / 65536) % 32768;
          return $seed;
          }

I derived it from an article here: (apparently wrong??)
http://perl.plover.com/FAQs/Namespaces.html#glos_lexical_variable

The issue is that declaring 'seed' inside the brackets as shown above yields an 
error of:
main::my_rand() called too early to check prototype at test_scope.pl line 6.
Use of uninitialized value in multiplication (*) at test_scope.pl line 11.

Basically I understand local, and am working on 'my' ... it would seem as if 
using 'my' one needs to always use it in a sub in the context of returning it as 
an lvalue...
like:
my $number = my_rand();
but like I said the code above does not work the way they:
http://perl.plover.com/FAQs/Namespaces.html#glos_lexical_variable
said it should or I am missing the big picture. BTW before I posted I read 
'Learning Perl' 3rd edition... and prehaps I *should* have 'got it' from that 
the example URL I reference above has me scratching my head...

TIA


-- 
Use our news server 'news.foorum.com' from anywhere.
More details at: http://nnrpinfo.go.foorum.com/


------------------------------

Date: Tue, 18 May 2004 00:04:06 +0000 (UTC)
From: Ben Morrow <usenet@morrow.me.uk>
Subject: Re: Understanding Scope with 'my'
Message-Id: <c8bjtm$dak$1@wisteria.csv.warwick.ac.uk>


Quoth Roger <roger1023@yahoo.com>:
> 
> My code is this:
> 
> use warnings;
> use strict;
> 
>     my $number = my_rand();
>     print "The number is ".$number;
>              {  my $seed = 1;

Why the bizzare indentation?

>           sub my_rand (){
> 
>           $seed = int(($seed * 1103515245 + 12345) / 65536) % 32768;
>           return $seed;
>           }

You have a missing } here.

> I derived it from an article here: (apparently wrong??)
> http://perl.plover.com/FAQs/Namespaces.html#glos_lexical_variable

No, this is a good basic description of how my and local work. The
problem is elsewhere.

> The issue is that declaring 'seed' inside the brackets as shown above yields
> an error of:
> main::my_rand() called too early to check prototype at test_scope.pl line 6.
> Use of uninitialized value in multiplication (*) at test_scope.pl line 11.

The problem is with these:

sub my_rand () {
            ^^

They are called a prototype, for which see perldoc perlsub. It will
suffice to say now that 1. they are an advanced feature of Perl and you
don't need to use them and 2. that perl needs to know a sub's prototype
*before* it encounters any calls to that sub (as the warning said). 

The other warning comes from the fact that the assignment $seed = 1 is
not executed until after the call to my_rand. There are two fixes for
this: either move the whole block up above any calls to the sub, so that
the variable is initialised properly, or, better, make it into a BEGIN
block that will *definitely* be run before anything else. For BEGIN
blocks c.f. perldoc perlmod.

So, to fix your code you want

use warnings;
use strict;

# if you put
# use subs qw/my_rand/;
# here, you can call my_rand without parens

my $number = my_rand();
print "The number is $number\n";

BEGIN {
    my $seed = 1;
    sub my_rand {  # Note: no ()
        $seed = int (...);
        return $seed;
    }
}

Ben

-- 
Musica Dei donum optimi, trahit homines, trahit deos.    |
Musica truces molit animos, tristesque mentes erigit.    |   ben@morrow.me.uk
Musica vel ipsas arbores et horridas movet feras.        |


------------------------------

Date: Mon, 17 May 2004 19:40:44 +0000 (UTC)
From: Ben Morrow <usenet@morrow.me.uk>
Subject: Re: Writing a module comprised of several files?
Message-Id: <c8b4fs$6ga$1@wisteria.csv.warwick.ac.uk>


Quoth Andrew Crabb <ahc@jhu.edu>:
> Question: Say my module is called Image::Munge.  So my main file is 
> called Image/Munge.pm.  Where do I put the other files - in 
> Image/Munge/UtilityFile.pm etc?

That's a good place, yes.

> And if my module (class) Image::Munge 
> uses a utility class UtilityClass, it'd be called 
> Image::Munge::UtilityClass right?  And its file would be 
> Image/Munge/UtilityClass.pm.  But this utility class is not useful on 
> its own - can I prevent it from being 'visible' so that people do not 
> inadvertantly try to use it?

Nope. What you can do is put

=head1 NAME

Image::Munge::UtilityClass - internal helper for Image::Munge

=head1 SYNOPSIS

No public interface.

=head1 DESCRIPTION

This is a helper class for Image::Munge. Do not use it by itself.

=head1 AUTHOR

Copyright (c) 2004 Andrew Crabb <ahc@jhu.edu>

=head1 SEE ALSO

Image::Munge

=cut

in the file. People'll get the message :).

Ben

-- 
Heracles: Vulture! Here's a titbit for you / A few dried molecules of the gall
   From the liver of a friend of yours. / Excuse the arrow but I have no spoon.
(Ted Hughes,        [ Heracles shoots Vulture with arrow. Vulture bursts into ]
 /Alcestis/)        [ flame, and falls out of sight. ]         ben@morrow.me.uk


------------------------------

Date: Mon, 17 May 2004 18:22:14 -0400
From: "David Staschover" <davezx1@yahoo.com>
Subject: Re: XML::Simple Example perl help needed
Message-Id: <TUaqc.254$ri.30053@dfw-read.news.verio.net>

That would have made sense, but it didn't work

"Richard Morse" <remorse@partners.org> wrote in message
news:remorse-AC8BA7.14031717052004@plato.harvard.edu...
> In article <dW5qc.244$ri.29178@dfw-read.news.verio.net>,
>  "David Staschover" <davezx1@yahoo.com> wrote:
>
> > Hi,
> >
> > Can someone help me get this working?
> >
> > Sample XML File test.xml:
> >
> >   <top1>
> >     <field>
> >       <value1>100</value1>
> >       <value2>test value1</value2>
> >     </field>
> >     <field>
> >       <value1>107</value1>
> >       <value2>test value2</value2>
> >     </field>
> >   </top1>
> >
> >
> > This program gets and prints the values for value1 and value2 without a
> > problem:
> >
> > #!/usr/bin/perl
> > use XML::Simple;
> > $simple = XML::Simple->new();
> > $struct = $simple->XMLin("test.xml", forcearray => 1, keeproot => 1);
> > for (@{$struct->{top1}->[0]->{field}})
> > {
> >   print "
> >   $_->{value1}->[0]
> >   $_->{value2}->[0]
> >   ";
> > }
> >
> > How can I get the same output using this XML file (I added <top>)?
> >
> > <top>
> >  <top1>
> >     <field>
> >       <value1>100</value1>
> >       <value2>test value1</value2>
> >     </field>
> >     <field>
> >       <value1>107</value1>
> >       <value2>test value2</value2>
> >     </field>
> >   </top1>
> > <top>
>
> Off the top of my head, probably by changing the for loop to be:
>
>    for (@{$struct->{top}->{top1}->[0]->{field}}) {
>
> HTH,
> Ricky
>
> -- 
> Pukku




------------------------------

Date: Mon, 17 May 2004 23:27:44 GMT
From: spamtotrash@toomuchfiction.com (Kevin Collins)
Subject: Re: XML::Simple Example perl help needed
Message-Id: <slrncaiijf.osh.spamtotrash@doom.unix-guy.com>

In article <TUaqc.254$ri.30053@dfw-read.news.verio.net>, David Staschover
wrote:
> That would have made sense, but it didn't work
> 
> "Richard Morse" <remorse@partners.org> wrote in message
> news:remorse-AC8BA7.14031717052004@plato.harvard.edu...
>> In article <dW5qc.244$ri.29178@dfw-read.news.verio.net>,
>>  "David Staschover" <davezx1@yahoo.com> wrote:
>>
>> > Hi,
>> >
>> > Can someone help me get this working?
>> >
>> > Sample XML File test.xml:
>> >
>> >   <top1>
>> >     <field>
>> >       <value1>100</value1>
>> >       <value2>test value1</value2>
>> >     </field>
>> >     <field>
>> >       <value1>107</value1>
>> >       <value2>test value2</value2>
>> >     </field>
>> >   </top1>
>> >
>> >
>> > This program gets and prints the values for value1 and value2 without a
>> > problem:
>> >
>> > #!/usr/bin/perl
>> > use XML::Simple;
>> > $simple = XML::Simple->new();
>> > $struct = $simple->XMLin("test.xml", forcearray => 1, keeproot => 1);
>> > for (@{$struct->{top1}->[0]->{field}})
>> > {
>> >   print "
>> >   $_->{value1}->[0]
>> >   $_->{value2}->[0]
>> >   ";
>> > }
>> >
>> > How can I get the same output using this XML file (I added <top>)?
>> >
>> > <top>
>> >  <top1>
>> >     <field>
>> >       <value1>100</value1>
>> >       <value2>test value1</value2>
>> >     </field>
>> >     <field>
>> >       <value1>107</value1>
>> >       <value2>test value2</value2>
>> >     </field>
>> >   </top1>
>> > <top>
>>
>> Off the top of my head, probably by changing the for loop to be:
>>
>>    for (@{$struct->{top}->{top1}->[0]->{field}}) {

This seems to do the trick:

for (@{$struct->{top}->[0]->{top1}->[0]->{field}})

However, this is only going to give the values for the 1st <top1> ({top1}->[0]}
inside the 1st <top>({top}->[0]), which may not be what you want. 

Kevin



------------------------------

Date: Tue, 18 May 2004 00:10:08 +0000 (UTC)
From: Ben Morrow <usenet@morrow.me.uk>
Subject: Re: XML::Simple Example perl help needed
Message-Id: <c8bk90$dak$2@wisteria.csv.warwick.ac.uk>

[AARGH. Quoting re-arranged. David, please learn not to top-post]

Quoth Kevin Collins <spamtotrash@toomuchfiction.com>:
> In article <TUaqc.254$ri.30053@dfw-read.news.verio.net>, David Staschover
> wrote:
> > "Richard Morse" <remorse@partners.org> wrote in message
> > news:remorse-AC8BA7.14031717052004@plato.harvard.edu...
> >> In article <dW5qc.244$ri.29178@dfw-read.news.verio.net>,
> >>  "David Staschover" <davezx1@yahoo.com> wrote:

[ snip sample XML file and most of the program ]

> >> > $simple = XML::Simple->new();
> >> > $struct = $simple->XMLin("test.xml", forcearray => 1, keeproot => 1);
> >> > for (@{$struct->{top1}->[0]->{field}})

[ more snippage: an element <top> has been added as the parent of <top1> ]

> >> Off the top of my head, probably by changing the for loop to be:
> >>
> >>    for (@{$struct->{top}->{top1}->[0]->{field}}) {
> >
> > That would have made sense, but it didn't work
> 
> This seems to do the trick:
> 
> for (@{$struct->{top}->[0]->{top1}->[0]->{field}})

You don't need all those ->s, and a little whitespace wouldn't hurt.

for (@{ $struct->{top}[0]{top1}[0]{field} }) {

The reason you need the extra [0] is almost certainly because of the
'forcearray => 1' you specified. Do you read the docs for the modules
you use?

Ben

-- 
I've seen things you people wouldn't believe: attack ships on fire off
the shoulder of Orion; I watched C-beams glitter in the dark near the
Tannhauser Gate. All these moments will be lost, in time, like tears in rain.
Time to die.                                                   ben@morrow.me.uk


------------------------------

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 6575
***************************************


home help back first fref pref prev next nref lref last post