[25396] in Perl-Users-Digest

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

Perl-Users Digest, Issue: 7641 Volume: 10

daemon@ATHENA.MIT.EDU (Perl-Users Digest)
Thu Jan 13 14:05:38 2005

Date: Thu, 13 Jan 2005 11:05:12 -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           Thu, 13 Jan 2005     Volume: 10 Number: 7641

Today's topics:
    Re: $! is set to "Inappropriate ioctl..." on Linux <do-not-use@invalid.net>
    Re: [perl-python] 20050112 while statement <peter@somewhere.com>
    Re: [perl-python] 20050112 while statement <steven.bethard@gmail.com>
    Re: [perl-python] 20050112 while statement <peter@engcorp.com>
    Re: Adding a delimiter inbetween number characters and  <tadmc@augustmail.com>
    Re: Adding a delimiter inbetween number characters and  <tadmc@augustmail.com>
    Re: array of arrays: searching for wisdom ... <jgibson@mail.arc.nasa.gov>
    Re: CGI: Execute a perl script inside another perl scri <xdarcos@hotmail.com>
    Re: CGI: Execute a perl script inside another perl scri <noreply@gunnar.cc>
    Re: CGI: Execute a perl script inside another perl scri <noreply@gunnar.cc>
    Re: CGI: Execute a perl script inside another perl scri <nobull@mail.com>
        convention regarding lexical filehandles <mritty@gmail.com>
    Re: convention regarding lexical filehandles <phaylon@dunkelheit.at>
    Re: convention regarding lexical filehandles <nobull@mail.com>
    Re: convention regarding lexical filehandles <mritty@gmail.com>
    Re: effective sort on two fields in both searchorders ( (Anno Siegel)
    Re: How can I tell if a variable is a FileHandle? peterkayatwork@yahoo.com
        Memory leak with PUSHMARK and PUTBACK <info@gansert.info>
    Re: Perl CGI does not generate file. <tadmc@augustmail.com>
    Re: Print question <jl_post@hotmail.com>
    Re: Random Functionality <sbryce@scottbryce.com>
    Re: Random Functionality xhoster@gmail.com
    Re: Random Functionality <jgibson@mail.arc.nasa.gov>
    Re: Random Functionality <sbryce@scottbryce.com>
    Re: Random Functionality <sbryce@scottbryce.com>
    Re: Random Functionality <nobull@mail.com>
    Re: Tk: Call subroutine when MainWindow is realized? <lusol@Dragonfly.cc.lehigh.edu>
    Re: Works great! <jgibson@mail.arc.nasa.gov>
        Digest Administrivia (Last modified: 6 Apr 01) (Perl-Users-Digest Admin)

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

Date: 13 Jan 2005 15:19:38 +0100
From: Arndt Jonasson <do-not-use@invalid.net>
Subject: Re: $! is set to "Inappropriate ioctl..." on Linux
Message-Id: <yzdhdllmmx1.fsf@invalid.net>


amirkargerweb@yahoo.com writes:
> 
> Running perl5.8.* on Linux (more details later), I get:
> 
> ==============
> >echo 'hi' > zzz
> >echo 'bye' >> zzz
> >perl -we 'print ".$\!.\n";open FOO, "<zzz" or die "foo\n";print
> ".$\!.\n"; print <FOO>'
> ..
> .Inappropriate ioctl for device.

This is the classical Unix "Not a typewriter" error that appears
if you check 'errno' after doing some I/O although nothing went wrong.
The reason is usually that the standard I/O routines check whether
stdout is a terminal or not and sets buffering accordingly. This check
results in an error from a system call in case stdout is not a
"typewriter", i.e., "tty", i.e., terminal.


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

Date: Thu, 13 Jan 2005 15:58:03 +0100
From: Peter Maas <peter@somewhere.com>
Subject: Re: [perl-python] 20050112 while statement
Message-Id: <cs62ed$oq0$1@swifty.westend.com>

brianr@liffe.com schrieb:
> "Xah Lee" <xah@xahlee.org> writes:
[...]
> (As a matter of interest, is this sequence of posts intended to
> demonstrate ignorance of both languages, or just one?)

:)

This sequence of posts is intended to stir up a debate just for
the sake of a debate. It's a time sink. It's up to you wether you
want to post to this thread or do something useful. :)

