[20021] in Perl-Users-Digest

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

Perl-Users Digest, Issue: 2216 Volume: 10

daemon@ATHENA.MIT.EDU (Perl-Users Digest)
Tue Nov 27 06:10:32 2001

Date: Tue, 27 Nov 2001 03:10:12 -0800 (PST)
From: Perl-Users Digest <Perl-Users-Request@ruby.OCE.ORST.EDU>
To: Perl-Users@ruby.OCE.ORST.EDU (Perl-Users Digest)
Message-Id: <1006859412-v10-i2216@ruby.oce.orst.edu>
Content-Type: text

Perl-Users Digest           Tue, 27 Nov 2001     Volume: 10 Number: 2216

Today's topics:
    Re: Problems with Mirror - ftp.pl doesn't get the right (Dirk)
        question about random~~~ help~~~ <swansun@kali.com.cn>
    Re: question about random~~~ help~~~ <bernard.el-hagin@lido-tech.net>
    Re: question about random~~~ help~~~ (Logan Shaw)
    Re: question about random~~~ help~~~ <bernard.el-hagin@lido-tech.net>
    Re: random pages... (Wiliam Stephens)
    Re: =?iso-8859-1?Q?=D1=F0=EE=F7=ED=EE_=EF=F0=E8=EE=E1=F <bernard.el-hagin@lido-tech.net>
    Re: =?iso-8859-1?Q?=D1=F0=EE=F7=ED=EE_=EF=F0=E8=EE=E1=F <tim@vegeta.ath.cx>
    Re: =?iso-8859-1?Q?=D1=F0=EE=F7=ED=EE_=EF=F0=E8=EE=E1=F <bernard.el-hagin@lido-tech.net>
    Re: regexp <michDEL_THIS@bsd.fr.eu.org>
    Re: Serious Regexp help... <ashley@pcraft.com>
    Re: Serious Regexp help... <ashley@pcraft.com>
        Stars instead of Characters (Tijs)
    Re: Stars instead of Characters (Rafael Garcia-Suarez)
    Re: String matching... irc hostmasks <jonni@ifm.liu.nospam.se>
        Win32 Access to X-Windows <david.mohorn@home.com>
        Would this be global variables? (Kit)
    Re: Would this be global variables? <bernard.el-hagin@lido-tech.net>
    Re: Would this be global variables? (Logan Shaw)
        Digest Administrivia (Last modified: 6 Apr 01) (Perl-Users-Digest Admin)

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

Date: 27 Nov 2001 00:38:00 -0800
From: dirk.drexler@web.de (Dirk)
Subject: Re: Problems with Mirror - ftp.pl doesn't get the right octal for chmod
Message-Id: <6f50dc53.0111270038.23f8129c@posting.google.com>

Hello,

thanx for your efforts to solve my problem.
I tested 

    &send( sprintf( "SITE CHMOD %o %s", $mode, $path ) );

and got the same effect: 
---> SITE CHMOD 100644 Testfile.txt
501 CHMOD: Mode value must be between 0 and 0777 


Tests with Bart's: 

        &send( sprintf( "SITE CHMOD %o $path", 0777 & $mode ) );

worked fine. 
Regards from rainy Germany

Dirk Drexler



Bart Lateur <bart.lateur@pandora.be> wrote in message news:<o8gsvtk54eqnlv9tb562r752suhe04h1qk@4ax.com>...
> Dirk wrote:
> 
> >The following error occurs:
> >---> SITE CHMOD 100644 Testfile.txt
> >501 CHMOD: Mode value must be between 0 and 0777 
> >
> >octal 100644 and not octal 0644.
>  
> >
> >        &send( sprintf( "SITE CHMOD %o $path", $mode ) );
> >
> >When I change the line to:
> >
> >        &send( sprintf( "SITE CHMOD 0644 $path", $mode ) );
> >
> >everything works fine, but It's sick to code such silly things :-)
> 
> OK... You need to remove the upper bits. Bitwise and is very nice for
> that.
> 
>         &send( sprintf( "SITE CHMOD %o $path", 0777 & $mode ) );


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

Date: Tue, 27 Nov 2001 17:42:53 +0800
From: "swansun" <swansun@kali.com.cn>
Subject: question about random~~~ help~~~
Message-Id: <9tvn7e$r5f$1@mail.cn99.com>

