[25390] in Perl-Users-Digest

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

Perl-Users Digest, Issue: 7635 Volume: 10

daemon@ATHENA.MIT.EDU (Perl-Users Digest)
Wed Jan 12 11:10:37 2005

Date: Wed, 12 Jan 2005 08:10:29 -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           Wed, 12 Jan 2005     Volume: 10 Number: 7635

Today's topics:
        Problem splitting lines from file input <Mothra@mothra.com>
    Re: Problem splitting lines from file input <do-not-use@invalid.net>
    Re: Problem splitting lines from file input <Mothra@mothra.com>
    Re: requires explicit package name <Mothra@mothra.com>
    Re: requires explicit package name <tadmc@augustmail.com>
    Re: stat() problem <bik.mido@tiscalinet.it>
        utf-8, was Re: Three questions: UTF-8, DBM, hash of lis <flavell@ph.gla.ac.uk>
        Waiting for non-child processes <zen8533@zen.co.uk>
    Re: Waiting for non-child processes (Anno Siegel)
    Re: Waiting for non-child processes <do-not-use@invalid.net>
    Re: Waiting for non-child processes (Anno Siegel)
    Re: Waiting for non-child processes <zen8533@zen.co.uk>
        Digest Administrivia (Last modified: 6 Apr 01) (Perl-Users-Digest Admin)

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

Date: Wed, 12 Jan 2005 11:14:14 GMT
From: Mothra <Mothra@mothra.com>
Subject: Problem splitting lines from file input
Message-Id: <aE7Fd.624600$2W1.51532@news.easynews.com>

I'm trying to parse a log file using ';' as a newline and then wherever 
I find items inside '(..)', indenting those lines thus:


foo;bar(foo;bar(foobar);foo)foobar

  becomes

foo
bar(
    foo
    bar(
        foobar
    )
    foo
)
foobar

What I've got so far is below - the problem is that when there are 
nested sets of parentheses, I can't increase the indents accordingly. 
Also, I can't work out how to get the closing ')' on a line by itself.

I'm in a bit of a pickle - can anyone help?

What I've got so far follows:


#!/usr/bin/perl -w
use strict;
$|=0;

my @in = <>;
my $tbchr = "\t";
my $tbcnt = 0;

for(@in){
        my @ln=split(';', $_);
        my $i;
        for ($i=0; $i<@ln; $i++){
                chomp($ln[$i]);
                my @subln;
                if ( $ln[$i] =~ /\([^)]/ ) {
                        @subln = split('\(',$ln[$i]);   
                        my $k;for($k=0;$k<@subln;$k++){ $subln[$k] .= 
'(' }
                } elsif ( $ln[$i] =~ /[^(]\)/ ) {
                        @subln = split('\)',$ln[$i]);   
                        my $k;for($k=0;$k<@subln;$k++){ $subln[$k] .= 
')' }
                } else {
                        @subln = $ln[$i];       
                }


                for(@subln){
                        my $j;
                        for($j=0; $j<$tbcnt; $j++){ print $tbchr }
                        print "$_\n";
                        $tbcnt++ if /\(/;
                        $tbcnt-- if /\)/;
                }

        }
}


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

Date: 12 Jan 2005 12:44:32 +0100
From: Arndt Jonasson <do-not-use@invalid.net>
Subject: Re: Problem splitting lines from file input
Message-Id: <yzdhdlmoorj.fsf@invalid.net>


Mothra <Mothra@mothra.com> writes:
> I'm trying to parse a log file using ';' as a newline and then wherever 
> I find items inside '(..)', indenting those lines thus:
> 
> 
> foo;bar(foo;bar(foobar);foo)foobar
> 
>   becomes
> 
> foo
> bar(
>     foo
>     bar(
>         foobar
>     )
>     foo
> )
> foobar
> 
> What I've got so far is below - the problem is that when there are 
> nested sets of parentheses, I can't increase the indents accordingly. 
> Also, I can't work out how to get the closing ')' on a line by itself.

I found your code far too complicated to try to understand, so I wrote
this version instead (not meant to be particularly clever or neat - I'm
sure there are things to improve in it):

    #! /usr/local/bin/perl

    use strict;
    use warnings;

    my $level = 0;

    sub newline {
        print "\n", " " x (4*$level);
    }

    while (<>) {
        for (split //) {
            if ($_ eq ";") {
                newline();
            } elsif ($_ eq "(") {
                print $_;
                $level++;
                newline();
            } elsif ($_ eq ")") {
                $level--;
                newline();
                print $_;
                newline();
            } else {
                print $_;
            }
        }
    }

    print "\n";