-- 
-------------------------------------------------------------------
Peter Maas,  M+R Infosysteme,  D-52070 Aachen,  Tel +49-241-93878-0
E-mail 'cGV0ZXIubWFhc0BtcGx1c3IuZGU=\n'.decode('base64')
-------------------------------------------------------------------


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

Date: Thu, 13 Jan 2005 11:23:09 -0700
From: Steven Bethard <steven.bethard@gmail.com>
Subject: Re: [perl-python] 20050112 while statement
Message-Id: <ra2dnedJ8ogXIXvcRVn-hg@comcast.com>

Xah Lee wrote:
> # here's a while statement in python.
> 
> a,b = 0,1
> while b < 20:
> print b
> a,b = b,a+b
> 
> ---------------
> # here's the same code in perl
> 
> ($a,$b)=(0,1);
> while ($b<20) {
> print $b, "\n";
> ($a,$b)= ($b, $a+$b);
> }

Because you're posting this to newsgroups, it would be advisable to use 
only spaces for indentation -- tabs are removed by a lot of newsreaders, 
which means your Python readers are going to complain about 
IndentationErrors.

Personally, I think you should not do this over comp.lang.python (and 
perhaps others feel the same way about comp.lang.perl.misc?)  Can't you 
start a Yahoo group or something for this?  What you're doing is 
probably not of interest to most of the comp.lang.python community, and 
might me more appropriate in a different venue.

Steve


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

Date: Thu, 13 Jan 2005 13:30:00 -0500
From: Peter Hansen <peter@engcorp.com>
Subject: Re: [perl-python] 20050112 while statement
Message-Id: <-oSdnYnagrc_I3vcRVn-3Q@powergate.ca>

Steven Bethard wrote:
> Xah Lee wrote:
[some Python code]
> Because you're posting this to newsgroups, it would be advisable to use 
> only spaces for indentation -- tabs are removed by a lot of newsreaders, 
> which means your Python readers are going to complain about 
> IndentationErrors.

Unfortunately, there are now two ways to post screwed up Python
code, and using tabs is only one of them.

The other is to post from Google Groups, and that's what Xah Lee
is doing.

(The rest of your advice, about going away, is pretty good though. ;-)

-Peter


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

Date: Thu, 13 Jan 2005 08:03:49 -0600
From: Tad McClellan <tadmc@augustmail.com>
Subject: Re: Adding a delimiter inbetween number characters and letter characters
Message-Id: <slrncucvu5.h28.tadmc@magna.augustmail.com>

Arndt Jonasson <do-not-use@invalid.net> wrote:
> 
> Tad McClellan <tadmc@augustmail.com> writes:
>> toomanyjoes@mail.utexas.edu <toomanyjoes@mail.utexas.edu> wrote:


>> I for one, think you are making stuff up to support your 
>> untenable position.
> 
> Didn't you plonk him five minutes ago?


Yes, but I usually manually un-delete them for a few days to either:

   Be amused at yet another example of human idiocy (until it becomes tedious)

   Make further points in this thread while ignoring future threads

   Make sure I didn't knee-jerk someone in who doesn't truly belong

   Show the valuable type of response that getting killfiled costs them

   Actually answer the question because it is an unusually slow day
     and I have time to give, even to those who won't respect it


I also occasionally un-delete long-killfiled folks to check up
on how they are behaving.

Getting killfiled does not guarantee ignoration, it only makes
it much more likely.


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


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

Date: Thu, 13 Jan 2005 08:15:00 -0600
From: Tad McClellan <tadmc@augustmail.com>
Subject: Re: Adding a delimiter inbetween number characters and letter characters
Message-Id: <slrncud0j4.h28.tadmc@magna.augustmail.com>

Arndt Jonasson <do-not-use@invalid.net> wrote:
> 
> "A. Sinan Unur" <1usa@llenroc.ude.invalid> writes:
>> toomanyjoes@mail.utexas.edu wrote in news:1105553915.762548.182620
>> @c13g2000cwb.googlegroups.com:
>> 
>> > Showing the passion of your emotions is very unattractive 
>> 
>> Lucky for us, this is not Fifth Wheel, we don't care about what you find 
>> attractive.
> 
> What's "Fifth wheel"? I didn't find google's hits helpful.