there is a question about random numbers troubles me many days

using perl to create 5000 numbers that every numbers contains 6 digits.
and there are rules
1.these 5000 numbers must be diffrent with each other
2.these 5000 numbers must contain 2 numbers that end with 2222
3.these 5000 numbers must contain 10 numbers that end with 222.(the number
end with 2222 don't include this rule)
4.these 5000 numbers must contain 100 numbers that end with 02
5.those special numbers must distribute random in 5000 numbers

i'm a perl beginner,so i try many times but don't get the right answer
of that. because i don't know how to cortrol that numbers with special rules
and cortrol their amount.


who can tell me the way to get the correct answer?

any help is appreciated~~~~~

thankx!







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

Date: 27 Nov 2001 09:51:22 GMT
From: Bernard El-Hagin <bernard.el-hagin@lido-tech.net>
Subject: Re: question about random~~~ help~~~
Message-Id: <slrna06re3.qsr.bernard.el-hagin@gdndev25.lido-tech>

On Tue, 27 Nov 2001 17:42:53 +0800, swansun <swansun@kali.com.cn> wrote:
> there is a question about random numbers troubles me many days
> 
> using perl to create 5000 numbers that every numbers contains 6 digits.
> and there are rules
> 1.these 5000 numbers must be diffrent with each other
> 2.these 5000 numbers must contain 2 numbers that end with 2222
> 3.these 5000 numbers must contain 10 numbers that end with 222.(the number
> end with 2222 don't include this rule)
> 4.these 5000 numbers must contain 100 numbers that end with 02
> 5.those special numbers must distribute random in 5000 numbers
> 
> i'm a perl beginner,so i try many times but don't get the right answer
> of that. because i don't know how to cortrol that numbers with special rules
> and cortrol their amount.
> 
> who can tell me the way to get the correct answer?


Could you provide some sample code which you've tried but doesn't
work for you? We'd gladly help you with that.


Cheers,
Bernard


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

Date: 27 Nov 2001 04:19:15 -0600
From: logan@cs.utexas.edu (Logan Shaw)
Subject: Re: question about random~~~ help~~~
Message-Id: <9tvpb3$1gl$1@starbuck.cs.utexas.edu>

In article <9tvn7e$r5f$1@mail.cn99.com>, swansun <swansun@kali.com.cn> wrote:
>there is a question about random numbers troubles me many days
>
>using perl to create 5000 numbers that every numbers contains 6 digits.

Does that mean numbers between 100000 and 999999 or numbers between
000000 and 999999?  I'm going to assume it means numbers between 100000
and 999999.

>and there are rules
>1.these 5000 numbers must be diffrent with each other

In that case, there are two approaches.  The first is to keep track of
the numbers already used and try again if you get one that has been
used.  The second is more complicated:  instead of taking the random
numbers straight, you use them to select an element from a set.  To
start with, the set is all possible 6-digit numbers and you remove ones
as you pick them.  (You'd have to come up with some implementation
of sets where you don't need to store all elements.)

In your case, I recommend the first approach because it's easier and
the performance will be fine with the numbers you've given.

It goes something like this:

	my $needed = 5000;
	my %already;
	my @chosen;

	while ($needed > 0)
	{
	    my $chosen = int (rand (900000)) + 100000;

	    if (not $already{$chosen})
	    {
		$already{$chosen}++;
		push (@chosen, $chosen);
		$needed--;
	    }
	}

>2.these 5000 numbers must contain 2 numbers that end with 2222

Exactly two numbers that end with "2222" or at least two?

If at least two, then all you have to do is choose these numbers before
you chose others.  To do that, just pick them first and make sure to
put them into the array @chosen and the hash %already.  Choosing
six-numbers that end in "2222" is just the same as choosing
two-digit numbers and then adding "2222" to the end of them.  Here's
some example code:

	my $needed_2222 = 2;

	while ($needed_2222 > 0)
	{
	    my $chosen;
	   
	    # choose a number betweeen 10 and 99.
	    $chosen = int (rand (90)) + 10;

	    # add "2222" to the end.
	    $chosen .= "2222";

	    if (not $already{$chosen})
	    {
		$already{$chosen}++;
		push (@chosen, $chosen);
		$needed--;
		$needed_2222--;
	    }
	}

This code would go before the while loop if you were inserting
it into your previous example.

If you need there to be no more than two numbers ending in "2222",
you'll have to insert code elsewhere to make sure that you aren't
generating something that does end with "2222".

>3.these 5000 numbers must contain 10 numbers that end with 222.(the number
>end with 2222 don't include this rule)
>4.these 5000 numbers must contain 100 numbers that end with 02

These are very much like #2, so I'll skip a description of them.

>5.those special numbers must distribute random in 5000 numbers

Does this mean that they must be in a random position within the final
list of numbers?  If so, the easiest way to do this is to put them in
the wrong place (i.e. at the beginning) and then randomly reorder the
array.  Type "perldoc -q shuffle" to find out how to randomly reorder
an array.

It would actually be possible to generate them in the right order to
begin with and avoid shuffling the array, but to do so requires a
pretty good knowledge of probability theory.  (My level of knowledge is
good enough that I know it's possible, but I'd have to spend hours with
books figuring out how to do it...)

>i'm a perl beginner,so i try many times but don't get the right answer
>of that. because i don't know how to cortrol that numbers with special rules
>and cortrol their amount.

It is a difficult problem in any language.  I don't think I fully
understood the specifics of what you need, so hopefully I've given you
enough idea that you can figure out the rest.

  - Logan
-- 
"In order to be prepared to hope in what does not deceive,
 we must first lose hope in everything that deceives."

                                          Georges Bernanos


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

Date: 27 Nov 2001 10:55:31 GMT
From: Bernard El-Hagin <bernard.el-hagin@lido-tech.net>
Subject: Re: question about random~~~ help~~~
Message-Id: <slrna06v6c.qsr.bernard.el-hagin@gdndev25.lido-tech>

On Tue, 27 Nov 2001 18:17:52 +0800, swansun <swansun@kali.com.cn> wrote:
> see the attached file rand.pl
> it is written by me
> but it doesn't give me the answer i want
> 
> can u show me where i ought to correct?


[snipped friggin uuencoded file]


I had a look at your script and I have to say that it's, well, pretty
bad. Luckily for you Logan took the time to post some very good code
and advice which I suggest you use as a starting point for a new script.
Keep in mind the following:


- start your script with the following two lines:

  #!/usr/bin/perl -w
  use strict;

  
- always test the result of open()

	
- use more descriptive names for your variables. In the long run
  i, j, k, l, m won't cut it.

  
- don't chomp the hell out of every single variable you use. Read
  perldoc -f chomp to learn about what chomp does. You clearly
  misunderstand its use.


- @num = ""; doesn't do what you probably think it does. I think
  you want @num = ();


- you use $tmp only once in the whole program and that's when you're
  setting it to "". If you had warnings enabled with the -w switch
  you'd know about that.


There are many other things wrong with your code. I really think it would
be beneficial for you to take the above advice, take Logan's code and
rewrite the script from scratch. We'll help you just don't post friggin'
uuencoded files to the group. If you have a problem with some code just
post a relevant snippet.


Cheers,
Bernard


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

Date: 27 Nov 2001 01:41:10 -0800
From: wil@fbagroup.co.uk (Wiliam Stephens)
Subject: Re: random pages...
Message-Id: <39e3e00a.0111270141.6a31b0fb@posting.google.com>

djones@flintschools.org (galaxy motion) wrote in message news:<fe63d339.0111261320.4432a08c@posting.google.com>...

> Hello...

Hi

> I'm working on a site and I have 50 pages 

Wrong newsgroup.

> but I want them to pop up randomly 

OK. This is the code I use for a set of 5 pages. In your case, I would
throw all the files into a directory and get Perl to slurp up the
directory contents into an array. It would be far more preety than
writing out an array with 50 elements - although there's nothing wrong
with this approach.

#!/usr/local/bin/perl

    use CGI;
    $query = CGI::new();

    print $query->header();

	@allhtml =  ('index.1.html',
                     'index.2.html',
                     'index.3.html',               
                     'index.4.html',  
                     ...
                    );

	my $randhtml = $allhtml[int rand @allhtml];

	open (HTMLDAT, "</path/to/dir/$randhtml");
	@html = <HTMLDAT>;
	close HTMLDAT;

	print @html;


> and I  also want a set of 6 different pages every 100 times
> someone clicks a link on the site.  Can someone help me with this? 

I've lost you there.

Wil


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

Date: 27 Nov 2001 09:21:11 GMT
From: Bernard El-Hagin <bernard.el-hagin@lido-tech.net>
Subject: Re: =?iso-8859-1?Q?=D1=F0=EE=F7=ED=EE_=EF=F0=E8=EE=E1=F0=E5=F2=E5=EC_=E2=E5?= =?iso-8859-1?Q?=ED=F2=E8=EB=FF=F2=EE=F0_=C2=D64-75-12.5_=EB=E5=E2=FB=E9?=
Message-Id: <slrna06plh.qsr.bernard.el-hagin@gdndev25.lido-tech>

On 27 Nov 2001 09:12:17 GMT,   <ukrspezmash@euro.ru> wrote:
> Срочно приобретем вентилятор ВЦ4-75-12.5 левый


<scratches head perplexed>


Cheers,
Bernard


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

Date: Tue, 27 Nov 2001 10:18:44 GMT
From: Tim Hammerquist <tim@vegeta.ath.cx>
Subject: Re: =?iso-8859-1?Q?=D1=F0=EE=F7=ED=EE_=EF=F0=E8=EE=E1=F0=E5=F2=E5=EC_=E2=E5?= =?iso-8859-1?Q?=ED=F2=E8=EB=FF=F2=EE=F0_=C2=D64-75-12.5_=EB=E5=E2=FB=E9?=
Message-Id: <slrna06q66.108.tim@vegeta.ath.cx>

Bernard El-Hagin graced us by uttering:
> On 27 Nov 2001 09:12:17 GMT, <ukrspezmash@euro.ru> wrote:
>> Срочно приобретем вентилятор ВЦ4-75-12.5 левый
> <scratches head perplexed>
>
> Cheers, Bernard

Someone in c.l.py said this was Russian spam, in case you were
wondering. =)

Tim Hammerquist
-- 
But the price of getting what you want
is getting what you once wanted.
    -- Morpheus, The Sandman


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

Date: 27 Nov 2001 10:33:40 GMT
From: Bernard El-Hagin <bernard.el-hagin@lido-tech.net>
Subject: Re: =?iso-8859-1?Q?=D1=F0=EE=F7=ED=EE_=EF=F0=E8=EE=E1=F0=E5=F2=E5=EC_=E2=E5?= =?iso-8859-1?Q?=ED=F2=E8=EB=FF=F2=EE=F0_=C2=D64-75-12.5_=EB=E5=E2=FB=E9?=
Message-Id: <slrna06ttd.qsr.bernard.el-hagin@gdndev25.lido-tech>

On Tue, 27 Nov 2001 10:18:44 GMT, Tim Hammerquist <tim@vegeta.ath.cx> wrote:
> Bernard El-Hagin graced us by uttering:
>> On 27 Nov 2001 09:12:17 GMT, <ukrspezmash@euro.ru> wrote:
>>> Срочно приобретем вентилятор ВЦ4-75-12.5 левый
>> <scratches head perplexed>
> 
> Someone in c.l.py said this was Russian spam, in case you were
> wondering. =)


<\scratches head perplexed>


Cheers,
Bernard


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

Date: 27 Nov 2001 10:44:06 GMT
From: "Michael L. Hostbaek" <michDEL_THIS@bsd.fr.eu.org>
Subject: Re: regexp
Message-Id: <slrna06rjm.30nf.michDEL_THIS@freebsdcluster.dk>

Paul Boardman tried to tell us something, and all I got was:
>  "Michael L. Hostbaek" wrote:
>  
>  $svin =~ s/,/./g;
>  
>  should work fine for you.
>  

OK.

The problem is as follows. (Stupid me, for not seing the backwards
substitution)

 $q->param("price_$i") =~ s/,/./g; 
Gives the error:
[Tue Nov 27 12:20:32 2001] [error] Can't modify non-lvalue subroutine
call at /web/htdocs/ora/user.pl line 724.

However, if I do like this:
$svin =  $q->param("price_$i");
$svin =~ s/,/./g;

Then it works just fine ???? Go figure...

Again, thanks for any help.

-- 
Regards,
Michael L. Hostbaek
-= Thanks for all the fish.. =-



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

Date: Mon, 26 Nov 2001 23:38:00 -0700
From: "Ashley M. Kirchner" <ashley@pcraft.com>
Subject: Re: Serious Regexp help...
Message-Id: <3C0334C8.EB0E033@pcraft.com>

Garry Williams wrote:

> I did and your correction is the ticket:
>
> #!/usr/bin/perl -lw
> use strict;
> $, = "|";
> while (<DATA>) {
>     print /^
>         (\S+)        # $1 =  4x6
>         \s+
>         (\d+)        # $2 =  1
>         \s+
>         (.*?)        # $3 =  Standard Print...
>         \s+
>         ([.\d]+)     # $4 =  .75
>         \s+
>         (\$\d+\.\d+) # $5 =  $0.75
>         $/x
> }
> __DATA__
> 4x6    1        4"x6" Standard Print (full-frame from 35mm) Image:Uploaded Image 1 .75 $0.75
> 4x6    1        4"x6" Standard Print (full-frame from 35mm) Image:Uploaded Image 2 .75 $0.75
> 5x7.5  1        5x7.5 Image:Uploaded Image 3 4.25 $4.25
> 8x12   1        8x12 Classic Full Frame Image:Uploaded Image 4 10.25 $10.25
>
> $ perl try
> 4x6|1|4"x6" Standard Print (full-frame from 35mm) Image:Uploaded Image 1|.75|$0.75
> 4x6|1|4"x6" Standard Print (full-frame from 35mm) Image:Uploaded Image 2|.75|$0.75
> 5x7.5|1|5x7.5 Image:Uploaded Image 3|4.25|$4.25
> 8x12|1|8x12 Classic Full Frame Image:Uploaded Image 4|10.25|$10.25
> $

    Um, that gives me 5 items.  And it should be six (from the first line):

    $1 -> 4x6
    $2 -> 1
    $3 -> 4"x6"
    $4 -> Standard Print (full...
    $5 -> .75
    $6 -> $0.75

    In the above code, you merged $3 and $4 into one.

--
H | "Life is the art of drawing without an eraser." - John Gardner
  +--------------------------------------------------------------------
  Ashley M. Kirchner <mailto:ashley@pcraft.com>   .   303.442.6410 x130
  Director of Internet Operations / SysAdmin    .     800.441.3873 x130
  Photo Craft Laboratories, Inc.            .     3550 Arapahoe Ave, #6
  http://www.pcraft.com ..... .  .    .       Boulder, CO 80303, U.S.A.




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

Date: Tue, 27 Nov 2001 02:47:14 -0700
From: "Ashley M. Kirchner" <ashley@pcraft.com>
Subject: Re: Serious Regexp help...
Message-Id: <3C036122.9CD487E8@pcraft.com>

Andrew Hamm wrote:

> So, are you ok with correcting for that? There have been many messages
> posted to this thread for you.

    Actually, Mona's original post of

my @pieces = $line =~ /^(\S+)\s+(\d+)\s+(\S+)\s+(.+?)\s+(\S+)\s+(\$\S+)$/;

    ...works like a charm, unless I'm missing something.  I did learn quite a bit by the other regexp posted, mainly by following it bit by bit, trying to figure out what it does, where and how.

    And I did manage to break $3 and $4, yes - but not after a long time of staring at the screen.

--
H | "Life is the art of drawing without an eraser." - John Gardner
  +--------------------------------------------------------------------
  Ashley M. Kirchner <mailto:ashley@pcraft.com>   .   303.442.6410 x130
  Director of Internet Operations / SysAdmin    .     800.441.3873 x130
  Photo Craft Laboratories, Inc.            .     3550 Arapahoe Ave, #6
  http://www.pcraft.com ..... .  .    .       Boulder, CO 80303, U.S.A.




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

Date: 27 Nov 2001 02:31:49 -0800
From: tijs.vanduffel@mail.be (Tijs)
Subject: Stars instead of Characters
Message-Id: <b907e785.0111270231.5bc655d0@posting.google.com>

Hi,

Do any of you guys know how I can change normal characters to stars
when I enter a password.  It's for a Perl script in Unix.
There doesn't have to be any security, just the conversion to stars so
you can't read the password at the prompt.

I use this: 
    Term :: ReadLine;
    $term = new Term :: ReadLine 'example';
for input.

Thank you very much.!!


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

Date: 27 Nov 2001 10:42:47 GMT
From: rgarciasuarez@free.fr (Rafael Garcia-Suarez)
Subject: Re: Stars instead of Characters
Message-Id: <slrna06riv.81r.rgarciasuarez@rafael.kazibao.net>

Tijs wrote in comp.lang.perl.misc:
} 
} Do any of you guys know how I can change normal characters to stars
} when I enter a password.  It's for a Perl script in Unix.
} There doesn't have to be any security, just the conversion to stars so
} you can't read the password at the prompt.
} 
} I use this: 
}     Term :: ReadLine;
}     $term = new Term :: ReadLine 'example';
} for input.

