[23650] in Perl-Users-Digest

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

Perl-Users Digest, Issue: 5857 Volume: 10

daemon@ATHENA.MIT.EDU (Perl-Users Digest)
Tue Nov 25 14:05:50 2003

Date: Tue, 25 Nov 2003 11:05:13 -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           Tue, 25 Nov 2003     Volume: 10 Number: 5857

Today's topics:
        aternative grouping and map, deuglification (Sara)
    Re: aternative grouping and map, deuglification (Greg Bacon)
    Re: client/ server nobull@mail.com
    Re: creating unix-like text files in windows <ben.liddicott@comodogroup.com>
        Help with updating HTML from drop down menu's please! (Paul Turner)
        How to store correctly UTF-8 texts (after XML parsing) (Francesco Moi)
    Re: Perl Editor (Thomas)
    Re: Protecting Source code of a perl script ctcgag@hotmail.com
    Re: Protecting Source code of a perl script (Rafael Garcia-Suarez)
    Re: Protecting Source code of a perl script <usenet@morrow.me.uk>
    Re: Protecting Source code of a perl script <abigail@abigail.nl>
    Re: push @arr, slice-of-href <pinyaj@rpi.edu>
        script works from prompt but not through telnet (Eric Lewton)
    Re: script works from prompt but not through telnet <abigail@abigail.nl>
    Re: script works from prompt but not through telnet <simon.andrews@bbsrc.ac.uk>
    Re: sending stdin to a shell command in perl <dmcbride@naboo.to.org.no.spam.for.me>
        system() never returns, how to kill it? (Zhidian Du)
    Re: system() never returns, how to kill it? <abigail@abigail.nl>
    Re: undif as if it is 0 <eddhig22@yahool.com>
    Re: unpack query (Jack Penarth)
        Using GetOptions twice in the script <motis@lyciumnetworks.com>
    Re: Using GetOptions twice in the script <usenet@morrow.me.uk>
    Re: Using GetOptions twice in the script <motis@lyciumnetworks.com>
    Re: Using GetOptions twice in the script <dmcbride@naboo.to.org.no.spam.for.me>
    Re: Using GetOptions twice in the script (Greg Bacon)
        Digest Administrivia (Last modified: 6 Apr 01) (Perl-Users-Digest Admin)

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

Date: 25 Nov 2003 08:19:54 -0800
From: genericax@hotmail.com (Sara)
Subject: aternative grouping and map, deuglification
Message-Id: <776e0325.0311250819.7fa75087@posting.google.com>

I have a file like

CAT  4
DOG  3
CAT 6
CAT 9
BIRD 4
DOG 13
MOUSE 2
  .
  .

I'm trying to find lines with a certain starting word like

  @a = map /^((CAT|DOG).+)$/, @a;

What I WANT is actually all of the $1's (the whole line), but I get an
array of the $1 and $2's instead. In this case I only wanted to use
the parens to define the grouping of alternating substrings (as
defined on p59 in Camel 2nd ed), but as a side-effect I end up with
twice the size array as I expected.

If I try

  @a = map /^(CAT|DOG).+$/, @a;

I end up with an array of CAT and DOG's only - the rest of the lines
are gone. I only want the parens to group the alternatives, but, well
you see what's happening.

My ugly solution was:

  my %a = map /^((CAT|DOG).+)$/, @a;
  @a = keys %a;

or equally unattractive:
  grep /\d/, ( map /^((ASIC|SYSTEM).+)$/i, @p );

which also tossees out the extra lines.

Is there a way to define the alternative grouping and still return the
entire line without these addtional steps? Yes they are effecitve, but
not up to my standard of style.

Happy T-Day!
G


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

Date: Tue, 25 Nov 2003 17:08:02 -0000
From: gbacon@hiwaay.net (Greg Bacon)
Subject: Re: aternative grouping and map, deuglification
Message-Id: <vs733ioshhni01@corp.supernews.com>

In article <776e0325.0311250819.7fa75087@posting.google.com>,
    Sara <genericax@hotmail.com> wrote:

: [...]
: I'm trying to find lines with a certain starting word like
: 
:   @a = map /^((CAT|DOG).+)$/, @a;

If you're trying to find matches for a pattern, use Perl's grep
operator:

    $ cat try
    #! /usr/local/bin/perl

    my @a = (
        'CAT  4',
        'DOG  3',
        'CAT 6',
        'CAT 9',
        'BIRD 4',
        'DOG 13',
        'MOUSE 2',
    );

    @a = grep /^(CAT|DOG)\b/, @a;

    print "[$_]\n" for @a;

    $ ./try
    [CAT  4]
    [DOG  3]
    [CAT 6]
    [CAT 9]
    [DOG 13]

    $

: [...]

See the perlfunc manpage's documentation of grep for more information.

Hope this helps,
Greg
-- 
Ah, women.  They make the highs higher and the lows more frequent.
    -- Nietzsche


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

Date: 25 Nov 2003 09:38:44 -0800
From: nobull@mail.com
Subject: Re: client/ server
Message-Id: <4dafc536.0311250938.644214aa@posting.google.com>