It is a dating "game show" on television in the U.S. where a
fifth person is added after four people (2 male, 2 female)
have gone out on a few dates together.

I guess that what happens to the existing relationships after adding
"the fifth wheel" is supposed to be entertaining or interesting...


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


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

Date: Thu, 13 Jan 2005 09:15:48 -0800
From: Jim Gibson <jgibson@mail.arc.nasa.gov>
Subject: Re: array of arrays: searching for wisdom ...
Message-Id: <130120050915489339%jgibson@mail.arc.nasa.gov>

In article <6a25ba72.0501130135.f6adfbd@posting.google.com>, Alythh
<alythh@netscape.net> wrote:

> hi people,
> I've some troubles trying to access an array of arrays:
> while using the format:
> 
> for $i ( 0 .. $#AoA ) {
>     $row = $AoA[$i];
>     for $j ( 0 .. $#{$row} ) {
>         print "element $i $j is $row->[$j]\n";
>     }
> }
> 
> surely works, I was wandering if there is a way to scan @AoA for its
> $i-th row, and returning it as @i_row, without rebuilding it (by
> pushing one-by-one $row->[$j], as in previous example):
> it is both an efficiency worry, and a styilistic one: that code is
> awful!

While Tore and Brian have answered your question, you should also know
that you can access the elements of AoA directly without resorting to
extracting the rows one-by-one:

#!/usr/local/bin/perl
#
use strict;
use warnings;

my @AoA = (
  [ 1.1, 1.2, 1.3 ],
  [ 2.1, 2.2, 2.3 ],
  [ 3.1, 3.2, 3.3 ]
);

for my $i ( 0 .. $#AoA ) {
  for my $j ( 0 .. $#{$AoA[$i]} ) {
    printf "%6.1f", $AoA[$i][$j];
  }
  print "\n";
}


__OUTPUT__
   1.1   1.2   1.3
   2.1   2.2   2.3
   3.1   3.2   3.3


There is nothing awful about that code. In fact, it almost resembles my
beloved Fortran! :^)

- Jim "I can code Fortran in any language" Gibson


----== Posted via Newsfeeds.Com - Unlimited-Uncensored-Secure Usenet News==----
http://www.newsfeeds.com The #1 Newsgroup Service in the World! >100,000 Newsgroups
---= East/West-Coast Server Farms - Total Privacy via Encryption =---


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

Date: 13 Jan 2005 07:04:53 -0800
From: "xad" <xdarcos@hotmail.com>
Subject: Re: CGI: Execute a perl script inside another perl script
Message-Id: <1105628693.424993.51310@z14g2000cwz.googlegroups.com>

Thanks for your response. You were right, it works with the full path
to the script in the do().
The only concern is that I also wanted to put all environment variables
in one script in order to give users the ability to modify it by
setting their own configurations files, and in another side keeping a
generic root script not to be modified. So now they will also need to
modify the root script.
Anyway I thought that SunOne Web Server was based on Apache. Isn't it
right ?

Xavier



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

Date: Thu, 13 Jan 2005 16:34:43 +0100
From: Gunnar Hjalmarsson <noreply@gunnar.cc>
Subject: Re: CGI: Execute a perl script inside another perl script
Message-Id: <34nim2F4drn7sU1@individual.net>

Brian McCauley wrote:
> xdarcos@hotmail.com wrote:
>> 
>> do 'y.pl';
> 
> As you say this is not related to Perl.

I think it is.

> The CGI standard does not define the setting of the current working
> directory when a CGI process is spawned.  In the case of Apache it
> (almost?) always sets the CWD to the directory containing the script.
> I'm guessing that SunOne does not do this.

"do" does not use the working directory, it searches @INC if the path is
relative.

     perldoc -f do

> You need to use a fully qualified filename in the do().

Suppose you mean the full path. That always works, of course, but you 
can also do:

     use lib '.';
     do 'y.pl';

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


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

Date: Thu, 13 Jan 2005 16:39:24 +0100
From: Gunnar Hjalmarsson <noreply@gunnar.cc>
Subject: Re: CGI: Execute a perl script inside another perl script
Message-Id: <34niusF4drn7sU2@individual.net>