That's is the FAQ :
    perldoc -q password

BTW it's better to avoid displaying stars, so people looking over your
shoulder can't guess how many characters there are in the password. The
command-line applications that I use don't display anything when
they require me to enter a password.

-- 
Rafael Garcia-Suarez / http://rgarciasuarez.free.fr/
print map substr($_,7).&$_, grep defined &$_, sort values %::;
sub Just {" another "}; sub Perl {" hacker,\n"};


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

Date: Tue, 27 Nov 2001 07:36:08 +0100
From: "Jonas Nilsson" <jonni@ifm.liu.nospam.se>
Subject: Re: String matching... irc hostmasks
Message-Id: <9tvc6t$mkl$1@news.island.liu.se>

"Mercutio" <mercutio@digitalrice.com> wrote in message
news:9ttlqh$24p$1@newsg1.svr.pol.co.uk...
|
| Jonas Nilsson wrote in message <9ttaf3$e4g$1@news.island.liu.se>...
| >Try this:
| >use strict;
| >my $address = 'Nickname!~name@nice.address.org';
| >my @nomatch =
| >('Nickname!~xxxx@nice.address.org','Nickname!~name@nice.address.com');
| >my $hostmask = '*!*name*@*.address.org';
| >my $hostmatch='^'.join('.*',map "\Q$_",split('\*',$hostmask)).'$';
| >for ($address,@nomatch) {
| > print "$_ is a match\n" if /$hostmatch/i;
| > print "$_ isn't a match\n" unless /$hostmatch/i;
| >}
| >/jN
|
| Cheers, that seems to do the job ok... as a sidenote, I was also sent this
| snippet of code by a friend...
|
| @hostmasks = ("*!*name@*.address.com", *!*other@*.other.org);