"spleen" <james.welsby@virgin.net> spits TOFU in my face:

[ Don't do that, it is rude ]

> "Brian McCauley" <nobull@mail.com> wrote in message
> news:u9k75pfycr.fsf@wcl-l.bham.ac.uk...
> > "spleen" <james.welsby@virgin.net> writes:
> > >
> > > Basically at the moment Im planning on using a python script to send a
> > > string of text to a website, I was woundering in perl, using the cgi-bin
> > > how I can accept this connection and take the text string and add it to
> > > a file (.txt)
> >
> > Examples and tutorials of writing trivial CGI scipts in Perl can be
> > found all over the place.
> >
> > If you have problems feel free to come here for help.
>
> Yea sorry my first post wasnt very clear, Ive sorted the perl - I have a
> server that takes the text string and puts it into a text file.

> all I was woundering how to do was to how to do this on a free hosting
> service, I cant seem to get it working on www.spaceports.com

Asking how to install arbritrary server software on a hosting service
is best directed to their technical support.  In general don't expect
a free service to allow you to do this.

If they've not taken any technical measures to prevent you opening a
listening socket then you could make a CGI script that forks a daemon
process (see FAQ "How do I fork a daemon process?") but you'd probably
get kicked off as soon as the admins notice you doing this.
  
> sorry for my misinformed post to start with, my code is posted below.
> although I dont think itll help anyone answer me becuase its more the
> application of it that im confused by
> 
> #!/usr/local/bin/perl

[ no warnings, no strict ]

Do not climb without ropes until you are an experienced climber.  You
will not endear yourself to the experienced climbers if you take the
attitude: "I'm new, I can't figure out all these ropes, they just get
in the way, I'll learn them later".  Yes the ropes will slow you down
at first, but the first time you fall you will loose much more time
than the time it would have taken you to figure out the ropes.

> use IO::Socket;
> 
> $file = "/home2/pp10aagg/public_html/sms.txt";
> open (FILE, >>$file) or die "cannot open $file: $!";

That is a syntax error - please cut/paste your code, do not retype.

>     my $listening_socket =
>         IO::Socket::INET->new(Proto => 'tcp',
>                               LocalPort => 2323,
>                               Listen => 1,
>                               Reuse => 1) or die $!;
> 
> $socket = $listening_socket->accept;

You probably want to be doing that inside a loop.

> $socket->recv($line, 80);

Do not use low-level system calls recv() or sysread() on a stream
based protocol (e.g. TCP) unless you know what you are doing (and you
are prepared to handle short returns).  For short messages you'll
often get away with it - but sooner or later you'll get burned.

Decide how the end of the message is to be indicated and use a
mechanism that reads everything until that condition is met.

This, of course, has nothing to do with Perl.  It applies equally to
all programming languages in all environments using even vaguely
POSIX-like file descriptors.

> print FILE "$line";

See FAQ: What's wrong with always quoting "$vars"?

> $socket->close;

If your variables are properly scoped you very rarely need to
explicitly close sockets and suchlike.

> close (FILE);


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

Date: Tue, 25 Nov 2003 14:05:42 -0000
From: "Ben Liddicott" <ben.liddicott@comodogroup.com>
Subject: Re: creating unix-like text files in windows
Message-Id: <bpvnjq$gei$1@kylie.comodogroup.com>

Hi Huppu,

By default, the translation from LF to CRLF happens in the IO layer.=20

You turn it off like this:

binmode FILEHANDLE;

Or by specifying binary mode when opening, which is a better idea.

For more information:
    perldoc -f binmode
    perldoc -f open
    perldoc -f sysopen

    perldoc IO::File
    perldoc PerlIO

Cheers,
Ben Liddicott


"Huppu" <huppu1@ROSKAPOSTI.yahoo.com> wrote in message =
news:3fc34f07@usenet01.boi.hp.com...
> Hi!
>=20
> I'm trying to create unix-like text files in Windows. The biggest =
issue is
> that I need to use only linefeed instead of carriage return+linefeed. =
It
> tried this with hex value \x0A, but it still did norman windows =
linebreak
> LF+CR(like I was using \n).



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

Date: 25 Nov 2003 09:33:53 -0800
From: paulturner@hotmail.com (Paul Turner)
Subject: Help with updating HTML from drop down menu's please!
Message-Id: <169fc109.0311250933.37ead2f4@posting.google.com>

Hi - I am a Perl newbie I'm afraid!

I wonder if any of you kind people could help me please!  I wish to
create a very simple Christmas gift management website for my family.

Put simply, I want to create an HTML page that contains a wish list -
after each of the items on the list I want a drop-down menu that
contains the menu items:

"Not bought" (default)
"Bought by person 1"
"Bought by person 2"
"Bought by person 3"
"Bought by person 4"

And a SUBMIT button.

When somebody buys an item on the list (from a real shop - nothing to
do with the website) they can go onto the site, and change the value
of that item from "Not bought" to "Bought by person 2" for instance,
then click SUBMIT and the HTML will then permanently reflect the
changes so that the item will always have "Bought by person 2" next to
it - therefore preventing multiple purchases from other family
members!

Does that make sense?  I hope so.  I know HTML really well, and know
how to install CGI scripts and stuff - It's just writing them which
proves tricky!

Any help/scripts etc would be very much appreciated by myself and my
family!

Regards,

(please don't email replies as I never check this email account any
more)
Paul


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

Date: 25 Nov 2003 09:31:45 -0800
From: francescomoi@europe.com (Francesco Moi)
Subject: How to store correctly UTF-8 texts (after XML parsing)
Message-Id: <5b829932.0311250931.7aaacd3@posting.google.com>

Hello.

I'm parsing an XML file, and storing some of the contents into
a MySQL database. I use Perl (XML::Parser).

I was adviced that 'XML::Parser' works with UTF-8 charset, so
I use 'Unicode::MapUTF8' to convert text.

But:
* If I parse the XML file, and print the text, I get 'música' (OK).
* If I parse the XML file, store the text, and print the
stored text, I get 'música' (wrong)
* If I parse the XML file, store the text, convert the stored text
from UTF-8 to ISO-8859-1 charset, and print it, I get 'música' (OK).
* If I parse the XML file, convert the text from UTF-8 to ISO-8859-1
charset, store it, and print the stored (and converted) text,
I get 'música' (wrong, but I did not expect it).

I only want to store text into my database and print it correctly,
but converting it _BEFORE_ storing. I don't feel like converting
each text I read from the database.

The piece of code I use to convert text is:
--------//----------
use Unicode::MapUTF8 qw(to_utf8 from_utf8 utf8_supported_charset);
sub convert_unicode
{
	$my_text = $_[0];
	my $converted  = from_utf8({ -string => $my_text, -charset => 'ISO-8859-1' });
	return $converted;
}
--------//---------

Does anybody know what I am doing wrong? Thank you very much.


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

Date: 25 Nov 2003 08:17:09 -0800
From: sctommi@gmx.net (Thomas)
Subject: Re: Perl Editor
Message-Id: <5a1109df.0311250817.316f9a56@posting.google.com>

carusoa@optonline.net (AnnMarie) wrote in message news:<c659b5f9.0311240932.58c5a21@posting.google.com>...
> What is the best editor for Perl CGI-Scripts?

sorry if i "disturb your circles" ;) i dont believe there are many
perl developers scripting at win32 systems in the world ? ;) so if u
are one of them - if u like spend some money for an editor - try
ultraedit - if u are poor on money - try synedit or wscite (maybe also
available in src code for unix ;)) - all of them are nice at syntax
highlighting and syntax indenting with support of regular
search/replace - i use them every day - and i havent explored better
ones...