xad wrote:
> Thanks for your response. You were right, it works with the full path
> to the script in the do().

It should be noted that a simple check for success would have saved you 
some trouble:

     do 'y.pl' or die "Couldn't execute y.pl: $!";

> The only concern is that I also wanted to put all environment variables
> in one script in order to give users the ability to modify it by
> setting their own configurations files, and in another side keeping a
> generic root script not to be modified. So now they will also need to
> modify the root script.

Not if you do:

     use lib '.';
     do 'y.pl';

See my reply to Brian.

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


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

Date: Thu, 13 Jan 2005 18:22:30 +0000
From: Brian McCauley <nobull@mail.com>
Subject: Re: CGI: Execute a perl script inside another perl script
Message-Id: <cs6dv6$mhg$1@sun3.bham.ac.uk>

Gunnar Hjalmarsson wrote:

> Brian McCauley wrote:
> 
>> xdarcos@hotmail.com wrote:
>>
>>>
>>> do 'y.pl';
>>
>>
>> As you say this is not related to Perl.
> 
> 
> I think it is.
> 
> "do" does not use the working directory, it searches @INC if the path is
> relative.

I always seem to forget that.  For some inexplicable reason I always 
imagine that the @INC search is in require() not in the lower level do().



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

Date: Thu, 13 Jan 2005 18:09:13 GMT
From: "Paul Lalli" <mritty@gmail.com>
Subject: convention regarding lexical filehandles
Message-Id: <dPyFd.16$Dz2.4@trndny09>

Prior to the invention of lexical file handles, I'd always seen the
convention that filehandles should be given in all capital letters:
open FILE, 'file.txt' or die "...";

Now that we have lexical file handles, I'm wondering if there is a
consensus as towards this convention.  Which of the following looks more
'right':

open $file, 'file.txt' or die "...";
or
open $FILE, 'file.txt' or die "...";

Obviously, I know that both work, but I'd like to get opinions on the
Best Practice way to do things.

Thanks for your input,
Paul Lalli



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

Date: Thu, 13 Jan 2005 19:13:29 +0100
From: phaylon <phaylon@dunkelheit.at>
Subject: Re: convention regarding lexical filehandles
Message-Id: <pan.2005.01.13.18.13.29.269561@dunkelheit.at>

Paul Lalli wrote:

> Obviously, I know that both work, but I'd like to get opinions on the Best
> Practice way to do things.

That, in my opinion, depends largely on what you want to do and how your
code is (like mostly everything, except strict;warning;&Co). I used the
uppercase-convention in first place because of the (often) missing syntax
highlighting for barewords. With lexical filehandle's (which I like much
more, just to say) it "fit's more in".

Sure, if I'm working on a method with a large number of filehandles to
handle, I prefix them ($fh_csvin or categorical: $csv_infh).

Hope that gives some idea's or so..


p

-- 
http://www.dunkelheit.at/

Kunst kommt von Koennen, kaeme sie von Wollen, hiesse sie Wulst.
                             -- Nietzsche - Bonmot



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

Date: Thu, 13 Jan 2005 18:32:01 +0000
From: Brian McCauley <nobull@mail.com>
Subject: Re: convention regarding lexical filehandles
Message-Id: <cs6egv$mqa$1@sun3.bham.ac.uk>



Paul Lalli wrote:

> Prior to the invention of lexical file handles, I'd always seen the
> convention that filehandles should be given in all capital letters:
> open FILE, 'file.txt' or die "...";
> 
> Now that we have lexical file handles, I'm wondering if there is a
> consensus as towards this convention.  Which of the following looks more
> 'right':
> 
> open $file, 'file.txt' or die "...";
> or
> open $FILE, 'file.txt' or die "...";

Neither looks right to me.

  open my $file, '<', 'file.txt' or die "...";

You should always declare all variables in the smallest applicable 
scope.  In the case of a lexical filehandle this means the my() 
declaration should be inside the open() statement.

Conceptually open() takes three arguments (or more/less in pipe mode).

The legacy two argument form of open() that combines the mode and 
filename into one argument is a wart on the language and using it is a 
dangerous habit that will burn you one day.

As for whether the variable containing the file handle reference should 
be uppercase or lowercase I have almost no preference.  I, myself, 
usually use lowercase.



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

