[22220] in Perl-Users-Digest

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

Perl-Users Digest, Issue: 4441 Volume: 10

daemon@ATHENA.MIT.EDU (Perl-Users Digest)
Tue Jan 21 14:10:39 2003

Date: Tue, 21 Jan 2003 11: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)

Perl-Users Digest           Tue, 21 Jan 2003     Volume: 10 Number: 4441

Today's topics:
    Re: Search array and return element? (Tad McClellan)
    Re: Search array and return element? (Helgi Briem)
    Re: Search array and return element? <camerond@mail.uca.edu>
    Re: Search array and return element? (Anno Siegel)
    Re: server and client on the same port news@roaima.freeserve.co.uk
    Re: simple perl cgi server <kumpf@mit.edu>
    Re: Starting with Perl (Tad McClellan)
    Re: Still a little variable trouble. <spikey-wan@bigfoot.com>
    Re: Still a little variable trouble. (Ben Morrow)
        Strange behavior for "system" call. <mrohm@gtemail.net>
    Re: Strange behavior for "system" call. <mothra@nowhereatall.com>
    Re: Strange behavior for "system" call. (Anno Siegel)
    Re: Strange behavior for "system" call. <nobull@mail.com>
    Re: Strange behavior for "system" call. <mrohm@gtemail.net>
    Re: Strange behavior for "system" call. <nobull@mail.com>
    Re: Strange behavior for "system" call. (Anno Siegel)
        Digest Administrivia (Last modified: 6 Apr 01) (Perl-Users-Digest Admin)

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

Date: Tue, 21 Jan 2003 10:27:19 -0600
From: tadmc@augustmail.com (Tad McClellan)
Subject: Re: Search array and return element?
Message-Id: <slrnb2qt77.37u.tadmc@magna.augustmail.com>

Cameron Dorey <camerond@mail.uca.edu> wrote:
> Anno Siegel wrote:

>>     grep 1 + index( $array[ $_], $string), 0 .. @array - 1;
> 
> 
> There's always the less elegant solution for those of us who did not 
> grow up with grep (it does the same thing AFAIK, but I can get my mind 
> around it easier):
> 
> for ($int=0; $int<@ary; ++$int) {


If the idea is to make it easier to understand, then you've
missed an opportunity with the line above.


   foreach $int ( 0 .. $#ary ) {  # does the same thing

or

   foreach my $int ( 0 .. $#ary ) {  # does the better thing   :-)


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


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

Date: Tue, 21 Jan 2003 16:30:54 GMT
From: helgi@decode.is (Helgi Briem)
Subject: Re: Search array and return element?
Message-Id: <3e2d7554.1517238904@news.cis.dfn.de>

On 21 Jan 2003 06:41:14 -0800, ryan@jimryan.com (jim ryan)
wrote:

>If I issue a system command that returns multple lines into an array,

my @array = qx/$system_command/ 
  or die "Error running $system_command:$!\n";

>how do I subsequently search through that array for a string and
>return all the element numbers where that string occurs?

my @array = grep /$desired_string/,@array;

-- 
Regards, Helgi Briem
helgi AT decode DOT is


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

Date: Tue, 21 Jan 2003 10:37:46 -0600
From: Cameron Dorey <camerond@mail.uca.edu>
Subject: Re: Search array and return element?
Message-Id: <3E2D775A.6000702@mail.uca.edu>

Tad McClellan wrote:

> Cameron Dorey <camerond@mail.uca.edu> wrote:
>>
>>for ($int=0; $int<@ary; ++$int) {
>>
> 
> 
> If the idea is to make it easier to understand, then you've
> missed an opportunity with the line above.
> 
> 
>    foreach $int ( 0 .. $#ary ) {  # does the same thing
> 
> or
> 
>    foreach my $int ( 0 .. $#ary ) {  # does the better thing   :-)


Thanks, Tad, the for expression I used is the one in the LPoW32S (Gecko) 
book, and that's the way I've always done it. foreach is easier to read 
and write, I need to get used to it.

(BTW, the "my" was there, just in a line at the top of the script, if I 
don't use -w and strict, I don't get _anything_ done right)


Cameron

-- 
Cameron Dorey
Associate Professor of Chemistry
University of Central Arkansas
Phone: 501-450-5938
camerond@mail.uca.edu



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

Date: 21 Jan 2003 17:28:02 GMT
From: anno4000@lublin.zrz.tu-berlin.de (Anno Siegel)
Subject: Re: Search array and return element?
Message-Id: <b0jvv2$4nh$1@mamenchi.zrz.TU-Berlin.DE>

Cameron Dorey  <camerond@mail.uca.edu> wrote in comp.lang.perl.misc:
> Anno Siegel wrote:
> 
> > jim ryan <ryan@jimryan.com> wrote in comp.lang.perl.misc:
> > 
> >>If I issue a system command that returns multple lines into an array,
> >>how do I subsequently search through that array for a string and
> >>return all the element numbers where that string occurs?
> >>
> > 
> >     grep 1 + index( $_, $string), @array;
> > 
> > Oh, you said element numbers.  Is that the same as line numbers?
> > If so:
> > 
> >     grep 1 + index( $array[ $_], $string), 0 .. @array - 1;
> 
> 
> There's always the less elegant solution for those of us who did not 
> grow up with grep (it does the same thing AFAIK, but I can get my mind 
> around it easier):
> 
> for ($int=0; $int<@ary; ++$int) {

Better: "( my $int; ... )" to reduce the scope of the loop variable
to the loop.

> 	if ($ary[$int] =~ /$string/) {
                          ^^^^^^^^^
Use /\Q$string\E/ if you want to use a regex to match an arbitrary
substring.  An awful lot of characters have special meaning in a regex,
\Q implicitly quotes those.

> 		print "$int, $ary[$int]\n";	
> 	}
> }

It does some more and some less.  grep() returns its result as a list.
To do the same, you would have to use a collector variable and something
like "push @coll, $int" instead of your print statement.  Printing the
results of an operation is fine to demonstrate that it works, but not
useful if the result is to be processed further.  This is one of the
reasons grep() is popular with Perl programmers.

What makes the code a bit hard to read is probably the use of "1 + index()"
as a boolean expression.  The expression is true when index() detects the
presence of a substring, because index() returns something non-negative
in that case.  If the substring is not present, index() returns -1, so
in that case the expression is false.  It should properly be written as
"index() != -1", but that's no paragon of clarity either.

Anno


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

Date: Tue, 21 Jan 2003 17:06:20 +0000
From: news@roaima.freeserve.co.uk
Subject: Re: server and client on the same port
Message-Id: <cmuj0b.r6m.ln@moldev.cmagroup.co.uk>

Markus Rachbauer <markus.rachbauer@chello.at> wrote:
> sorry, if i described my problem not exactly enough.

You have described it well.

1. As Lao said, it's not a Perl problem

2. As Benjamin said, you can't do that.

TCP/IP identifies a connection uniquely by the tuple (source host,
source port, destination host, destination port). You *cannot* have
two separate but simultaneous TCP/IP conversations with the same four
values. Period.

You will need to revisit your communication protocol as you *cannot*
implement this as described.

> is there any workaround to send a message through the same
> port where a server is already listening?

Yes, you use the same socket endpoints that you used to initiate the
conversation in the first place.

Chris
-- 
@s=split(//,"Je,\nhn ersloak rcet thuarP");$k=$l=@s;for(;$k;$k--){$i=($i+1)%$l
until$s[$i];$c=$s[$i];print$c;undef$s[$i];$i=($i+(ord$c))%$l}


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

Date: Tue, 21 Jan 2003 11:38:28 -0500
From: "Adam Kumpf" <kumpf@mit.edu>
Subject: Re: simple perl cgi server
Message-Id: <3e2d7697$0$3948$b45e6eb0@senator-bedfellow.mit.edu>

Thanks for everyone's input.. I got it up and working in under 2 hours.  : )

Best Regards,
    Adam Kumpf
kumpf@mit.edu



"Adam Kumpf" <kumpf@mit.edu> wrote in message
news:3e2c8637$0$3930$b45e6eb0@senator-bedfellow.mit.edu...
> I am looking for a simple application that allows me to host CGI scripts
on
> my windows machine.  I have a pretty quick connection and would like to
host
> a little image serving script I made.  I'm just looking for something
small
> and simple that works.  Any feedback is greatly appreciated.
>
> Thank you very much for your time.
>
> Best Regards,
>     Adam Kumpf
> kumpf@mit.edu
> http://web.mit.edu/kumpf/www/index.html
>
>




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

Date: Tue, 21 Jan 2003 10:20:41 -0600
From: tadmc@augustmail.com (Tad McClellan)
Subject: Re: Starting with Perl
Message-Id: <slrnb2qsqp.363.tadmc@magna.augustmail.com>

Brian Smart <brian.smart@blueyonder.co.uk> wrote:

> Thanks


Do you really mean that?


> "Tad McClellan" <tadmc@augustmail.com> wrote in message
> news:slrnb2qn87.2l4.tadmc@magna.augustmail.com...


[snip TOFU this one last time]


>> [snip TOFU, please see the Posting Guidelines that are posted here weekly]


C'mon now, get with the program before it is too late.


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


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

Date: Tue, 21 Jan 2003 16:24:58 -0000
From: "Richard S Beckett" <spikey-wan@bigfoot.com>
Subject: Re: Still a little variable trouble.
Message-Id: <b0jsaf$ta$1@newshost.mot.com>

"Ben Morrow" <mauzo@ux-ma160-6.csv.warwick.ac.uk> wrote

> Two ways (and if you think the above is messy, you'll probably think the
same
> of these, but I'm afraid I'll have to tell you're wrong here :):
>
> -command => sub {
>   $text_area->insert(blah...);
>   whatever else...
> }
>
> or, if the sub is too long to inline or you want to set several callbacks
to
> call the same thing,
>
> -command => sub { text_sub($text_area) }
>
> This _is_ the right answer. Closures (the subs above, which 'trap' the
value
> of $text_area even though they may be called at any time) are a very good
way
> to deal with getting lexicals (my variables) to places they otherwise
couldn't
> be. Adn passing arguments to a sub is nearly always better than using
globals.

Thanks Ben.

I can now move my subroutines out of the subroutine, which I suppose is
another Bad Thing (TM) to do ;-)

R.




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

Date: Tue, 21 Jan 2003 18:42:48 +0000 (UTC)
From: mauzo@mimosa.csv.warwick.ac.uk (Ben Morrow)
Subject: Re: Still a little variable trouble.
Message-Id: <b0k4b8$jbm$1@wisteria.csv.warwick.ac.uk>

"Richard S Beckett" <spikey-wan@bigfoot.com> wrote:
>Thanks Ben.

No problem :)
 
>I can now move my subroutines out of the subroutine, which I suppose is
>another Bad Thing (TM) to do ;-)

Err, I'm confused by this. Do you mean you have

sub foo {
  sub bar {
  }
}

? This doesn't do anything special, except make variables in foo available in
bar, which if you really need to is clearer done as

{
  my $var;
  sub foo { ... }
  sub bar { ... }
}

 . But again, param passing is usually better.

Then again, you might mean something quite else?

Ben


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

Date: Tue, 21 Jan 2003 09:40:11 -0800
From: MR <mrohm@gtemail.net>
Subject: Strange behavior for "system" call.
Message-Id: <3E2D85FB.3030708@gtemail.net>


I'm having trouble using "system" to invoke a command with
arguments.

The behavior is that the program being called doesn't appear
to recognize the arguments being provided, yet when I cut-paste
the exact same line to the shell prompt everything works just
fine.

I'm sure that the arguments are being passed through, but I
cannot explain why the same program will run under the shell
correctly but not under the "system" command.

Any help?



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

Date: Tue, 21 Jan 2003 09:45:26 -0800
From: "Mothra" <mothra@nowhereatall.com>
Subject: Re: Strange behavior for "system" call.
Message-Id: <3e2d8664$1@usenet.ugs.com>

Hello,

"MR" <mrohm@gtemail.net> wrote in message
news:3E2D85FB.3030708@gtemail.net...
>
> I'm having trouble using "system" to invoke a command with
> arguments.
>
> The behavior is that the program being called doesn't appear
> to recognize the arguments being provided, yet when I cut-paste
> the exact same line to the shell prompt everything works just
> fine.
>
> I'm sure that the arguments are being passed through, but I
> cannot explain why the same program will run under the shell
> correctly but not under the "system" command.
>
> Any help?
Please provide a SMALL code sample so the people here
can help you better.

Mothra




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

Date: 21 Jan 2003 17:44:56 GMT
From: anno4000@lublin.zrz.tu-berlin.de (Anno Siegel)
Subject: Re: Strange behavior for "system" call.
Message-Id: <b0k0uo$4nh$2@mamenchi.zrz.TU-Berlin.DE>

MR  <mrohm@gtemail.net> wrote in comp.lang.perl.misc:
> 
> I'm having trouble using "system" to invoke a command with
> arguments.
> 
> The behavior is that the program being called doesn't appear
> to recognize the arguments being provided, yet when I cut-paste
> the exact same line to the shell prompt everything works just
                         ^^^^^^^^^^^^^^^^
What shell?  We're not clairvoyant.

> fine.
> 
> I'm sure that the arguments are being passed through, but I
> cannot explain why the same program will run under the shell
> correctly but not under the "system" command.
> 
> Any help?

Not without your showing us actual code, plus information about the
system you're running.  (It may well turn out not to be a Perl problem
at all.)

Anno


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

Date: 21 Jan 2003 17:48:33 +0000
From: Brian McCauley <nobull@mail.com>
Subject: Re: Strange behavior for "system" call.
Message-Id: <u9znpu5r7y.fsf@wcl-l.bham.ac.uk>

MR <mrohm@gtemail.net> writes:

> I'm having trouble using "system" to invoke a command with
> arguments.

I'm having trouble with my crystall ball.  I've looked in it but I
can't see your program.

> The behavior is that the program being called doesn't appear
> to recognize the arguments being provided, yet when I cut-paste
> the exact same line to the shell prompt everything works just
> fine.
> 
> I'm sure that the arguments are being passed through, but I
> cannot explain why the same program will run under the shell
> correctly but not under the "system" command.
> 
> Any help?

Please produce two minimal but complete scripts (one to take the place
of the external program) that you have actually run and found to
reproduce this behaviour.

Post them here unaltered. 

Meanwhile, can I confirm that you are not doing anything silly.  Can I
safely assume:

 1) You are calling the Perl script from the same shell prompt as you
    tried pasting the command.  (i.e they are not runnig as different
    users in different environments).

 2) The command is not a shell built-in (or alias or function...)

 3a) The shell you are using is linked to /bin/sh.
   or
 3b) There are no shell metacharacters in the command line.