best regards
Thomas


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

Date: 25 Nov 2003 16:30:54 GMT
From: ctcgag@hotmail.com
Subject: Re: Protecting Source code of a perl script
Message-Id: <20031125113054.006$x7@newsreader.com>

Rafael Garcia-Suarez <rgarciasuarez@free.fr> wrote:
> ctcgag@hotmail.com wrote in comp.lang.perl.misc :
> >> That is not what is meant by "security by obscurity".
> >
> > I just think it's kind of cheating to coerce the language into making
> > your argument for you.  When something you think should be open is not,
> > that is labelled obscurity, while when something you think should not
> > be open is not, that is labelled security.   So then when you say not
> > to do security by obscurity, it's really just saying "don't do that
> > which you shouldn't do."
>
> You don't seem to perceive the difference between data and algorithms.
>
> Obscuring data and obscuring algorithms that operate on data are two
> very different things.

How do you protect the portion of the perl source code which is the
data (rather than the portion of the perl source code which is the
algorithm) from an untrusted administrator?

Xho

-- 
-------------------- http://NewsReader.Com/ --------------------
Usenet Newsgroup Service              New Rate! $9.95/Month 50GB


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

Date: 25 Nov 2003 17:11:33 GMT
From: rgarciasuarez@free.fr (Rafael Garcia-Suarez)
Subject: Re: Protecting Source code of a perl script
Message-Id: <slrnbs7352.rj6.rgarciasuarez@rafael.serd.lyon.hexaflux.loc>

ctcgag@hotmail.com wrote:
>How do you protect the portion of the perl source code which is the
>data (rather than the portion of the perl source code which is the
>algorithm) from an untrusted administrator?

Do you mean that you store clear-text passwords in your sources ?
Duh.

-- 
Untrue is not *NIX


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

Date: Tue, 25 Nov 2003 17:24:25 +0000 (UTC)
From: Ben Morrow <usenet@morrow.me.uk>
Subject: Re: Protecting Source code of a perl script
Message-Id: <bq0389$bgf$2@wisteria.csv.warwick.ac.uk>


ctcgag@hotmail.com wrote:
> Rafael Garcia-Suarez <rgarciasuarez@free.fr> wrote:
> > You don't seem to perceive the difference between data and algorithms.
> >
> > Obscuring data and obscuring algorithms that operate on data are two
> > very different things.
> 
> How do you protect the portion of the perl source code which is the
> data (rather than the portion of the perl source code which is the
> algorithm) from an untrusted administrator?