Date: Thu, 13 Jan 2005 18:48:36 GMT
From: "Paul Lalli" <mritty@gmail.com>
Subject: Re: convention regarding lexical filehandles
Message-Id: <8ozFd.151$Vx2.35@trndny01>

"Brian McCauley" <nobull@mail.com> wrote in message
news:cs6egv$mqa$1@sun3.bham.ac.uk...
>
>
> Paul Lalli wrote:
>
> > open $file, 'file.txt' or die "...";
> > or
> > open $FILE, 'file.txt' or die "...";
>
> Neither looks right to me.
>
>   open my $file, '<', 'file.txt' or die "...";
>
> You should always declare all variables in the smallest applicable
> scope.  In the case of a lexical filehandle this means the my()
> declaration should be inside the open() statement.

Okay, point.  Admittedly, I was typing fast and neglected to include the
'my'.  Thanks for catching that.

> Conceptually open() takes three arguments (or more/less in pipe mode).
>
> The legacy two argument form of open() that combines the mode and
> filename into one argument is a wart on the language and using it is a
> dangerous habit that will burn you one day.

This one I've never really understood.  I'm going to go take a look at
perlopentut, but if you'd care to explain in what way the two-argument
form is dangerous, I'd appreciate it.

Thank you,
Paul Lalli



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

Date: 13 Jan 2005 14:05:08 GMT
From: anno4000@lublin.zrz.tu-berlin.de (Anno Siegel)
Subject: Re: effective sort on two fields in both searchorders (no FAQ !)
Message-Id: <cs5v6k$apc$3@mamenchi.zrz.TU-Berlin.DE>

peter pilsl  <pilsl@goldfisch.at> wrote in comp.lang.perl.misc:
> 
> 
> $val={a=>[1,2],
>        b=>[5,9],
>        c=>[2,4],
>        ...}
> 
> I need to sort the keys on the two fields (first field and then second 
> field), so I use:
> 
> my @sort1= sort {
>                   $val->{$a}->[0] <=> $val->{$b}->[0] ||
>                   $val->{$a}->[1] <=> $val->{$b}->[1]
>                  } keys %$val;
> 
> So far so good. Now I need - in the very next step - to do a second sort 
> with opposite sort-order (second field and then first field)
> 
> my @sort2= sort {
>                   $val->{$a}->[1] <=> $val->{$b}->[1] ||
>                   $val->{$a}->[0] <=> $val->{$b}->[0]
>                  } keys %$val;
> 
> 
> The true $val is very big and I'm sure there is a much more efficient 
> (and complex) way to get both searchorders in one step without doing all 
> the <=>-operations twice.

In general, the result of one sort says *nothing* about the other.  If
no two elements of the list compare equal in the first comparison, the
other comparison doesn't even happen.  You can't use one sort to speed
up the other.

Anno


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

Date: 13 Jan 2005 08:26:20 -0800
From: peterkayatwork@yahoo.com
Subject: Re: How can I tell if a variable is a FileHandle?
Message-Id: <1105633580.800988.120500@c13g2000cwb.googlegroups.com>

<blushes bring pink>

Thanks!



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

Date: Thu, 13 Jan 2005 17:12:45 +0100
From: Uwe Gansert <info@gansert.info>
Subject: Memory leak with PUSHMARK and PUTBACK
Message-Id: <cs66lt$uot$1@fourier.suse.de>

Hi,

I'm fighting with using Perl Modules from within C programms.
While digging into that topic, I'm wondering why the following simple C
function is a memory leak:


void leak() {
    dSP;
    PUSHMARK(SP);
    PUTBACK;
}

calling this function in an endless loop, eats up all free memory.
perl -v => "This is perl, v5.8.5 built for i586-linux-thread-multi"

--
tschau, Uwe


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

Date: Thu, 13 Jan 2005 08:36:16 -0600
From: Tad McClellan <tadmc@augustmail.com>
Subject: Re: Perl CGI does not generate file.
Message-Id: <slrncud1r0.h28.tadmc@magna.augustmail.com>

Gazza <gazza@192.168.1.1> wrote:

> CGI
> script is supposed to then create the file with the text in it. It's
> simple but, unfortunately, the file doesn't get created.