Global variable. Unwanted in my opinion.

|
| if (checkAddress("SomeName!myname@this.address.com")) {

You really should use single quotes here. Use strict will tell you why.

|     ...
| }
|
| sub checkAddress {
|     my $hostname = shift;
|     foreach $mask (@hostmasks) {
|         $hostmask = $mask;
|         $hostmask =~ s/\./\\\./g;
|         $hostmask =~ s/\*/\.\*/g;
|         if ($hostname =~ /$hostmask/) {
|             return 1;
|         }
|     }
|     return 0;
| }
|
| Can I get an opinion or two on this by you guys?

In my opinion, my version is better. I use the quote-meta "\Q" which will
turn _every_ unsafe character into a escaped one. The other version just
changes . to \. and thus does not consider the presence of ) ( + ? \ / and
such.

If the mask is entered by a user there will also be a possible security risk
if you don't use quotemeta "\Q".

One other difference. My version uses anchors which will make some
differences like this:

mask          host                  my version    other version
name@*.se     name@some.where.se    match         match
name@*.se     Myname@some.where.se  nomatch       match
name@*.se     name@some.se.com      nomatch       match
*name@*.se    Myname@some.where.se  match         match
name@*.se*    name@some.se.com      match         match

Ofcourse you can also use my version is a sub.

/jN




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

Date: Tue, 27 Nov 2001 05:47:00 GMT
From: "David Mohorn" <david.mohorn@home.com>
Subject: Win32 Access to X-Windows
Message-Id: <oNFM7.19372$V46.4552967@news1.rdc1.va.home.com>

Would someone please explain to me the advantages of using Telnet, Rlogin,
Rsh,  Rexec,  XDCM, etc., when trying to run a Perl/TK application on AIX
Unix from a Microsoft Windows platform.  What is the common method here?  I
appear to get telnet/rlogin working succssfully to a Linux 7.1 system at
home....

david.mohorn@home.com







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

Date: 27 Nov 2001 01:34:22 -0800
From: adrook@yahoo.com (Kit)
Subject: Would this be global variables?
Message-Id: <e756f30f.0111270134.2cbc7f83@posting.google.com>

Just a quick question.  I am writing a single script.  I need to use
variables defined in one subroutine in another subroutine.  How would
I do this?  Would this be declaring global variables?  Thanks!


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

Date: 27 Nov 2001 09:47:17 GMT
From: Bernard El-Hagin <bernard.el-hagin@lido-tech.net>
Subject: Re: Would this be global variables?
Message-Id: <slrna06r6e.qsr.bernard.el-hagin@gdndev25.lido-tech>

On 27 Nov 2001 01:34:22 -0800, Kit <adrook@yahoo.com> wrote:
> Just a quick question.  I am writing a single script.  I need to use
> variables defined in one subroutine in another subroutine.  How would
> I do this?  Would this be declaring global variables?  Thanks!


If you have variables defined in a sub then they're not global
(well, not normally). If you want to get at them then it
depends on how you're invoking the function which uses
those variables.


You didn't provide example code so I'm only guessing at what
exactly it is you're doing, but if you're calling that other
function from the first function you can do this:


sub aaa{
  my $var_a1;
  my $var_a2;

  # do some stuff
  # then call bbb with vars
  # $var_a1 and $var_a2
  
  bbb ($var_a1, $var_a2);
}

sub bbb{
  my ($var_b1, $var_b2) = @_;

  # $var_b1 == $var_a1 and
  # $var_b2 == $var_a2
}


If you're calling bbb somewhere else then it's better to get
aaa() to return the values you're going to use in bbb() rather
than use global variables. For example:


sub aaa{
  my $var_a1;
  my $var_a2;

  # do the wild thang

  return ($var_a1, $var_a2);
}


sub bbb{
  my ($var_b1, $var_b2) = aaa();
}


Cheers,
Bernard


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

Date: 27 Nov 2001 03:56:25 -0600
From: logan@cs.utexas.edu (Logan Shaw)
Subject: Re: Would this be global variables?
Message-Id: <9tvo09$1dp$1@starbuck.cs.utexas.edu>

In article <e756f30f.0111270134.2cbc7f83@posting.google.com>,
Kit <adrook@yahoo.com> wrote:
>Just a quick question.  I am writing a single script.  I need to use
>variables defined in one subroutine in another subroutine.

You don't *need* to.  Probably you want to because you don't know
another way.

Usually the best way is to store everything in an object or some data
structure.  Then you can pass references to this object or data
structure to the subroutines and they can both operate on it as
necessary without sharing any global variables.

Here's an example of the wrong way (using global variables):

    sub print_numbers
    {
	for (my $i = 1; $i <= 8; $i++)
	{
	    print "$line\t$i\n";
	    $line++;
	}
    }

    sub print_strings
    {
	for my $string ("schlemiel", "schlimazel", "Hausenfeffer Inc.")
	{
	    print "$line\t$string\n";
	    $line++;
	}
    }

    sub do_it
    {
	$line = 1;

	print_numbers;
	print_strings;
    }

    do_it;

The right way, using just a data structure (a very simple one):

    sub print_numbers
    {
	my ($line_ref) = @_;

	for (my $i = 1; $i <= 8; $i++)
	{
	    print "${$line_ref}\t$i\n";
	    ${$line_ref}++;
	}
    }

    sub print_strings
    {
	my ($line_ref) = @_;

	for my $string ("schlemiel", "schlimazel", "Hausenfeffer Inc.")
	{
	    print "${$line_ref}\t$string\n";
	    ${$line_ref}++;
	}
    }

    sub do_it
    {
	my $line = 1;

	print_numbers (\$line);
	print_strings (\$line);
    }

    do_it;

Using objects is somewhat similar, except that you access the data in
the object indirectly, through methods on the object.

Two reasons not to use global variables:

(1)  With global variables, you can only have one instance of
     whatever it is that the global variables represent.

(2)  Code with global variables in it is much harder to read
     and understand.

Hope that helps.

  - Logan
-- 
"In order to be prepared to hope in what does not deceive,
 we must first lose hope in everything that deceives."

                                          Georges Bernanos


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

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.  

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


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