[18320] in Perl-Users-Digest

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

Perl-Users Digest, Issue: 488 Volume: 10

daemon@ATHENA.MIT.EDU (Perl-Users Digest)
Wed Mar 14 14:10:33 2001

Date: Wed, 14 Mar 2001 11:10:17 -0800 (PST)
From: Perl-Users Digest <Perl-Users-Request@ruby.OCE.ORST.EDU>
To: Perl-Users@ruby.OCE.ORST.EDU (Perl-Users Digest)
Message-Id: <984597017-v10-i488@ruby.oce.orst.edu>
Content-Type: text

Perl-Users Digest           Wed, 14 Mar 2001     Volume: 10 Number: 488

Today's topics:
    Re: Monitoring a Serial Port (/dev/ttyd2) (Chris Fedde)
        Perl FAQ? <kellyboy@nospanner>
        Print own "die" message (BUCK NAKED1)
    Re: Small perl sort question (Anno Siegel)
    Re: Sorting multiple fields in a file. <shanem@ll.mit.edu>
    Re: Sorting multiple fields in a file. <peb@bms.umist.ac.uk>
    Re: Sorting multiple fields in a file. <mjcarman@home.com>
    Re: Sorting multiple fields in a file. nobull@mail.com
        Strange behavior in subroutine <lhswartw@ichips.intel.com>
    Re: Unix trap command in Perl on Win? (Anno Siegel)
        Useless use of addition (+) in a void context... <lmoran@wtsg.com>
    Re: Useless use of addition (+) in a void context... <tony_curtis32@yahoo.com>
    Re: Useless use of addition (+) in a void context... <lmoran@wtsg.com>
    Re: Useless use of addition (+) in a void context... <tony_curtis32@yahoo.com>
    Re: Useless use of addition (+) in a void context... (Abigail)
    Re: Using the 'use' directive, and page reloading in a  <nbr@newsbrowser.com>
        win32::shell::execute problem <founder@pege.org>
        XML::Parser <francois@idylic.com>
        Digest Administrivia (Last modified: 16 Sep 99) (Perl-Users-Digest Admin)

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

Date: Wed, 14 Mar 2001 16:52:26 GMT
From: cfedde@fedde.littleton.co.us (Chris Fedde)
Subject: Re: Monitoring a Serial Port (/dev/ttyd2)
Message-Id: <elNr6.947$T3.196892672@news.frii.net>

In article <98n4l5$b4u$1@murrow.corp.sgi.com>,
David Walford <davewal@echo.corp.sgi.com> wrote:
>Is there a way to have a perl script monitor a serial port on a local system in
>order to receive input from it?
>
>Here is a description on what I am trying to accomplish.  I have a Crestron
>system that can send a command (string) out onto a 9 pin serial.  I would like
>to have a perl script running on my system that constently looking for this
>command and respond instantly to it.
>
>Anyone have experience doing this and if so, where could I get examples and/or
>what should I read up on?
>

If it is a simple line oriented protocol then life is fery simple.

    open(PORT, '/dev/ttyd2') or die "$0: can't open /dev/ttyd2: $!";

    while (<PORT>) {
	print $_
    } 

If it is a character at a time thing then you might have to use sysopen and
sysread.
-- 
    This space intentionally left blank


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

Date: Wed, 14 Mar 2001 12:36:08 -0600
From: "kellyboy" <kellyboy@nospanner>
Subject: Perl FAQ?
Message-Id: <tavec0tilano28@corp.supernews.com>

Is there any Perl FAQ out there on the Net??

Not just "FAQ" but a really comprehensive FAQ that cover many topics under
Perl?

kellyboy

--





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

Date: Wed, 14 Mar 2001 12:10:31 -0600 (CST)
From: dennis100@webtv.net (BUCK NAKED1)
Subject: Print own "die" message
Message-Id: <4729-3AAFB417-13@storefull-247.iap.bryant.webtv.net>

I've studied die in perlfunc, but can't figure this out. How do you die
and print just your own message? For ex: when I used something like 

open(FH, "file.txt") or print "<font size=3 color=660000>Cannot open
this file</font>\n";

 .. and it fails to open, it prints my message, but also prints other
stuff from STDIN. I just want it to print my message.