> CGI script on a BASH command line like this:

> (all one line, of course) works.


> What have I done wrong?


You did not check the Perl FAQ before posting to the Perl newsgroup.

   perldoc -q CGI

       My CGI script runs from the command line but not the browser.  (500
       Server Error)

       How can I get better error messages from a CGI program?


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


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

Date: 13 Jan 2005 07:11:47 -0800
From: "jl_post@hotmail.com" <jl_post@hotmail.com>
Subject: Re: Print question
Message-Id: <1105629107.541510.296690@f14g2000cwb.googlegroups.com>

> jl_post@hotmail.com wrote:
>
> > I got the impression that his print() statements
> > were always printing a newline.

Sherm Pendley replied:
>
> Hmmm... that's not how I read it, but I see your
> point. I read it as "I need to have a word etc.
> print immediately ... without having to print a new
> line until later."


Evidently, judging by the original poster's response,
you were correct, not me.  (It's not like it matters
now, but I just thought I'd point that out.)

   -- Jean-Luc



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

Date: Thu, 13 Jan 2005 10:21:03 -0700
From: Scott Bryce <sbryce@scottbryce.com>
Subject: Re: Random Functionality
Message-Id: <X5SdncMB6PRvMHvcRVn-ig@comcast.com>

Anno Siegel wrote:

> That code fulfills almost none of your requirements.  The number of
> subroutines is hard-coded (it is 3), as are their references.  You only
> avoid hard-coding their names by not giving them any.

Then I must have done a poor job of describing my requirements.

What I want to avoid is having to include a chunk of code like:

my $i = int(rand(3));    # This line has the number hard coded

Sub_1() if $i == 0;      # These lines have sub names hard coded.
Sub_2() if $i == 1;
Sub_3() if $i == 2;



If I add a Sub_4, the code above would have to be changed to reflect 
that. I want to avoid having to make that kind of change to the code. I 
have found from working in other environments that this approach is 
prone to errors when the code is maintained.


> If you want the code to change at run-time (not a good idea in general),
> you'll have to compile it at run-time.  See "perldoc -f do" and "perldoc
> -f eval" for alternative ways to do that.

My original approach involved eval'ing the contents of external files. I 
found this approach difficult to maintain.



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

Date: 13 Jan 2005 17:22:29 GMT
From: xhoster@gmail.com
Subject: Re: Random Functionality
Message-Id: <20050113122229.990$gl@newsreader.com>

Scott Bryce <sbryce@scottbryce.com> wrote:
> I want to create a script that has random functionality.
>
> I want the script to randomly choose between an unknown number of
> subroutines and run one of them. By "unknown" I mean that the neither
> names of the subroutines, the references to the subroutines, nor the
> number of subroutines are hard coded into the script,

I started using a Nerf-brand keyboard.  Now none of the features in
my programs are hard-coded.

> but are determined
> at runtime. This will allow me to add additional functions to the list
> of possible random functions without having to update any other portion
> of the script.

What do you mean any "other" portion of the script?  If you are modifying
any portion of your scipt in the first place, that is hard-coding, isn't
it?

>
> If that is as clear as mud, the code sample below demonstrates what I
> want to do.

But it is at odds with your description above.  Which should we trust?

> I came up with this. Is there a better way to do it?

Assuming that what it does is what you want it to do, I think it is just
fine.  I might get rid of the push and go with a format more like:

  @sub_list = (
     sub {adfsf},
     sub {lkqjer},
     sub {lkjwre;lk},
   );


Xho


>
> use strict;
> use warnings;
>
> my @sub_list;
>
> print &{$sub_list[int(rand(@sub_list))]};
>
> BEGIN
> {
>         push @sub_list, sub
>         {
>                 return "Sub One was chosen\n";
>         };
>
>         push @sub_list, sub
>         {
>                 return "Sub Two was chosen\n";
>         };
>
>         push @sub_list, sub
>         {
>                 return "Sub Three was chosen\n";
>         };
> }

-- 
-------------------- http://NewsReader.Com/ --------------------
Usenet Newsgroup Service                        $9.95/Month 30GB


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

Date: Thu, 13 Jan 2005 09:37:59 -0800
From: Jim Gibson <jgibson@mail.arc.nasa.gov>
Subject: Re: Random Functionality
Message-Id: <130120050937599234%jgibson@mail.arc.nasa.gov>

In article <UrqdnfGlof3ZgXvcRVn-rA@comcast.com>, Scott Bryce
<sbryce@scottbryce.com> wrote:

> I want to create a script that has random functionality.

Most of the scripts I write have random functionality. Perhaps you
would like to hire me? (I find Perl hands-down the best language for
implementing random functionality scripts. :^)

Maybe you can put Perl files defining functions in a directory, scan
the directory, get the subroutine names from the files, 'do' the files,
put the subroutine names and references in a hash, get an array of hash
keys, and select one at random to execute.

-- 
Jim Gibson


----== Posted via Newsfeeds.Com - Unlimited-Uncensored-Secure Usenet News==----
http://www.newsfeeds.com The #1 Newsgroup Service in the World! >100,000 Newsgroups
---= East/West-Coast Server Farms - Total Privacy via Encryption =---


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

Date: Thu, 13 Jan 2005 10:50:31 -0700
From: Scott Bryce <sbryce@scottbryce.com>
Subject: Re: Random Functionality
Message-Id: <NYOdnWn8865HKXvcRVn-hg@comcast.com>

xhoster@gmail.com wrote:

> But it is at odds with your description above.  Which should we trust?

The fault lies in the description of the problem. I am finding it 
difficult to describe exactly what I want to do. I attempted to clarify 
the problem description in my reply to Anno. If the problem description 
is still unclear, I guess I'm on my own.

The code does what I want it to do. I want to know if there is a better 
way to do it than what I wrote.



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

Date: Thu, 13 Jan 2005 11:02:03 -0700
From: Scott Bryce <sbryce@scottbryce.com>
Subject: Re: Random Functionality
Message-Id: <UoOdnfaXcuQSKnvcRVn-vQ@comcast.com>

Jim Gibson wrote:

> Maybe you can put Perl files defining functions in a directory, scan
> the directory, get the subroutine names from the files, 'do' the files,
> put the subroutine names and references in a hash, get an array of hash
> keys, and select one at random to execute.

My original approach was similar to this. I put Perl files containing a 
chunk of code (not in subroutines. Perhaps my implementation was bad?) 
in a directory, scanned the directory, created a list of file names, 
randomly selected one file name from the list, and eval'ed the contents 
of the file.

I found this approach difficult to maintain.

I would prefer that the functions in question be in the script, rather 
than residing in external files.

I want the code that selects one subroutine to work regardless of the 
number of subroutines.



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

Date: Thu, 13 Jan 2005 18:15:53 +0000
From: Brian McCauley <nobull@mail.com>
Subject: Re: Random Functionality
Message-Id: <cs6dip$mhb$1@sun3.bham.ac.uk>


Scott Bryce wrote:

> Anno Siegel wrote:
> 
>> That code fulfills almost none of your requirements.  The number of
>> subroutines is hard-coded (it is 3), as are their references.  You only
>> avoid hard-coding their names by not giving them any.
> 
> 
> Then I must have done a poor job of describing my requirements.
> 
> What I want to avoid is having to include a chunk of code like:
> 
> my $i = int(rand(3));    # This line has the number hard coded
> 
> Sub_1() if $i == 0;      # These lines have sub names hard coded.
> Sub_2() if $i == 1;
> Sub_3() if $i == 2;

Sometimes symbolic CODErefs are the right tool.
{
   no strict 'refs';
   ( 'Sub_' . ( $i + 1 ) )->();
}

> If I add a Sub_4, the code above would have to be changed to reflect 
> that. I want to avoid having to make that kind of change to the code. I 
> have found from working in other environments that this approach is 
> prone to errors when the code is maintained.

You can find all the functions with a prefix 'Sub_' by analysing the 
symbol table.  However rather than using a prefix string it's probably 
better to use a separate symbol table (package) and simply find _all_ 
the subrouines in that symbol table.

>> If you want the code to change at run-time (not a good idea in general),
>> you'll have to compile it at run-time.  See "perldoc -f do" and "perldoc
>> -f eval" for alternative ways to do that.
> 
> 
> My original approach involved eval'ing the contents of external files. I 
> found this approach difficult to maintain.

Can you explain the nature of the difficulty.

Usually I'd do something like:

{
    # This package is used only for the handlers defined in handlers.pl
    package MyModule::Handlers;
    # handlers.pl does not countain a package directive
    do 'handlers.pl';
}

my @sub_list =
    map { \&$_ }
    grep { exists(&$_) }
    map {"MyModule::Handlers::$_"}
    sort keys %MyModule::Handlers::;

Note \&$_ and exists(&$_) are exempt from strict refs precisely so that 
you can do stuff like this.



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

Date: Thu, 13 Jan 2005 13:50:06 +0000 (UTC)
From: Steve Lidie <lusol@Dragonfly.cc.lehigh.edu>
Subject: Re: Tk: Call subroutine when MainWindow is realized?
Message-Id: <cs5uae$55kc$1@ws2.cc.lehigh.edu>

Josef Moellers <josef.moellers@fujitsu-siemens.com> wrote:
> Steve Lidie wrote:
>> Josef Moellers <josef.moellers@fujitsu-siemens.com> wrote:
>> 
>> You'll get better, more, help in comp.lang.perl.tk.
> 
> I didn't know that group existed B-{(

;)

 ...