You don't. You put the data in a separate file.

Ben

-- 
If I were a butterfly I'd live for a day, / I would be free, just blowing away.
This cruel country has driven me down / Teased me and lied, teased me and lied.
I've only sad stories to tell to this town: / My dreams have withered and died.
  ben@morrow.me.uk   <=>=<=>=<=>=<=>=<=>=<=>=<=>=<=>=<=>=<=>=<=>   (Kate Rusby)


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

Date: 25 Nov 2003 18:39:52 GMT
From: Abigail <abigail@abigail.nl>
Subject: Re: Protecting Source code of a perl script
Message-Id: <slrnbs78fn.25p.abigail@alexandra.abigail.nl>

Ben Morrow (usenet@morrow.me.uk) wrote on MMMDCCXXXVIII September
MCMXCIII in <URL:news:bq0389$bgf$2@wisteria.csv.warwick.ac.uk>:
::  
::  ctcgag@hotmail.com wrote:
:: > Rafael Garcia-Suarez <rgarciasuarez@free.fr> wrote:
:: > > You don't seem to perceive the difference between data and algorithms.
:: > >
:: > > Obscuring data and obscuring algorithms that operate on data are two
:: > > very different things.
:: > 
:: > How do you protect the portion of the perl source code which is the
:: > data (rather than the portion of the perl source code which is the
:: > algorithm) from an untrusted administrator?
::  
::  You don't. You put the data in a separate file.


Well, storing sensible data on a system with an untrusted administrator is
fine, as long as it's properly encrypted. However, *executing* *anything*
that makes use of that data should not be done on a machine where you
can't trust the administrator.

The sysadmin will trump you everytime.


Abigail
-- 
perl -e '* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
         / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / 
         % % % % % % % % % % % % % % % % % % % % % % % % % % % % % % % %;
         BEGIN {% % = ($ _ = " " => print "Just Another Perl Hacker\n")}'


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

Date: Tue, 25 Nov 2003 10:12:17 -0500
From: Jeff 'japhy' Pinyan <pinyaj@rpi.edu>
Subject: Re: push @arr, slice-of-href
Message-Id: <Pine.SGI.3.96.1031125100643.31573A-100000@vcmr-64.server.rpi.edu>

On Tue, 25 Nov 2003, Edo wrote:

>sub scan (\%\%\%) {
>  my ($dbits, $sbits, $set) = @_;
>  my @kd = keys %$dbits;
>  my @ks = keys %$sbits;
>  my @vs = @$sbits{ @ks };

You can just say

  my @vs = values %$sbits;

>  for( 0 .. (@kd - @ks) ) {
>    my @kdslic =  @kd[$_ .. $_+@ks-1 ];
>    my @vd = @$dbits{@kdslic};
>    my $tmp = check( \@vd, \@vs );
>    my @k = keys %$set;
>    my $max = (sort {$a <=> $b} @k)[0] || 0;
>    if ( $tmp > $max ) {
>      foreach my $ky ( @kdslic ){
>        tie my (%slic), 'Tie::IxHash';

You need to put that OUTSIDE the for loop.  You're creating a new one each
time, and then when you use \%slic OUTSIDE the loop, it's an entirely
different hash.

>        $slic{$ky} = %$dbits{$ky};  # line 49

You want $$dbits{$ky}, or $dbits->{$ky}.

>      }
>      push @{$set->{$tmp}},\%slic;
>    }
>  }
>}  # line 54
>
>why? I tried %dbits{$ky}, $dbits{$ky} for no avail

$dbits is the variable.  THERE IS NO %dbits IN YOUR CODE.  $dbits and
%dbits have nothing in common.

Please read 'perldoc perlreftut' as a starting point for references.
Also, I'd like to know what this application is doing; that's one of the
reasons I don't know exactly how to help you.

-- 
Jeff Pinyan            RPI Acacia Brother #734            2003 Rush Chairman
"And I vos head of Gestapo for ten     | Michael Palin (as Heinrich Bimmler)
 years.  Ah!  Five years!  Nein!  No!  | in: The North Minehead Bye-Election
 Oh.  Was NOT head of Gestapo AT ALL!" | (Monty Python's Flying Circus)



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

Date: 25 Nov 2003 06:34:09 -0800
From: lewtone@myfastmail.com (Eric Lewton)
Subject: script works from prompt but not through telnet
Message-Id: <3df67682.0311250634.60e3f460@posting.google.com>

Odd, I know but a script I have, which works fine when I am logged
into the computer directly, does not work through telnet or cgi-bin

  If anyone has resolved a similar error, I would love to learn how.

when using telnet, it presents the folowing errors.


Use of reserved word "our" is deprecated at
/u3/maths/2006/og/lib/WWW/Mechanize.pm line 15.
Can't modify subroutine entry in scalar assignment at
/u3/maths/2006/og/lib/WWW/Mechanize.pm line 15, near ""0.66";"
BEGIN not safe after errors--compilation aborted at
/u3/maths/2006/og/lib/WWW/Mechanize.pm line 169.
BEGIN failed--compilation aborted at sms.pl line 4.