-- 
     \\   ( )
  .  _\\__[oo
 .__/  \\ /\@
 .  l___\\
  # ll  l\\
 ###LL  LL\\


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

Date: Tue, 21 Jan 2003 10:04:16 -0800
From: MR <mrohm@gtemail.net>
Subject: Re: Strange behavior for "system" call.
Message-Id: <3E2D8BA0.3020705@gtemail.net>


Sorry, I know better...

The shell is /bin/tcsh, on a solaris box.

SunOS xxxx 5.8 Generic_108528-13 sun4u sparc SUNW,Sun-Fire

The perl version is:

This is perl, version 5.004_04 built for sparc-sun-solaris2.6


Here's the section of perl code as it stands now.  I've
also tried just providing a command string to system
and got the same results.

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

my @args = ("$starxtract", "-clean", "$cmd_file");
print "executing: @args\n";

system(@args) == 0 or die "system(@args) failed: $?\n";

-----------

I'm actually executing a csh wrapper which eventually
"execs" the program after setting a few environment
variables.


The perl program prints:

executing: /auto/cot_sw/wrappers/StarXtract -clean starXtract.cmd


The wrapper echoes  !*   (it sees the arguments):

-clean starXtract.cmd


The program doesn't seem to read the command file, so it dies.

I can execute the wrapper directly from the shell by cut-paste
of the "executing: ..." line, and everything is fine.

Thanks for any help.




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

Date: 21 Jan 2003 18:09:22 +0000
From: Brian McCauley <nobull@mail.com>
Subject: Re: Strange behavior for "system" call.
Message-Id: <u9r8b65q99.fsf@wcl-l.bham.ac.uk>

MR <mrohm@gtemail.net> writes:

> my @args = ("$starxtract", "-clean", "$cmd_file");
> print "executing: @args\n";
> 
> system(@args) == 0 or die "system(@args) failed: $?\n";

> I'm actually executing a csh wrapper

Does it have a proper csh shebang?

> I can execute the wrapper directly from the shell by cut-paste
> of the "executing: ..." line, and everything is fine.

> The shell is /bin/tcsh, on a solaris box.

Well if the csh wrapper lacks a shebang then it would be executed as
tcsh is called from tcsh but as sh if called from anything else.

-- 
     \\   ( )
  .  _\\__[oo
 .__/  \\ /\@
 .  l___\\
  # ll  l\\
 ###LL  LL\\


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

Date: 21 Jan 2003 18:21:20 GMT
From: anno4000@lublin.zrz.tu-berlin.de (Anno Siegel)
Subject: Re: Strange behavior for "system" call.
Message-Id: <b0k330$4nh$5@mamenchi.zrz.TU-Berlin.DE>

MR  <mrohm@gtemail.net> wrote in comp.lang.perl.misc:

[...]

> my @args = ("$starxtract", "-clean", "$cmd_file");
> print "executing: @args\n";
> 
> system(@args) == 0 or die "system(@args) failed: $?\n";

The list form of system() doesn't involve a shell.  This is mostly a
Good Thing, but it makes the "paste to shell" test for the command less
than conclusive.

> 
> -----------
> 
> I'm actually executing a csh wrapper which eventually
> "execs" the program after setting a few environment
> variables.
> 
> 
> The perl program prints:
> 
> executing: /auto/cot_sw/wrappers/StarXtract -clean starXtract.cmd
> 
> 

Note two newlines.

> The wrapper echoes  !*   (it sees the arguments):
> 
> -clean starXtract.cmd
> 
> 

Two newlines again!

> The program doesn't seem to read the command file, so it dies.

It doesn't find it.

> I can execute the wrapper directly from the shell by cut-paste
> of the "executing: ..." line, and everything is fine.

You forgot to chomp $cmd_file, it has a linefeed at the end.

Anno


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

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


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