>> I'm having trouble understanding what you really want to do from above.
> 
> I'm creating a GUI application whose main/only task is to communicate 
> with a smart card reader through a serial port. As the reader may not 
> have been switched on, I'd like to "ping" it at a very early time and, 
> if that fails, create a Dialog widget notifying the user of the fact and 
> asking him to switch it on.

That's what I thought you said ... given that, I couldn't understand why
you would have so much trouble ;)

> 
> I have tried something like this:
> 
> use Tk;
> use Tk::Dialog;
> my $top = new MainWindow;
> # populate the window
> my $cardterminal = new CardTerminal();
> unless ($cardterminal->ready()) {
>     my $d = $top->Dialog(-text => "Please switch on the card terminal", 
> ...);
>     $d->Show(-global);
> }
> MainLoop;

Looks just like what I might write.

> 
> and I recall having had problems.
> 
> However, I just tried this and it appears that the MainLoop is somehow 
> invoked when the Show method is called. So, it worked

Not MainLopp(), but the event loop in general. If you had created a
good CardTerminal object the above code would have fallen through to
the MainLoop() statement.

Now, back to threads.  You can't use them, but with care you can use
fork() with Perl/Tk.  W/O knowing anything about your CardTerminal class,
if it's methods block the event loop then the GUI will stop working. The
traditional way to solve this is to fork() a child that talks to the
hardware device, and uses, say, pipes to communicate with the Tk parent.
Perl/Tk's filevent() can watch the pipes in a non-blocking fashion.