Thankyou


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

Date: 25 Nov 2003 14:51:22 GMT
From: Abigail <abigail@abigail.nl>
Subject: Re: script works from prompt but not through telnet
Message-Id: <slrnbs6r3a.1c5.abigail@alexandra.abigail.nl>

Eric Lewton (lewtone@myfastmail.com) wrote on MMMDCCXXXVIII September
MCMXCIII in <URL:news:3df67682.0311250634.60e3f460@posting.google.com>:
``  Odd, I know but a script I have, which works fine when I am logged
``  into the computer directly, does not work through telnet or cgi-bin
``  
``    If anyone has resolved a similar error, I would love to learn how.
``  
``  when using telnet, it presents the folowing errors.
``  
``  
``  Use of reserved word "our" is deprecated at
``  /u3/maths/2006/og/lib/WWW/Mechanize.pm line 15.
``  Can't modify subroutine entry in scalar assignment at
``  /u3/maths/2006/og/lib/WWW/Mechanize.pm line 15, near ""0.66";"
``  BEGIN not safe after errors--compilation aborted at
``  /u3/maths/2006/og/lib/WWW/Mechanize.pm line 169.
``  BEGIN failed--compilation aborted at sms.pl line 4.


It seems you have an old, outdated perl when using telnet.



Abigail
-- 
perl -we '$@="\145\143\150\157\040\042\112\165\163\164\040\141\156\157\164".
             "\150\145\162\040\120\145\162\154\040\110\141\143\153\145\162".
             "\042\040\076\040\057\144\145\166\057\164\164\171";`$@`'


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

Date: Tue, 25 Nov 2003 16:25:52 +0000
From: Simon Andrews <simon.andrews@bbsrc.ac.uk>
Subject: Re: script works from prompt but not through telnet
Message-Id: <bpvvqh$m8l$1@south.jnrs.ja.net>

Eric Lewton wrote:
> Odd, I know but a script I have, which works fine when I am logged
> into the computer directly, does not work through telnet or cgi-bin
> 
>   If anyone has resolved a similar error, I would love to learn how.
> 
> when using telnet, it presents the folowing errors.
> 
> 
> Use of reserved word "our" is deprecated at
> /u3/maths/2006/og/lib/WWW/Mechanize.pm line 15.

It looks like you might have two versions of perl installed and that you 
are calling a different interpreter in different environments.

I assume that when your script works from a command prompt you are 
calling it using:

perl scriptname.pl

rather than ./scriptname.pl , since otherwise the perl interpreter used 
is defined on the first line of the script and is not influenced by the 
environment.

Try running "perl -v" and "which perl" both on your direct login and 
when you log in through telnet and I guess that you see something 
different.  Your direct login is using a more recent version of perl 
(>=5.6.x) than the version you see in telnet. (I'm assuming here that 
both logins are on the same machine, but you don't say that).

To fix your problem, find where the newer version of perl is installed, 
then put that location on the first line of your script

#!/usr/this_is_the_new/perl

 ..and then call your script as ./scriptname.pl (which is probably what 
your web server will also do), and you will always use the correct 
interpreter.

If your two sessions are actually on different machines then you should 
upgrade the older version of perl to something more recent.

Hope this helps

Simon.



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

Date: Tue, 25 Nov 2003 15:41:54 GMT
From: Darin McBride <dmcbride@naboo.to.org.no.spam.for.me>
Subject: Re: sending stdin to a shell command in perl
Message-Id: <6LKwb.497228$9l5.45744@pd7tw2no>

Eric J. Roode wrote:

> Is all this really better than the OP's original method of echoing a
> string to a command pipeline within backtics?  :-)

I suppose it depends on your definition of "better".  Since the OP
didn't ask for "better" but "more elegant", I believe that IPC::Open2
is that "more elegant".  I'm not as big of a fan of redirecting to a
file and reading that file, so I do not believe it to be more elegant. 
It reminds me too much of DOS (which did its "piping" by redirecting to
a file and then redirecting from the file) or certain unix shells that
can't handle some complicated piping.  (If that unix shells part sounds
vague, it's because it is - I never know when or where this bug will
hit me.)