I've also tried warn and carp.

IOW, I want to check a file open for success. If it fails, I want it to
print to the browser something like "File Could Not Be Opened" and
that's all I want it to print.

I'd also like to prevent my error msg from going to the server's default
screen for die messages.

Thanks for any assistance or tips.

--Dennis



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

Date: 14 Mar 2001 16:18:06 GMT
From: anno4000@lublin.zrz.tu-berlin.de (Anno Siegel)
Subject: Re: Small perl sort question
Message-Id: <98o5ju$9pg$3@mamenchi.zrz.TU-Berlin.DE>

According to Bryan Coon  <bcoon@sequenom.com>:
 
[...]

> not had time to figure it out.  I also would prefer not to suppress warnings,
> I am still new enough to perl that I want to see what I potentially may have
> screwed up :)

Not like that.  The more experienced you get in Perl, the less will
you want to switch off warnings.

Anno


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

Date: Wed, 14 Mar 2001 11:30:33 -0500
From: Shane McDaniel <shanem@ll.mit.edu>
Subject: Re: Sorting multiple fields in a file.
Message-Id: <3AAF9CA9.5796ECA6@ll.mit.edu>

Sort allows you to pass a customized sort function to it.  I'd recommend
looking this up somewhere.  If you have the Camel book it is in there. 

It goes something along the lines of you would basically make two custom
functions that looked like this.

sub sortfunc1 {
        $aind = (split ',', $a)[index you want to sort on];
        $bind = (split ',', $b)[index you want to sort on];

        return $aind <=> $bind;
        # or remmeber to use cmp is the data is a string and not
numeric.
}

then when you call sort you do

@sortedarray = sort sortfunc1, @array

I don't have any docs in front of my so the syntax might be a little off
but you get the idea.

Mike Flaherty wrote:
> 
> I am trying to write a web front end to our script that adds users to our
> systems (multiple platforms).  I have example data below.  The problem is
> that I need to sort this file by 2 fields - the second (usename = XXNNNN
> i.e. JB9876) and the last (training or production).
> 
> This needs to be done via perl only - no shell scripts please.  I have
> attached an excerpt of some code suggested to me but it only works for 2
> fields.  How can I sort data in a file that has more than 2 fields.  Any
> help would be greatly appreciated since all we can seem to hire is a bunch
> of drunks.
> 
> Thanks in Advance,
> 
> Mike
> Westwood, MA.
> 
> ***************************************
> 
> datafile.csv
> 
> Jim Beam,JB9876,jb9876,mf4350,flatts1,training
> Jack Daniels,JD1234,jd1234,bg1234,flatts1,training
> Johnny Walker,JW5678,jw5678,rr1234,jd7427,production
> Paul Masson,PM3456,pm3456,rr5555,,jd7427,training
> 
> ***************************************
> 
> use strict;
> 
> my %data;
> 
> # Read the data
> while (<DATA>) {
>     chomp;
>     my ($value, $key) = split /\,/, $_;
>     $data{$key} = $value;
> }
> 
> # Print the sorted values
> foreach (sort {$b <=> $a} keys %data) {
>     print "$data{$_},$_\n";
> }


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

Date: Wed, 14 Mar 2001 16:40:59 +0000
From: Paul Boardman <peb@bms.umist.ac.uk>
Subject: Re: Sorting multiple fields in a file.
Message-Id: <3AAF9F1B.D0390C0B@bms.umist.ac.uk>

Mike Flaherty wrote:
> 
> I am trying to write a web front end to our script that adds users to our
> systems (multiple platforms).  I have example data below.  The problem is
> that I need to sort this file by 2 fields - the second (usename = XXNNNN
> i.e. JB9876) and the last (training or production).
> 
> This needs to be done via perl only - no shell scripts please.  I have
> attached an excerpt of some code suggested to me but it only works for 2
> fields.  How can I sort data in a file that has more than 2 fields.  Any
> help would be greatly appreciated since all we can seem to hire is a bunch
> of drunks.

<snip data file>

if you know which two fields you want to sort by then you only need to
make a slight change to the program.

> # Read the data
> while (<DATA>) {
>     chomp;
>     my ($value, $key) = split /\,/, $_;
>     $data{$key} = $value;
> }