Goto comp.lang.perl.tk for the best info.

> 
> In essence, please let's all forget I asked B-{).
> 
> Sorry to have wasted your time,
> 
> Josef, promising to check better before asking nonesense questions.

Not a problem!



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

Date: Thu, 13 Jan 2005 09:48:46 -0800
From: Jim Gibson <jgibson@mail.arc.nasa.gov>
Subject: Re: Works great!
Message-Id: <130120050948468000%jgibson@mail.arc.nasa.gov>

In article <x0nFd.5722$C52.3204@newsread2.news.atl.earthlink.net>,
edgrsprj <edgrsprj@ix.netcom.com> wrote:

> And buffering is restored in this manner.  Or is that the wrong way to do
> that?
> 
> $|=1;
> print 'Thanks ';
> sleep 2;
> $|='';
> print 'again';
> sleep 2;
> print "\a\n";
> sleep 5;

The effect of setting $| to non-zero can be localized by using the
local declaration inside a block. The local declaration creates a
temporary variable within the innermost enclosing block:

{
  local $| = 1;
  print "unbuffered ";
  sleep(1);
  print "print ";
  sleep(1);
  print "statements\n";
}

# buffering restored here
print "buffered ";
sleep(1);
print "print ";
sleep(1);
print "statements\n";


----== Posted via Newsfeeds.Com - Unlimited-Uncensored-Secure Usenet News==----
http://www.newsfeeds.com The #1 Newsgroup Service in the World! >100,000 Newsgroups
---= East/West-Coast Server Farms - Total Privacy via Encryption =---


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

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


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