I didn't address the requirement of printing ')' on their own lines, but
you can probably modify my program to do so.

I think there's an ambiguity in your input/output data: you have ");"
in one place and just ")" in another, but in the output, both ')'
characters are followed by precisely one newline.


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

Date: Wed, 12 Jan 2005 11:57:58 GMT
From: Mothra <Mothra@mothra.com>
Subject: Re: Problem splitting lines from file input
Message-Id: <ah8Fd.625827$O24.94338@news.easynews.com>

Arndt Jonasson <do-not-use@invalid.net> wrote in 
news:yzdhdlmoorj.fsf@invalid.net:

> 
> I found your code far too complicated to try to understand, so I wrote
> this version instead (not meant to be particularly clever or neat - I'm
> sure there are things to improve in it):
>

My original script simply split lines on ';'.  I was then asked to modify 
it to do the stuff with parentheses, hence it got a bit ugly! :-)


> 
> I didn't address the requirement of printing ')' on their own lines, but
> you can probably modify my program to do so.
> 
Thanks - the structure of your script is a lot better than mine.

> I think there's an ambiguity in your input/output data: you have ");"
> in one place and just ")" in another, but in the output, both ')'
> characters are followed by precisely one newline.
> 
Sorry about that - the real data I'm parsing is customer names, adresses, 
bank details etc., so I couldn't post that.


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

Date: Wed, 12 Jan 2005 11:39:10 GMT
From: Mothra <Mothra@mothra.com>
Subject: Re: requires explicit package name
Message-Id: <y%7Fd.624829$O24.94354@news.easynews.com>

sam <sam.wun@authtec.com> wrote in news:crj0rc$15p0$1@news.hgc.com.hk:

> use strict;
> use warnings;
> 
> local %hash1 = ('sales_subtotal'=>100);
> local  %hash2 = ('sales_subtotal'=>150);
> 
> 
> When I execute the above code, I got the following error:
> Global symbol "%hash1" requires explicit package name at hashs.pl line 6.
> Global symbol "%hash2" requires explicit package name at hashs.pl line 7.
> 
use 'my' to scope a variable:

my %hash1 = ('sales_subtotal'=>100);
my %hash2 = ('sales_subtotal'=>150);


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

Date: Wed, 12 Jan 2005 09:07:40 -0600
From: Tad McClellan <tadmc@augustmail.com>
Subject: Re: requires explicit package name
Message-Id: <slrncuaf9s.dd8.tadmc@magna.augustmail.com>

sam <sam.wun@authtec.com> wrote:

> I don;t know what is wrong with the following simple perl code:


See the documentation for the strict pragma. You are
running afoul of the "strict vars" part:

   perldoc strict


> use strict;


Here you promise to declare all of your variables, or to use the
full package name of the variable...


> local %hash1 = ('sales_subtotal'=>100);


 ... and here you use a variable doing neither. 

You have broken your promise, perl refuses to run your program.


Why are you using package variables instead of lexical variables?

If you can use lexical variables, then you declare them like this:

   my %hash1 = ('sales_subtotal'=>100);

If you really do need package variables (unlikely) then you declare
them like this:

   our %hash1 = ('sales_subtotal'=>100);

or, you use its full name:

   %main::hash1 = ('sales_subtotal'=>100);


> When I execute the above code, I got the following error:
> Global symbol "%hash1" requires explicit package name at hashs.pl line 6.


All of the messages that perl might issue are documented in

   perldoc perldiag

You should look up messages there as a *first* step in troubleshooting:

   =item Global symbol "%s" requires explicit package name

   (F) You've said "use strict vars", which indicates that all variables
   must either be lexically scoped (using "my"), declared beforehand using
   "our", or explicitly qualified to say which package the global variable
   is in (using "::").


If you don't know what this "package" vs. "lexical" thing is,
then please see:

   "Coping with Scoping":

      http://perl.plover.com/FAQs/Namespaces.html


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


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

Date: Wed, 12 Jan 2005 16:20:19 +0100
From: Michele Dondi <bik.mido@tiscalinet.it>
Subject: Re: stat() problem
Message-Id: <nddau058attnlt8tildns3bb59seiv33gl@4ax.com>

On Mon, 10 Jan 2005 10:27:02 +0100, Petterson Mikael
<mikael.petterson@ericsson.se> wrote:

>Hi,
>
>I am trying to get change time of a directory with the following perl 
>snippet. @allfiles are containing the directory names.
>
>foreach (@allfiles) {
>   if ( -d "$dir/$_"){
>     print "$dir/$_\n";

as a side note, I'd do

  #!/usr/bin/perl -l
  #               ^^
  #               ^^
  # or set $\ local()ly later,
  # according to how big your script really is.
  # ...
  
  use strict;
  use warnings;

  # You always include these lines, don't you?
  # If you don't... do!
  
  for (map "$dir/$_", @allfiles) {
      next unless -d;
      print;
      print +(stat)[9];
  }

  # So that I can take advantage of $_ being the topicalizer...  

>I get the following error message:
>
>Use of uninitialized value in string at test.pl line 17

Said this, you shouldn't get this error, so I guess it stems from
something else you didn't include in your post. Can you prepare a
full, self-contained, but still minimal example script exhibiting the
problem?


Michele
-- 
{$_=pack'B8'x25,unpack'A8'x32,$a^=sub{pop^pop}->(map substr
(($a||=join'',map--$|x$_,(unpack'w',unpack'u','G^<R<Y]*YB='
 .'KYU;*EVH[.FHF2W+#"\Z*5TI/ER<Z`S(G.DZZ9OX0Z')=~/./g)x2,$_,
256),7,249);s/[^\w,]/ /g;$ \=/^J/?$/:"\r";print,redo}#JAPH,


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

Date: Wed, 12 Jan 2005 11:48:36 +0000
From: "Alan J. Flavell" <flavell@ph.gla.ac.uk>
Subject: utf-8, was Re: Three questions: UTF-8, DBM, hash of lists, ...
Message-Id: <Pine.LNX.4.61.0501121107560.17649@ppepc56.ph.gla.ac.uk>

On Tue, 11 Jan 2005, Wes Groleau wrote:

> Three questions

There are no special awards for folding several questions into one 
posting.  All that it achieves is: several unrelated subthreads 
hanging-off the original posting.  Confusion all round.

The key to effective problem-solving is to break up a complex problem 
into manageable parts, and deal with each separately, until one 
understands it well enough to use it at a component of the whole.  In 
that sense, I'd commend to you the strategy of asking detailed 
questions one at a time (with enough context for the group to 
understand the detailed question).  If, on the other hand, you can't 
decide how to partition a complex problem, then ask about the problem 
itself, at a higher level, without pre-judging the lower-level 
implementation detail. IMHO and YMMV, anyway.

> I've been rooting around in perlutf8, perlencoding, perlunicode,
> and other such things.  I think I follow most of it, but there
> are some contradictions.  Or I thought there were.
> 
> 1. At the moment, my source is pure ASCII, but I want to
>    treat it as UTF-8 because the text I work with is UTF-8
>    and my editor is configured accordingly. 

Please distinguish carefully between your program source and your 
data.

As a matter of fact, us-ascii -is- a subset of utf-8 - utf-8 was 
deliberately designed that way - but you *don't* have to use utf-8 
encoding in your program source in order to process unicode data.

In any case, Perl's unicode implementation is supposed to be 
transparent, i.e you shouldn't normally need to know that its internal 
representation happens to be utf-8.  What you /do/ need to know is 
what encoding is used in your /external data/, and to tell Perl about 
it at the appropriate time (e.g by an encoding layer on an I/O 
statement).

> (And data can easily become literals in source). 

In many situations, you might be better advised to write unicode 
characters into the source by means of their \x{..} representation.  
Which is not to deny that there can also be situations where you'd 
want to write unicode characters directly - but then you have to be a 
lot more careful with how you edit and transfer your source code.
See 
http://www.perldoc.com/perl5.8.4/pod/perlunicode.html#Effects-of-Character-Semantics
for more details.

> I put -CSD on
>    my bang-line, which one man page said covers everything
>    (except -CL which I did not want for some reason).

Could we have a cite on that? 

-C is a request to use wide system calls.  It doesn't influence Perl's 
interpretation of your program source or data "as such".

>  But
>    another man page seemed to say that "use utf8;" covered
>    something that -CSD did not, so I put that in, too. 

The perlunicode pod, for the version of Perl that you're using, should 
be your "bible".  Don't go tossing-in arbitrary bits and pieces that 
you may have acquired from elsewhere - treat them as possibly 
misleading clues, but check with the authoritative documentation to 
make sure that they really do what you want.

See what 
http://www.perldoc.com/perl5.8.4/pod/perlunicode.html#Important-Caveats
says about "use utf8;".

> Is either one interfering with the other in any way?

I don't know of any reason why they should.

good luck


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

Date: Wed, 12 Jan 2005 14:06:57 -0000
From: "Ben Veal" <zen8533@zen.co.uk>
Subject: Waiting for non-child processes
Message-Id: <41e52f01$0$31433$db0fefd9@news.zen.co.uk>

Can anyone tell me how I can wait for a non-child process to complete?
(waitpid doesn't work).

Thanks.
__________________________________________________________________ Ben Veal
ICQ#: 152274124 Current ICQ status: + More ways to contact me
__________________________________________________________________




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

Date: 12 Jan 2005 15:11:57 GMT
From: anno4000@lublin.zrz.tu-berlin.de (Anno Siegel)
Subject: Re: Waiting for non-child processes
Message-Id: <cs3ent$m46$1@mamenchi.zrz.TU-Berlin.DE>

Ben Veal <zen8533@zen.co.uk> wrote in comp.lang.perl.misc:
> Can anyone tell me how I can wait for a non-child process to complete?
> (waitpid doesn't work).

That's an OS question, not a Perl question.  It can't be reasonably
answered without more information.

What OS are you running?  Assuming something Unix-ish: What kind of process
are we talking about?  Is it yours (running as the same user as the process
you want to wait for)?  Do you control what it does, or is it any random
process?  How do you identify it in the first place?

Anno


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

Date: 12 Jan 2005 16:34:17 +0100
From: Arndt Jonasson <do-not-use@invalid.net>
Subject: Re: Waiting for non-child processes
Message-Id: <yzd6522oe4m.fsf@invalid.net>


"Ben Veal" <zen8533@zen.co.uk> writes:
> Can anyone tell me how I can wait for a non-child process to complete?
> (waitpid doesn't work).

If the OS is Unix, you can use 'kill' with the 0 signal to find out
whether a process exists, given its PID. But I think that if the parent
of the process hasn't waited for it (so it is a zombie), it will be
shown to still exist, although for all practical purposes it has finished.
That may or may not be a problem in your application.


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

Date: 12 Jan 2005 15:46:52 GMT
From: anno4000@lublin.zrz.tu-berlin.de (Anno Siegel)
Subject: Re: Waiting for non-child processes
Message-Id: <cs3gpc$o14$1@mamenchi.zrz.TU-Berlin.DE>

Arndt Jonasson  <do-not-use@invalid.net> wrote in comp.lang.perl.misc:
> 
> "Ben Veal" <zen8533@zen.co.uk> writes:
> > Can anyone tell me how I can wait for a non-child process to complete?
> > (waitpid doesn't work).
> 
> If the OS is Unix, you can use 'kill' with the 0 signal to find out
> whether a process exists, given its PID.

 ...provided you are allowed to signal the other process at all.  You
must be the same user or superuser for that.

Anno


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

Date: Wed, 12 Jan 2005 16:03:14 -0000
From: "Ben Veal" <zen8533@zen.co.uk>
Subject: Re: Waiting for non-child processes
Message-Id: <41e54a43$0$31424$db0fefd9@news.zen.co.uk>


"Anno Siegel" <anno4000@lublin.zrz.tu-berlin.de> wrote in message
news:cs3ent$m46$1@mamenchi.zrz.TU-Berlin.DE...
> Ben Veal <zen8533@zen.co.uk> wrote in comp.lang.perl.misc:
> > Can anyone tell me how I can wait for a non-child process to complete?
> > (waitpid doesn't work).
>
> That's an OS question, not a Perl question.  It can't be reasonably
> answered without more information.
>
> What OS are you running?  Assuming something Unix-ish: What kind of
process
> are we talking about?  Is it yours (running as the same user as the
process
> you want to wait for)?  Do you control what it does, or is it any random
> process?  How do you identify it in the first place?
>
> Anno

Sorry, should have given more info.
I'm a newbie so I don't know much about the workings of operating systems,
but I'll try.
It's running on Linux 2.4.20-20.7.
I've written a perl script that runs a specific sequence of processes with
the input arguments of the script.
I want to do something when they've completed (they typically take about
30mins).
I am using system() in perl to run the commands, and it returns immediately.
Also using waitpid in perl with their pid's reports that they've completed,
even though I know that they're still running.
The processes consists of a server type process & clients which connect via
a port. When I run them as normal from the shell I am immediately returned
to the shell prompt (and can use it as normal) even though they are still
running.
The processes show up with ps as my processes, and I can get their pid's and
kill them, but I can't do fg on them.

Ben.




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

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


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