if you change the my ($value... line to 

>     my ($value, $key) = (split /\,/, $_)[$column1, $column2];

where $column1 and $column2 are the indexes of the columns you wish to
sort by. (so 0 and 1 if you just wanted to sort by the first two
columns).

HTH

Paul


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

Date: Wed, 14 Mar 2001 10:56:09 -0600
From: Michael Carman <mjcarman@home.com>
Subject: Re: Sorting multiple fields in a file.
Message-Id: <3AAFA2A9.EB191936@home.com>

Mike Flaherty wrote:
> 
> I have example data below.  The problem is that I need to sort 
> this file by 2 fields [...]

This is an FAQ. People here get upset if you don't check the
documentation before posting. Read up on the sort manpage and see
perlfaq4: "How do I sort an array by (anything)?"

For DOSish/Unixy systems, type this at a command prompt:
perldoc -f sort
perldoc -q sort

-mjc


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

Date: 14 Mar 2001 18:30:04 +0000
From: nobull@mail.com
Subject: Re: Sorting multiple fields in a file.
Message-Id: <u9hf0wgq4z.fsf@wcl-l.bham.ac.uk>

"Mike Flaherty" <mflaherty2@earthlink.net> writes:

> How can I sort data in a file that has more than 2 fields.

First slurp the file into an array.  For details see FAQ: How can I
read in an entire file all at once?

Then print the array sorted on multiple fields.  For details see FAQ:
How do I sort an array by (anything)?

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


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

Date: Wed, 14 Mar 2001 10:12:41 -0800
From: "Swanthog" <lhswartw@ichips.intel.com>
Subject: Strange behavior in subroutine
Message-Id: <98ocaq$rgv@news.or.intel.com>

Below is a short routine that executes and completes with correct results
when ran alone. The routine simply executes and retries a function or perl
statement and reports a timeout condition if the command doesn't complete
within a specified time period. When used in a larger script the same
routine will invariably fail. Any thoughts as to how something like this
could happen? Example useage is also shown below.

Thanks,
Larry S.
use strict;
use diagnostics;

#
# Prototypes
#
sub try( $);

sub try( $)
{
   my $ret_val = 0;
   my $command = shift;
   my $sleep_time = 1;
   my $cntr = 0;
   my $timeout = 5; # Wait 10 minutes before failing
   my $begin_time = time;

   while( not $ret_val)
   {
      $ret_val = eval $command;
      if( not $ret_val)
      {
         # Report the error only once
         if( $cntr++ == 0) {
            print "Error! Command: $command\n";
         }

         if(( time - $begin_time) >= $timeout)
         {
            print "Timeout occurred!\n" .
                     "Command: $command\n";
            return( undef);
         }
         sleep( $sleep_time);
      }
   }
   return $ret_val;
}

#
# Example Useage
#

#
# Globals
#
my $root_dir = 'h:/projects/sendem/';
my @a_num = qw( 50);
my $index = 0;
my $read_dat = '';

try("opendir A_DIR, '$root_dir'") or
   die "Can't open dir";

try("open A_FILE, '< $root_dir/test.old.txt'") or
   die "Can't open file";

try("sysread A_FILE, \$read_dat, $a_num[ $index]") or
   die "Can't read file";

print $read_dat;







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

Date: 14 Mar 2001 16:13:15 GMT
From: anno4000@lublin.zrz.tu-berlin.de (Anno Siegel)
Subject: Re: Unix trap command in Perl on Win?
Message-Id: <98o5ar$9pg$2@mamenchi.zrz.TU-Berlin.DE>

According to David L. Heim <dlheim@collins.rockwell.com>:
> -=-=-=-=-=-
> 
> Is there a way to mimic the behavior of the Unix "trap" command in
> Perl on a windows platform?

See what the %SIG array does.

[snip]

Anno


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

Date: Wed, 14 Mar 2001 12:39:53 -0500
From: Lou Moran <lmoran@wtsg.com>
Subject: Useless use of addition (+) in a void context...
Message-Id: <veavatoib1mlh6ul1v5s48ubnpbuhc62d4@4ax.com>

I am trying to write a program to figure out overtime hours in Perl
which I have already written in Lotus Notes.

I am getting the following error:

Useless use of addition (+) in a void context (... lines affected)
^^^^^^^^^^^^^^
What does this mean?  I haven't been able to look it up in perldoc.

I realize there are probably a million things that could be done
better here but I'm trying to slog through one problem at a time.

(Ultimtely this runs but it doesn't run as expected b/c it doesn't add
the 12 hours to PM)

I hope the code explains what I am trying to do, here it is:


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

my ($start, $leave, $ampm, $lunch, $hours, $hoursl, $othours);

print "What time did you start? (HH:MM) ";
$start = <STDIN>;
chomp $start;

#Do REGEX here for valid HH:MM input;

print "AM or PM?  ";	#This should
$ampm = <STDIN>;	#be a
chomp $ampm;		#sub routine

if ($ampm eq 'PM') {
$leave + 12 };		#add 12 hours to time


print "What time did you leave? (HH:MM) ";
$leave = <STDIN>;
chomp $leave;

#Do REGEX here for valid HH:MM input;

print "AM or PM?  ";	#This should
$ampm = <STDIN>;	#be a
chomp $ampm;		#sub routine

if ($ampm eq 'PM') {
$leave + 12 };		#add 12 hours to time



print "Did you take lunch? (Y or N)  ";
$lunch = <STDIN>;
chomp $lunch;

# Do REGEX here to change case to UPPER and to check for Y or N;

$hours = ($leave - $start);
$hoursl = (($leave - $start) - 1);

if ($lunch eq 'Y') {
  print "You worked L $hoursl\n";
} else {
  print "You worked NL $hours\n";
}

--
Member of the Nondeterministic Football League


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

Date: 14 Mar 2001 11:46:41 -0600
From: Tony Curtis <tony_curtis32@yahoo.com>
Subject: Re: Useless use of addition (+) in a void context...
Message-Id: <87lmq88cqm.fsf@limey.hpcc.uh.edu>

>> On Wed, 14 Mar 2001 12:39:53 -0500,
>> Lou Moran <lmoran@wtsg.com> said:

> I am getting the following error:

> Useless use of addition (+) in a void context (... lines
> affected) ^^^^^^^^^^^^^^ What does this mean?  I haven't
> been able to look it up in perldoc.

It means the code is performing an addition but not
assigning the result anywhere.  Like just writing

    $ perl -cw -e '$x = 1; $y = 2; $x + $y';
    Useless use of addition (+) in void context at -e line 1.
    -e syntax OK

> if ($ampm eq 'PM') { $leave + 12 };

Bingo.  Where you want to store the result of $leave plus
12?

Did you mean

    $leave += 12;

?

hth
t
-- 
Just reach into these holes.  I use a carrot.


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

Date: Wed, 14 Mar 2001 13:10:26 -0500
From: Lou Moran <lmoran@wtsg.com>
Subject: Re: Useless use of addition (+) in a void context...
Message-Id: <picvatsb89o3ekpknhagufe3bvsup7jm71@4ax.com>

On 14 Mar 2001 11:46:41 -0600, Tony Curtis <tony_curtis32@yahoo.com>
wrote wonderful things about sparkplugs:

>
>Did you mean
>
>    $leave += 12;

Probably.  (in as much as it works as expected now)

For my understanding the above takes $leave and adds 12 to it; yes?

(Now I can read about how to manipulate time (or which module to use)
and I am good to go.)

Thanks.

>
>?
>
>hth
>t


--
Member of the Nondeterministic Football League


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

Date: 14 Mar 2001 12:19:21 -0600
From: Tony Curtis <tony_curtis32@yahoo.com>
Subject: Re: Useless use of addition (+) in a void context...
Message-Id: <871ys0jjrq.fsf@limey.hpcc.uh.edu>

>> On Wed, 14 Mar 2001 13:10:26 -0500,
>> Lou Moran <lmoran@wtsg.com> said:

> On 14 Mar 2001 11:46:41 -0600, Tony Curtis
> <tony_curtis32@yahoo.com> wrote wonderful things about
> sparkplugs:

>>  Did you mean
>> 
>> $leave += 12;

> Probably.  (in as much as it works as expected now)

> For my understanding the above takes $leave and adds 12
> to it; yes?

Yes...*and* it stores the result of that addition in
$leave.  It's a shorthand for

    $leave     =    $leave + 12;
    (lvalue)        (rvalue)

What you wrote initially, computed a new value ($leave +
12) and then threw that away.

hth
t
-- 
Just reach into these holes.  I use a carrot.


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

Date: 14 Mar 2001 18:42:18 GMT
From: abigail@foad.org (Abigail)
Subject: Re: Useless use of addition (+) in a void context...
Message-Id: <slrn9avesa.roj.abigail@tsathoggua.rlyeh.net>

Lou Moran (lmoran@wtsg.com) wrote on MMDCCLII September MCMXCIII in
<URL:news:veavatoib1mlh6ul1v5s48ubnpbuhc62d4@4ax.com>:
:) I am trying to write a program to figure out overtime hours in Perl
:) which I have already written in Lotus Notes.
:) 
:) I am getting the following error:
:) 
:) Useless use of addition (+) in a void context (... lines affected)
:) ^^^^^^^^^^^^^^
:) What does this mean?  I haven't been able to look it up in perldoc.