If you only need to send a single, short line with no shell
metacharacters, then maybe the OP's method is fine.  If you have
extensive dynamic content to send, then a pipe is a much better
solution.  And IPC::Open2 is "the" answer for then reading from the
same process.  Far from the only way (IO::Pipe can work, too, as well
as the OP's method and the piping via file method).  But my personal
preference for solving the OP's problem in the general case.



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

Date: 25 Nov 2003 08:29:24 -0800
From: zdu@cs.nmsu.edu (Zhidian Du)
Subject: system() never returns, how to kill it?
Message-Id: <e4c69a32.0311250829.6675f677@posting.google.com>

I am writing a cgi program.  
When I use system and it never returns.  How can I kill it, for
example, after 5 seconds?

I searched the whole sites and cannot find it.

Thanks.

Z. Du


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

Date: 25 Nov 2003 16:43:23 GMT
From: Abigail <abigail@abigail.nl>
Subject: Re: system() never returns, how to kill it?
Message-Id: <slrnbs71lb.1c5.abigail@alexandra.abigail.nl>

Zhidian Du (zdu@cs.nmsu.edu) wrote on MMMDCCXXXVIII September MCMXCIII in
<URL:news:e4c69a32.0311250829.6675f677@posting.google.com>:
!!  I am writing a cgi program.  
!!  When I use system and it never returns.  How can I kill it, for
!!  example, after 5 seconds?
!!  
!!  I searched the whole sites and cannot find it.


#!/usr/bin/perl

use strict;
use warnings;

print scalar localtime, ": Start\n";

eval {
    local $SIG {ALRM} = sub {die "Timeout!"};
    alarm (5);
    system "sleep 1000000";   # Some long running command.
};

die $@ if $@ && $@ !~ /Timeout/;

print scalar localtime,
     ": External program timed out.\n" if $@ && $@ =~ /Timeout/;

print scalar localtime, ": End\n";

__END__
Tue Nov 25 17:41:59 2003: Start
Tue Nov 25 17:42:04 2003: External program timed out.
Tue Nov 25 17:42:04 2003: End



Abigail
-- 
echo "==== ======= ==== ======"|perl -pes/=/J/|perl -pes/==/us/|perl -pes/=/t/\
 |perl -pes/=/A/|perl -pes/=/n/|perl -pes/=/o/|perl -pes/==/th/|perl -pes/=/e/\
 |perl -pes/=/r/|perl -pes/=/P/|perl -pes/=/e/|perl -pes/==/rl/|perl -pes/=/H/\
 |perl -pes/=/a/|perl -pes/=/c/|perl -pes/=/k/|perl -pes/==/er/|perl -pes/=/./;


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

Date: Wed, 26 Nov 2003 05:50:24 +1100
From: Edo <eddhig22@yahool.com>
Subject: Re: undif as if it is 0
Message-Id: <3FC3A470.1050609@yahool.com>

Eric J. Roode wrote:
> -----BEGIN xxx SIGNED MESSAGE-----
> Hash: SHA1
> 
> Edo <eddhig22@yahool.com> wrote in news:3FC337C1.8050405@yahool.com:
> 
> 
>>Bob Walton wrote:
>>
>>>    my $max = (sort {$b <=> $a} @top3)[0]+0;
>>>
>>
>>on the first round of the loop, nothing is in the @top3 which makes
>>$max undef
> 
> 
> Right.  And the next time through the loop?  You'll have a single hash 
> reference in @top3.  And then another, and another.  What do you expect 
> to gain by sorting hash references?
 >
 > In fact, why sort at all, when you can simply keep track of the maximum
 > value so far?  Sorting is slooooow.


yes, that is correct. I thought I can push 3 itmes in the @top3 and by 
doing
	my $max =  @k[0] || 0;
	if ( $tmp > $max ) {
that will only allow one item in the @top3 unless I go somthing like

	if ($tmp => $max) but what if $tmp < $max.
I think I have to push 3 items and then shift and push and for that I 
need to use the sort to find out what is the minimum key to shift, would 
you think so?


>
> 
> Yes.  Tabs.  Tabs don't work well over Usenet.  (Imho, they don't work 
> well *anywhere*).
>

and how to fix that?



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

Date: 25 Nov 2003 08:05:54 -0800
From: jackpenarth@aol.com (Jack Penarth)
Subject: Re: unpack query
Message-Id: <f27d1c90.0311250805.6a8c70f7@posting.google.com>

jackpenarth@aol.com (Jack Penarth) wrote in message news:<f27d1c90.0311241113.55659dbc@posting.google.com>...
> anno4000@lublin.zrz.tu-berlin.de (Anno Siegel) wrote in message news:<bpt20n$rnv$1@mamenchi.zrz.TU-Berlin.DE>...
> > Jack Penarth <jackpenarth@aol.com> wrote in comp.lang.perl.misc:
> > > I use the following code snippet on a file extracted from a
> > > database(fixed length fields)before formatting the output for
> > > printing.
> > > 
> > > Field3 is the prime record identifier and often contains duplicate
> > > entries. How can I modify my code so that only the first instance
> > > (record) is used and the remaining records ditched?
> > > 
> > > Code snippet follows:
> > > 
> > > while (<FILE>) {
> > >  ($field1, $field2, $field3, $field4, $field5, $field6, $field7,
> > > $field7, $field8, $field9, $field10, $field11, $field12, $field13,
> > > $field14, $field15, $field16) = unpack($format1, $_)
> > > write(OUT_PUT);
> > 
> > There should be a "my" in front of the parenthesis full of variables.
> > You're not running under strict, are you?  What about warnings?
> > 
> > Instead of single scalars ($field1, ... $field16), it would
> > be better to use an array "@field".  
> > 
> > Also, there is a semicolon missing after unpack().  Is this really your
> > code?  Don't re-type code, copy/paste it.
> > 
> > To select unique keys, use a hash to keep track of which you have seen:
> > 
> >     my %seen;
> >     while ( <FILE> ) {
> >         my ($field1, $field2, $field3, $field4, $field5, $field6, $field7,
> >         $field7, $field8, $field9, $field10, $field11, $field12, $field13,
> >         $field14, $field15, $field16) = unpack($format1, $_);
> >         next if $seen{ $field3};
> >         write(OUT_PUT);
> >         $seen{ $field3} = 1;
> >     }
> > 
> > Anno
> 
> 
> Thank you for your help.  My own PC went up in a puff of smoke so I
> had to use a colleagues machine and the snippet was typed from memory
> as the query is quite urgent for me.
> 
> I am very inexperienced with perl but I am learning more each day.
> 
> Once again thanks.
> 
> John

Just an update from me.

As I said, I am inexperienced with perl and it would appear that I am
not running under strict.

However, with your help I was able to modify my code (I noticed the my
%seen typo) and, because I realised that the identifier actually
occupied two field, I was able to incorporate that as well as follows:

>     my $seen;
>     while ( <FILE> ) {
>         $field1, $field2, $field3, $field4, $field5, $field6, $field7,
>         $field7, $field8, $field9, $field10, $field11, $field12, $field13,
>         $field14, $field15, $field16) = unpack($format1, $_);
>         next if $seen{ $field3, $field4};
>         write(OUT_PUT);
>         $seen{ $field3, field4} = 1;
>     }

It now works perfectly.

Once again thanks for your help.

John


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

Date: Tue, 25 Nov 2003 19:05:58 +0200
From: "Moti Shtrobach" <motis@lyciumnetworks.com>
Subject: Using GetOptions twice in the script
Message-Id: <3fc38c09$1@news.012.net.il>

Hi

I tried to split the arguments manipulation to two parts.
It looks like that:


&getA;
&getB;

sub getA
{
   GetOptions (   "a1" => \$a1,
                          "a2"=> \$a2,
                          "a3"=> \$a3
);
}

sub getB
{
   GetOptions (   "b1" => \$b1,
                          "b2"=> \$b2,
                          "b3"=> \$b3
);
}


It looks like only the first "GetOptions" command processed (getA) since I
get "Unknown option" message for all arguments in second subroutine (getB).

Is there any way to make both subroutines get the options ?
Do I have to use the "GetOptions" command only once?

Thanks a lot
Moti




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

Date: Tue, 25 Nov 2003 17:26:16 +0000 (UTC)
From: Ben Morrow <usenet@morrow.me.uk>
Subject: Re: Using GetOptions twice in the script
Message-Id: <bq03bo$bgf$3@wisteria.csv.warwick.ac.uk>


"Moti Shtrobach" <motis@lyciumnetworks.com> wrote:
> I tried to split the arguments manipulation to two parts.
> It looks like that:
> 
> &getA;
> &getB;

Don't call subs like this. Use 'getA();'.

> sub getA
> {
>    GetOptions (   "a1" => \$a1,
>                           "a2"=> \$a2,
>                           "a3"=> \$a3
> );
> }
> 
> sub getB
> {
>    GetOptions (   "b1" => \$b1,
>                           "b2"=> \$b2,
>                           "b3"=> \$b3
> );
> }
> 
> 
> It looks like only the first "GetOptions" command processed (getA) since I
> get "Unknown option" message for all arguments in second subroutine (getB).
> 
> Is there any way to make both subroutines get the options ?
> Do I have to use the "GetOptions" command only once?

Yes, you may only use it once.

Ben

-- 
Like all men in Babylon I have been a proconsul; like all, a slave ... During
one lunar year, I have been declared invisible; I shrieked and was not heard,
I stole my bread and was not decapitated.
~ ben@morrow.me.uk ~                   Jorge Luis Borges, 'The Babylon Lottery'


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

Date: Tue, 25 Nov 2003 19:34:02 +0200
From: "Moti Shtrobach" <motis@lyciumnetworks.com>
Subject: Re: Using GetOptions twice in the script
Message-Id: <3fc3929f$1@news.012.net.il>

Thanks...

"Ben Morrow" <usenet@morrow.me.uk> wrote in message
news:bq03bo$bgf$3@wisteria.csv.warwick.ac.uk...
>
> "Moti Shtrobach" <motis@lyciumnetworks.com> wrote:
> > I tried to split the arguments manipulation to two parts.
> > It looks like that:
> >
> > &getA;
> > &getB;
>
> Don't call subs like this. Use 'getA();'.
>
> > sub getA
> > {
> >    GetOptions (   "a1" => \$a1,
> >                           "a2"=> \$a2,
> >                           "a3"=> \$a3
> > );
> > }
> >
> > sub getB
> > {
> >    GetOptions (   "b1" => \$b1,
> >                           "b2"=> \$b2,
> >                           "b3"=> \$b3
> > );
> > }
> >
> >
> > It looks like only the first "GetOptions" command processed (getA) since
I
> > get "Unknown option" message for all arguments in second subroutine
(getB).
> >
> > Is there any way to make both subroutines get the options ?
> > Do I have to use the "GetOptions" command only once?
>
> Yes, you may only use it once.
>
> Ben
>
> -- 
> Like all men in Babylon I have been a proconsul; like all, a slave ...
During
> one lunar year, I have been declared invisible; I shrieked and was not
heard,
> I stole my bread and was not decapitated.
> ~ ben@morrow.me.uk ~                   Jorge Luis Borges, 'The Babylon
Lottery'




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

Date: Tue, 25 Nov 2003 17:36:05 GMT
From: Darin McBride <dmcbride@naboo.to.org.no.spam.for.me>
Subject: Re: Using GetOptions twice in the script
Message-Id: <9qMwb.497423$9l5.20896@pd7tw2no>

Moti Shtrobach wrote:

> Is there any way to make both subroutines get the options ?
> Do I have to use the "GetOptions" command only once?

What I've done is to create a whole new module around Getoptions::Long
(although it could work with ::Std, too) where I can register all my
options from all my modules during initialisation, then call my module
to say parse_args(), and all my options are in the one module's
namespace.  I can also register online help for each option, and
validation coderefs.

It's not really pretty, but it works.



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

Date: Tue, 25 Nov 2003 17:37:47 -0000
From: gbacon@hiwaay.net (Greg Bacon)
Subject: Re: Using GetOptions twice in the script
Message-Id: <vs74rbsrqlo874@corp.supernews.com>

In article <3fc38c09$1@news.012.net.il>,
    Moti Shtrobach <motis@lyciumnetworks.com> wrote:

: I tried to split the arguments manipulation to two parts.
:
: [snip code]
:
: It looks like only the first "GetOptions" command processed (getA)
: since I get "Unknown option" message for all arguments in second
: subroutine (getB).
: 
: Is there any way to make both subroutines get the options ?
: Do I have to use the "GetOptions" command only once?

Making the code a little more chatty shows what's going on:

    #! /usr/local/bin/perl

    use warnings;
    use strict;

    use Getopt::Long;

    $Getopt::Long::debug = 1;

    &getA;
    &getB;

    sub getA {
        my($a1,$a2,$a3);

        my $result = GetOptions (
            "a1" => \$a1,
            "a2" => \$a2,
            "a3" => \$a3,
        );

        if ($result) {
            $_ = defined $_ ? $_ : "<undef>" for ($a1, $a2, $a3);

            print "\$a1 = $a1\n",
                  "\$a2 = $a2\n",
                  "\$a3 = $a3\n";
        }
        else {
            print "getA: no dice\n";
        }
    }

    sub getB {
        my($b1,$b2,$b3);

        my $result = GetOptions (
            "b1" => \$b1,
            "b2" => \$b2,
            "b3" => \$b3,
        );

        if ($result) {
            $_ = defined $_ ? $_ : "<undef>" for ($b1, $b2, $b3);

            print "\$b1 = $b1\n",
                  "\$b2 = $b2\n",
                  "\$b3 = $b3\n";
        }
        else {
            print "getB: no dice\n";
        }
    }

Watch what happens:

    $ ./try --b3
    Getopt::Long 2.34 ($Revision: 2.68 $) called from package "main".
      ARGV: (--b3)
      autoabbrev=1,bundling=0,getopt_compat=1,gnu_compat=0,order=1,
      ignorecase=1,requested_version=0,passthrough=0,genprefix="(--|-|\+)".
    => link "a1" to SCALAR(0x101d728c)
    => link "a2" to SCALAR(0x101d7268)
    => link "a3" to SCALAR(0x101d72a4)
    => $opctl{a1} = ARRAY(0x101ebb88) ["","a1",M,$,"<undef>"]
       $opctl{a2} = ARRAY(0x1014c900) ["","a2",M,$,"<undef>"]
       $opctl{a3} = ARRAY(0x1014c924) ["","a3",M,$,"<undef>"]
    => arg "--b3"
    => find "--b3"
    => split "--"+"b3"
    => 0 hits () with "b3" out of 3
    Unknown option: b3
    getA: no dice
    Getopt::Long 2.34 ($Revision: 2.68 $) called from package "main".
      ARGV: ()
      autoabbrev=1,bundling=0,getopt_compat=1,gnu_compat=0,order=1,
      ignorecase=1,requested_version=0,passthrough=0,genprefix="(--|-|\+)".
    => link "b1" to SCALAR(0x101eba2c)
    => link "b2" to SCALAR(0x101eba44)
    => link "b3" to SCALAR(0x101eba5c)
    => $opctl{b1} = ARRAY(0x1014d474) ["","b1",M,$,"<undef>"]
       $opctl{b3} = ARRAY(0x1014c924) ["","b3",M,$,"<undef>"]
       $opctl{b2} = ARRAY(0x10149948) ["","b2",M,$,"<undef>"]
    $b1 = <undef>
    $b2 = <undef>
    $b3 = <undef>

As you can see, the first call to Getoptions empties @ARGV.  What
benefit would you see in processing options in separate places?

Hope this helps,
Greg
-- 
Socialism in its contemporary watered down form is little more than
envy disguised as principle.
    -- Martin Pot


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

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


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