man perldiag


:) if ($ampm eq 'PM') {
:) $leave + 12 };		#add 12 hours to time

Yeah, you add 12 to $leave. But what are you doing with the result?



Abigail
-- 
perl -MLWP::UserAgent -MHTML::TreeBuilder -MHTML::FormatText -wle'print +(
HTML::FormatText -> new -> format (HTML::TreeBuilder -> new -> parse (
LWP::UserAgent -> new -> request (HTTP::Request -> new ("GET",
"http://work.ucsd.edu:5141/cgi-bin/http_webster?isindex=perl")) -> content))
=~ /(.*\))[-\s]+Addition/s) [0]'


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

Date: Wed, 14 Mar 2001 17:59:41 -0000
From: "The NewsBrowser" <nbr@newsbrowser.com>
Subject: Re: Using the 'use' directive, and page reloading in a perl script
Message-Id: <98obid$2tf46$1@ID-18325.news.dfncis.de>

Thanks Galen, I found both your answers very helpful.
--
Akin

email: akin at aksoto dot idps dot co dot uk





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

Date: Wed, 14 Mar 2001 19:59:53 +0100
From: "Roland Mösl" <founder@pege.org>
Subject: win32::shell::execute problem
Message-Id: <3aafbf76$0$21958@SSP1NO25.highway.telekom.at>

I develop HTA Hyper Text Applications with Perl
as scripting language.

Win32::Shell::Execute( "open", "E:\\internet\\manual\\manuals\\inet.chm",
"", "E:\\internet\\manual\\manuals\\", "SW_SHOWMAXIMIZED" );

does not work.

The inet.chm icon appears when I press ALT TAB to view
the task list icons.

Hh appears in the task least each time I call this perl line.

But it does not show up.

Exactly the same Win32::Shell::Execute with an htm or hta file works fine
also with msg to open an email stored as a file works fine.

But no other Application works. Only the Explorer and Outlook Express works
with this win32::Shell::Execute

Any ideas, alternatives ?


--
Roland Mösl
http://pege.org Clear targets for a confused civilization
http://BeingFound.com Web Design starts at the search engine




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

Date: Wed, 14 Mar 2001 19:36:22 +0000
From: =?iso-8859-1?Q?Fran=E7ois?= Breuiller <francois@idylic.com>
Subject: XML::Parser
Message-Id: <3AAFC836.5CD70CC@idylic.com>

Hello,

I want to parse xml document and keep back all information in perl
structure such like hash. I use for this XML::Parser but I don't success
to access to the different nodes, attributes and datas ?

Could someone help me and give some sample code to do that !

Thanks

François



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

Date: 16 Sep 99 21:33:47 GMT (Last modified)
From: Perl-Users-Request@ruby.oce.orst.edu (Perl-Users-Digest Admin) 
Subject: Digest Administrivia (Last modified: 16 Sep 99)
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: The mail to news gateway, and thus the ability to submit articles
| through this service to the newsgroup, has been removed. I do not have
| time to individually vet each article to make sure that someone isn't
| abusing the service, and I no longer have any desire to waste my time
| dealing with the campus admins when some fool complains to them about an
| article that has come through the gateway instead of complaining
| to the source.

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


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