[21873] in Perl-Users-Digest

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

Perl-Users Digest, Issue: 4077 Volume: 10

daemon@ATHENA.MIT.EDU (Perl-Users Digest)
Thu Nov 7 00:11:38 2002

Date: Wed, 6 Nov 2002 21:10:18 -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, 6 Nov 2002     Volume: 10 Number: 4077

Today's topics:
    Re: Script is skipping chapters... <krahnj@acm.org>
        setuid script <sheken@videotron.ca>
    Re: setuid script (Sam Holden)
    Re: setuid script <paladin@techmonkeys.org>
    Re: setuid script <sheken@videotron.ca>
    Re: setuid script <dmeans@the-means.net>
    Re: setuid script (Sam Holden)
    Re: setuid script (Sam Holden)
    Re: setuid script <sheken@videotron.ca>
    Re: setuid script (Sam Holden)
    Re: setuid script <sheken@videotron.ca>
        sh (bash) $1 equivalent <seppanen@chartermi.net>
    Re: sh (bash) $1 equivalent <goldbb2@earthlink.net>
    Re: sh (bash) $1 equivalent <seppanen@chartermi.net>
    Re: sh (bash) $1 equivalent (Sam Holden)
    Re: sh (bash) $1 equivalent <dmeans@the-means.net>
    Re: sh (bash) $1 equivalent <dmeans@the-means.net>
    Re: Simple question - What is unicode? <goldbb2@earthlink.net>
    Re: Telnet to OpenVMS, run cobol program - not working. <bwalton@rochester.rr.com>
    Re: Tie'd filehandle and the system() function (Tim)
    Re: Tie'd filehandle and the system() function <goldbb2@earthlink.net>
    Re: Unix command <jurgenex@hotmail.com>
        WebCalendar, Registry.pm and @INC problem <kaushik@mho.com>
    Re: WebCalendar, Registry.pm and @INC problem <dmeans@the-means.net>
    Re: Win32 and unicode <goldbb2@earthlink.net>
    Re: Win32 and unicode (Jay Tilton)
        Digest Administrivia (Last modified: 6 Apr 01) (Perl-Users-Digest Admin)

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

Date: Wed, 06 Nov 2002 23:54:35 GMT
From: "John W. Krahn" <krahnj@acm.org>
Subject: Re: Script is skipping chapters...
Message-Id: <3DC9AB94.F9E84470@acm.org>

Andrew Burton wrote:
> 
> I'm stumped! I mean, I am really stumped...  I wrote a script to convert a text
> file into an HTML file -- change breaks to the <br> tags and such -- and half
> of it works.  What half you ask...  The even half.  I have a regular expression
> that looks for "Chapter " ("Chapter" and a single space) in a line and then
> makes that line bold.  It works too, but it skips the odd chapters.  I have run
> a grep for "Chapter 77" (on my FreeBSD machine) and Find (on my Windows2000)
> macine, so I know these chapters exist, and these chapter headers are even
> found when looked for.  My script just misses them.  The file is 2.8MB, but I
> thought Perl could handle that.  As said, I have run this on Windows2000
> (ActivePerl 5.6.1) and FreeBSD 4.0 (Perl 5.8).  For whatever it's worth, I'm
> trying to convert the text file of "Cryptonomicon" from textz.org -- in case
> someone knows of tricks they use.  Any help would be appreciated.  Thanks.
> 
> ---
> 
> use strict;

You should enable warnings as well.

use warnings;


> my $file = @ARGV[0];
             ^^^^^^^^
Warnings would have caught this.


> my $line;
> 
> if ($file =~ m/\\/) {
>         my @splfile = split (/\\/, $file);
>         my $splfile = @splfile;
>         $file = $splfile[$splfile-1];
> }

A lot of work to do this:

$file =~ s/.*\\//;


> open ("ain", "@ARGV[0]");
                ^^^^^^^^
Warnings would have caught this.


> open ("aout", ">$file\.html");

You should _always_ verify that the files opened correctly.  While
putting filehandles in quotes will work is there some reason you are
doing it this way?

open ain,    $ARGV[0]    or die "Cannot open $ARGV[0]: $!";
open aout, ">$file.html" or die "Cannot open $file.html: $!";


> print aout "<html><head><title> $file </title></head><body>\n";
> 
> while (<ain>) {
>         $line = <ain>;
>         $line =~ s/\n//g;

Andreas has pointed this out already.

while ( my $line = <ain> ) {
        chomp $line;


>         if (($line =~ m/Chapter /) || ($line =~ m/Prologue/)){
>                 print aout "<b>$line</b><br><br>\n";
>                 print "$line\r\n";
>         }
>         elsif ($line ne "") {
>                 print aout "$line<br><br>\n";
>         }
> }
> 
> print aout "</body></html>";
> 
> close ("aout");
> close ("ain");
> 
> print "$file has been converted.\n";
> 
> exit;



John
-- 
use Perl;
program
fulfillment


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

Date: Wed, 6 Nov 2002 20:06:48 -0500
From: "George" <sheken@videotron.ca>
Subject: setuid script
Message-Id: <T0jy9.55162$T_3.665311@wagner.videotron.net>

Hi,

I need to run a perl script setuid root but I am not sure how to go about
doing that.
My script is not receiving any data so it doesn't need to untaint anything.
The owner of my script is root, and I have set the +s flag. It doesn't want
to run as root though.

What can I do in order to get it to run as root ?






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

Date: 7 Nov 2002 02:26:16 GMT
From: sholden@flexal.cs.usyd.edu.au (Sam Holden)
Subject: Re: setuid script
Message-Id: <slrnasjjq8.sb3.sholden@flexal.cs.usyd.edu.au>

On Wed, 6 Nov 2002 20:06:48 -0500, George <sheken@videotron.ca> wrote:
> Hi,
> 
> I need to run a perl script setuid root but I am not sure how to go about
> doing that.
> My script is not receiving any data so it doesn't need to untaint anything.
> The owner of my script is root, and I have set the +s flag. It doesn't want
> to run as root though.
> 
> What can I do in order to get it to run as root ?

Depends what you mean by "doesn't want to". 

The 'perlsec' documentation explains a few things which can cause
problems at both the perl and OS/system level. It also provides solutions.

"perldoc perlsec" explains it better than I can, so I'll let you read it.

On some system an almost one-line C program is required to exec the perl
script, but that is also explained in the perlsec docs.

-- 
Sam Holden



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

Date: Thu, 07 Nov 2002 02:45:24 GMT
From: Alan Cameron <paladin@techmonkeys.org>
Subject: Re: setuid script
Message-Id: <3ojcqa.in2.ln@paladin>

On Wed, 6 Nov 2002 20:06:48 -0500, George <sheken@videotron.ca> wrote:

> I need to run a perl script setuid root but I am not sure how to go about
> doing that.

[snip]

> What can I do in order to get it to run as root ?

perldoc perlsec has a good discussion on some of the problems you may
encounter in running a script with the setuid bit set.

-- 
Mother is the name for GOD on the lips and
hearts of all children.  - Eric Draven


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

Date: Wed, 6 Nov 2002 21:48:15 -0500
From: "George" <sheken@videotron.ca>
Subject: Re: setuid script
Message-Id: <_vky9.55477$T_3.714368@wagner.videotron.net>

the one line C wrapper works for setuid as non-root users
C wrapper does not work as root

I also read the perlsec document on perldoc.com and it just explains about
taint and untaint which is of no use to me

"Sam Holden" <sholden@flexal.cs.usyd.edu.au> a écrit dans le message news:
slrnasjjq8.sb3.sholden@flexal.cs.usyd.edu.au...
> On Wed, 6 Nov 2002 20:06:48 -0500, George <sheken@videotron.ca> wrote:
> > Hi,
> >
> > I need to run a perl script setuid root but I am not sure how to go
about
> > doing that.
> > My script is not receiving any data so it doesn't need to untaint
anything.
> > The owner of my script is root, and I have set the +s flag. It doesn't
want
> > to run as root though.
> >
> > What can I do in order to get it to run as root ?
>
> Depends what you mean by "doesn't want to".
>
> The 'perlsec' documentation explains a few things which can cause
> problems at both the perl and OS/system level. It also provides solutions.
>
> "perldoc perlsec" explains it better than I can, so I'll let you read it.
>
> On some system an almost one-line C program is required to exec the perl
> script, but that is also explained in the perlsec docs.
>
> --
> Sam Holden
>




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

Date: Wed, 06 Nov 2002 21:58:35 -0500
From: David Means <dmeans@the-means.net>
Subject: Re: setuid script
Message-Id: <pan.2002.11.07.02.57.48.217328.6681@the-means.net>

On Wed, 06 Nov 2002 21:26:16 -0500, Sam Holden wrote:

> On Wed, 6 Nov 2002 20:06:48 -0500, George <sheken@videotron.ca> wrote:
>> Hi,
>> 
>> I need to run a perl script setuid root but I am not sure how to go
>> about doing that.
>> My script is not receiving any data so it doesn't need to untaint
>> anything. The owner of my script is root, and I have set the +s flag.
>> It doesn't want to run as root though.
>> 
>> What can I do in order to get it to run as root ?
> 
> Depends what you mean by "doesn't want to".
> 
> The 'perlsec' documentation explains a few things which can cause
> problems at both the perl and OS/system level. It also provides
> solutions.
> 
> "perldoc perlsec" explains it better than I can, so I'll let you read
> it.
> 
> On some system an almost one-line C program is required to exec the perl
> script, but that is also explained in the perlsec docs.

Some *nix type systems will not allow a script to run setuid root.  Doing
so can lead to compromise of a system.

http://phobos.illtel.denver.co.us/pub/cert/clippings/910801-01


-- 
David Means

#define QUESTION ( (bb) || !(bb) )



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

Date: 7 Nov 2002 03:37:04 GMT
From: sholden@flexal.cs.usyd.edu.au (Sam Holden)
Subject: Re: setuid script
Message-Id: <slrnasjnv0.stl.sholden@flexal.cs.usyd.edu.au>

On Wed, 6 Nov 2002 21:48:15 -0500, George <sheken@videotron.ca> wrote:
> the one line C wrapper works for setuid as non-root users
> C wrapper does not work as root

If a C program can't be setuid root on this strange system of yours (how
the heck does it implement su???) then perl has no chance - since perl
is after all a C program :)

If you mean perl bails then read that perlsec page again, and see why...

-- 
Sam Holden



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

Date: 7 Nov 2002 03:38:34 GMT
From: sholden@flexal.cs.usyd.edu.au (Sam Holden)
Subject: Re: setuid script
Message-Id: <slrnasjo1q.stl.sholden@flexal.cs.usyd.edu.au>

On Wed, 06 Nov 2002 21:58:35 -0500, David Means <dmeans@the-means.net> wrote:
> On Wed, 06 Nov 2002 21:26:16 -0500, Sam Holden wrote:
> 
>> 
>> On some system an almost one-line C program is required to exec the perl
>> script, but that is also explained in the perlsec docs.
> 
> Some *nix type systems will not allow a script to run setuid root.  Doing
> so can lead to compromise of a system.

Hence the need for that C wrapper... Why else would you think I would
mention it?

-- 
Sam Holden



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

Date: Wed, 6 Nov 2002 23:23:42 -0500
From: "George" <sheken@videotron.ca>
Subject: Re: setuid script
Message-Id: <iVly9.24881$7e4.612110@weber.videotron.net>

Sam,

I use this as C wrapper

#include <stdlib.h>
void main() {
    system("/home/pulsar/public_html/cgi-bin/test.pl");
    }

compiled, owned by root and with +s

script still won't run as root

anything I am missing ?

"Sam Holden" <sholden@flexal.cs.usyd.edu.au> wrote in message
news:slrnasjo1q.stl.sholden@flexal.cs.usyd.edu.au...
> On Wed, 06 Nov 2002 21:58:35 -0500, David Means <dmeans@the-means.net>
wrote:
> > On Wed, 06 Nov 2002 21:26:16 -0500, Sam Holden wrote:
> >
> >>
> >> On some system an almost one-line C program is required to exec the
perl
> >> script, but that is also explained in the perlsec docs.
> >
> > Some *nix type systems will not allow a script to run setuid root.
Doing
> > so can lead to compromise of a system.
>
> Hence the need for that C wrapper... Why else would you think I would
> mention it?
>
> --
> Sam Holden
>




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

Date: 7 Nov 2002 04:37:43 GMT
From: sholden@flexal.cs.usyd.edu.au (Sam Holden)
Subject: Re: setuid script
Message-Id: <slrnasjrgl.t9p.sholden@flexal.cs.usyd.edu.au>

On Wed, 6 Nov 2002 23:23:42 -0500, George <sheken@videotron.ca> wrote:
> Sam,
> 
> I use this as C wrapper
> 
> #include <stdlib.h>
> void main() {
>     system("/home/pulsar/public_html/cgi-bin/test.pl");
>     }
> 
> compiled, owned by root and with +s
> 
> script still won't run as root

Of course not, since that effectively does nothing.

> 
> anything I am missing ?

READ THE DOCUMENTATION!!!!

perlsec has the C program you should use in it.

It is not the same as your one above.

Instead of guessing READ THE DOCUMENTATION!

and yes I meant to yell.

-- 
Sam Holden



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

Date: Thu, 7 Nov 2002 00:00:38 -0500
From: "George" <sheken@videotron.ca>
Subject: Re: setuid script
Message-Id: <Vrmy9.26967$7e4.626472@weber.videotron.net>

I'm running freebsd 4.5

basically I have a c wrapper, setuid root that runs my script. My script
just writes a text file into root's home directory
I login as a normal user and I run the wrapper.
script executes normally, I have the output to screen but the file never
gets written. No permission denied error either.

I'm not sure where it gets blocked.

"Sam Holden" <sholden@flexal.cs.usyd.edu.au> wrote in message
news:slrnasjnv0.stl.sholden@flexal.cs.usyd.edu.au...
> On Wed, 6 Nov 2002 21:48:15 -0500, George <sheken@videotron.ca> wrote:
> > the one line C wrapper works for setuid as non-root users
> > C wrapper does not work as root
>
> If a C program can't be setuid root on this strange system of yours (how
> the heck does it implement su???) then perl has no chance - since perl
> is after all a C program :)
>
> If you mean perl bails then read that perlsec page again, and see why...
>
> --
> Sam Holden
>




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

Date: Wed, 06 Nov 2002 19:49:45 -0500
From: Brian Seppanen <seppanen@chartermi.net>
Subject: sh (bash) $1 equivalent
Message-Id: <3DC9B8A9.5030009@chartermi.net>

I'm trying to use perl to handle some snmptrap data.   One of the code 
examples I have is using bash and the $1 variable is used to pass some 
data to a handler script and is used as follows.

#!/bin/sh
read host
read ip
vars=

while read oid val
do
	if [ "$vars" = "" ]
	then
		vars="$oid = $val"
	else vars="$vars, $oid = $val"
	fi
done

echo trap: $1 $host $ip $vars


This scipt is called traptest, and would be called as 
/usr/local/bin/traptest linkDown from snmptrapd as follows

traphandle IF-MIB::linkDown  /usr/local/bin/traptest linkDown

Bash uses $1 to capture linkDown, which I'm trying to use to evaluate 
for specific actions.

Now with snmptraps most of the data is coming in as standard input.   So 
my attempt at a perl script does the following.   At this time, I'm just 
trying to determine what's input, and move onto parsing it further later.

open (DUMP,">>traps");
my @trapdump=<STDIN>;
$host=$trapdump[0];
$ipaddress=$trapdump[1];
print DUMP "HOSTNAME: $host\n";
print DUMP "IP ADDRESS: $ipaddress\n";



chomp ($host, $ipaddress, $traptype);
for (my $i=2; $i<$#trapdump; $i++) {
         ($trapoid,$result) = split(/\s/,$trapdump[$i]);
         chomp $result;
         print DUMP "\tOID: $trapoid\n";
         print DUMP "\tRESULT: $result\n";

}

close DUMP;

At this point I'm just trying to figure out how to capture the data that 
bash captures with $1.   I've tried using $ARGV[0], but this doesn't 
appear to work.   I'm sort of confused how something could be passing 
both an argument and standard input.

Any insights...?

-- 
Brian E. Seppanen
seppanen@chartermi.net



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

Date: Wed, 06 Nov 2002 20:28:43 -0500
From: Benjamin Goldberg <goldbb2@earthlink.net>
Subject: Re: sh (bash) $1 equivalent
Message-Id: <3DC9C1CB.87169E51@earthlink.net>

Brian Seppanen wrote:
> 
> I'm trying to use perl to handle some snmptrap data.   One of the code
> examples I have is using bash and the $1 variable is used to pass some
> data to a handler script and is used as follows.
[snip]

To learn about perl's special variables, including the ones that are
used to pass arguments from the command line to a script, read the
perlvar document that comes with perl.  To do this, run the command:

   perldoc perlvar

-- 
my $n = 2; print +(split //, 'e,4c3H r ktulrnsJ2tPaeh'
 ."\n1oa! er")[map $n = ($n * 24 + 30) % 31, (42) x 26]


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

Date: Wed, 06 Nov 2002 21:11:31 -0500
From: Brian Seppanen <seppanen@chartermi.net>
Subject: Re: sh (bash) $1 equivalent
Message-Id: <3DC9CBD3.8080601@chartermi.net>

Is there an equivalent in perl, which I may have missed in my reading of 
the documentation?   The documentation for perl indicates that $1 would 
be the first regular expression match.   So I am left to assume that 
perl cannot do this, but I was wondering if someone knew better.

Benjamin Goldberg wrote:
> Brian Seppanen wrote:
> 
>>I'm trying to use perl to handle some snmptrap data.   One of the code
>>examples I have is using bash and the $1 variable is used to pass some
>>data to a handler script and is used as follows.
> 
> [snip]
> 
> To learn about perl's special variables, including the ones that are
> used to pass arguments from the command line to a script, read the
> perlvar document that comes with perl.  To do this, run the command:
> 
>    perldoc perlvar
> 


-- 
Brian E. Seppanen
seppanen@chartermi.net



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

Date: 7 Nov 2002 02:35:30 GMT
From: sholden@flexal.cs.usyd.edu.au (Sam Holden)
Subject: Re: sh (bash) $1 equivalent
Message-Id: <slrnasjkbi.sb3.sholden@flexal.cs.usyd.edu.au>

On Wed, 06 Nov 2002 21:11:31 -0500,
	Brian Seppanen <seppanen@chartermi.net> wrote:
> Is there an equivalent in perl, which I may have missed in my reading of 
> the documentation?   The documentation for perl indicates that $1 would 
> be the first regular expression match.   So I am left to assume that 
> perl cannot do this, but I was wondering if someone knew better.

Read the bit about @ARGV again.

< snip jeopardy quoted message, about accessing an equivalent to the
  shell's $1 >
-- 
Sam Holden



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

Date: Wed, 06 Nov 2002 22:11:44 -0500
From: David Means <dmeans@the-means.net>
Subject: Re: sh (bash) $1 equivalent
Message-Id: <pan.2002.11.07.03.11.42.546015.6681@the-means.net>

On Wed, 06 Nov 2002 21:11:31 -0500, Brian Seppanen wrote:

> Is there an equivalent in perl, which I may have missed in my reading of
> the documentation?   The documentation for perl indicates that $1 would
> be the first regular expression match.   So I am left to assume that
> perl cannot do this, but I was wondering if someone knew better.
> 
{ snip } 
>> run the command:
>> 
>>    perldoc perlvar
>>
>>

local($a,$b,$c,$d) = @ARGV;
print ( "a = [$a]\n");
print ( "b = [$b]\n");
print ( "c = [$c]\n");
print ( "d = [$d]\n");

-- 
David Means

Don't frobnicate without grokking.



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

Date: Wed, 06 Nov 2002 22:36:10 -0500
From: David Means <dmeans@the-means.net>
Subject: Re: sh (bash) $1 equivalent
Message-Id: <pan.2002.11.07.03.36.04.397030.6681@the-means.net>

On Wed, 06 Nov 2002 22:11:44 -0500, David Means wrote:

{ snip } 

> local($a,$b,$c,$d) = @ARGV;
> print ( "a = [$a]\n");
> print ( "b = [$b]\n");
> print ( "c = [$c]\n");
> print ( "d = [$d]\n");
 
As usual, there's always more than one way to hang yourself in perl, as
is the case with most good languages:


while( $arg=shift(@ARGV) ) {
    print ( "arg = $arg\n");
}

-- 
David Means

C makes it easy for you to shoot yourself in the foot.  C++ makes that
harder, but when you do, it blows away your whole leg.
-- Bjarne Stroustrup



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

Date: Wed, 06 Nov 2002 19:21:54 -0500
From: Benjamin Goldberg <goldbb2@earthlink.net>
Subject: Re: Simple question - What is unicode?
Message-Id: <3DC9B222.78657C64@earthlink.net>

David wrote:
> 
> What is unicode *.txt?

Unicode is simply a mapping between numeric values and characters.

> There is an option to save a file in this format in Excel.

Err, no.  It may say that, but it's really offering to save in one of
the many *encodings* of unicode, such as utf8, utf16-be, utf16-le,
utf32-be, utf32-le, or one of the encodings for a subset of unicode,
such as ucs2 or ucs4.  These encodings are mappings from numbers to
sequences of bytes.

You wouldn't happen to know which of those encodings Excel uses, when it
saves in its so-called unicode format, do you?

> The following code I wrote does not do what I expect it to do.  It
> should read in a file of names and then output the names, but when it
> outputs the names it includes a space between each letter.

Hmm, sounds like you're reading a file in utf16-be, utf16-le, or ucs2.

The spaces between the letters are actually "\0" bytes, which your
terminal is showing as if they were " " bytes.

> I spent close to an hour trying to solve this when I finally realised
> that I had saved the original data file from Excel using unicode
> *.txt.  When I change to comma delimited *.txt it worked perfectly. 
> But why?

As far as Excel is concerned, comma delimited data must be in a format
with one-byte-per-character.... such as latin1 or latin2, which are the
formats which you are used to.  To do this, it throws away the upper
eight bits of the 16 bit data it has.

> What is unicode?
> 
> open (FILENAMES, $Filenames) || die "The file with the list of filenames
> does not exist!\n Program Aborted!\n ";

Actually, there are other reasons than non-existance for open to fail. 
If you were to output $! as part of the error message, you would see
these.  Something like:

   open( FILENAMES, $Filenames )
      or die "Failed to open file '$Filenames': $!\n";

>  while ($name_of_file = <FILENAMES>) { #look at each line in the list
>       chop($name_of_file);

Err, you should be using chomp, not chop.  Throw out whatever book
advised you code like this, and get a new book.

>       print ("\n$name_of_file");

And you probably ought to be printing the \n at the end of the line, not
at the beginning -- otherwise, you get a blank line at the beginning of
the list, and you'll be missing a line at the end of the list.

> } #While loop

-- 
my $n = 2; print +(split //, 'e,4c3H r ktulrnsJ2tPaeh'
 ."\n1oa! er")[map $n = ($n * 24 + 30) % 31, (42) x 26]


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

Date: Thu, 07 Nov 2002 02:22:51 GMT
From: Bob Walton <bwalton@rochester.rr.com>
Subject: Re: Telnet to OpenVMS, run cobol program - not working.
Message-Id: <3DC9CE56.6040401@rochester.rr.com>

Bob Walton wrote:

> Joseph Norris wrote:
> 
> 
>> I am using Net::Telnet to telnet into a OpenVms box and run a cobol
>> program. I have set the script up as below:
>>
>>
>> The problem is that the displays from the cobol program do not show
>> up and so my waitfor's do not make it and the script dies.
>>
>> The last entry in the log file just shows the name of the cobol program
> 
> ...
> 
> 
>>     $t->waitfor("PRINT ALL CLASSES? (DEFAULT ALL CLASSES)(Y/N)");
> 
> 
> 
> Hmmmm...according to the docs, the single-string-argument waitfor is 
> expecting a "matchop".  That includes delimiters.  So perhaps:
> 
>       $t->waitfor('/PRINT ALL CLASSES? (DEFAULT ALL CLASSES)(Y\/N)/');


Um, I've got the quote all the metacharacters, too:

   $t->waitfor('/PRINT ALL CLASSES\? \(DEFAULT ALL CLASSES\)\(Y\/N\)/');

Sorry.

-- 
Bob Walton



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

Date: 6 Nov 2002 15:10:11 -0800
From: google@hoodfamily.org (Tim)
Subject: Re: Tie'd filehandle and the system() function
Message-Id: <1d2abc5d.0211061510.40a5a140@posting.google.com>

Benjamin Goldberg <goldbb2@earthlink.net> wrote in message news:<3DC86B67.29CBE495@earthlink.net>...

> Although I've given you one answer already, I'd like to suggest the
> following, non-tie solution:
> 
>    open( LOG_FILE_DIRECT, ">>", "script.log" )
>       or die "Couldn't open script.log: $!";
>    my $old_select = select LOG_FILE_DIRECT;
>    $| = 1;
> 
>    for my $fh (\*LOG_OUT, \*LOG_ERR) {
>       defined( my $pid = open( $fh, "|-" ) )
>          or die "Couldn't fork: $!";
>       select($fh), ($| = 1), next if $pid;
>       my $info = join("|", hostname(), $0, __FILE__, getppid());
>       print scalar(localtime), "|", $info, "|", $_ while <STDIN>;
>       exit;
>    }
> 
>    select($old_select);
> 
>    open( STDOUT, ">&LOG_OUT" )
>       or die "Couldn't dup LOG_OUT over STDOUT: $!";
>    open( STDERR, ">&LOG_ERR" ) or
>       or die "Couldn't dup LOG_ERR over STDERR: $!";
> 
>    print "Beginning script\n";
>    print "This script was executed with \@ARGV == [@ARGV]\n";
>    system("external_process");
>    exit;
> 
> [untested]

That's an interesting approach. However, I found it has the
unfortunate side effect of placing all of the standard error output
AFTER all of the standard out. In my test, the "external_process"
shell command does some simple echo's:

echo "Hello, I am an external process"
echo "I am generating my own non-compliant output"
echo "This is standard error" >&2
echo "You will have to format my output to be compliant"
echo "I am done processing now"
exit 1

The log file output (with the timestamp and other formatting removed
for readability) looks like this:

Beginning script
This script was executed with @ARGV == []
Hello, I am an external process
I am generating my own non-compliant output
You will have to format my output to be compliant
I am done processing now
Error: 1
Ending script
This is standard error

It seems nearly every approach I have examined carries some kind of
unwanted baggage. It's odd that kind of output management isn't more
easily accessible in perl.


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

Date: Wed, 06 Nov 2002 18:49:06 -0500
From: Benjamin Goldberg <goldbb2@earthlink.net>
Subject: Re: Tie'd filehandle and the system() function
Message-Id: <3DC9AA72.824C11E@earthlink.net>

Tim wrote:
[snip]
> It seems nearly every approach I have examined carries some kind of
> unwanted baggage. It's odd that kind of output management isn't more
> easily accessible in perl.

Well, as a last resort, you could use IPC::Open3 to get seperate streams
for the external program's stdout and stderr, and use IO::Select to
multiplex them.

   use IPC::Open3; use Symbol qw(gensym); use IO::Select;
   open( LOG_FILE, ">>", "script.log" )
      or die "Couldn't open script.log: $!";
   my $pid = open3( gensym, my( $out, $err ), 'external_process' );
   my %buf = ($out => '', $err => '');
   my $sel = IO::Select->new($out, $err);
   my $info = join("|", "", hostname(), $0, __FILE__, $$, "");
   while( my @r = $sel->can_read ) { for my $r (@r) {
      unless( my $n = sysread( $r, $buf{$r}, 8192, length $buf{$r} ) ) {

         defined $n or die "Error in sysread: $!";

         my $i = localtime . $info;
         print LOG_FILE $i;
         print LOG_FILE $buf{$r}, "\n", $i, "\\missing newline "
            if length $buf{$r};
         print LOG_FILE "EOF\n";

         delete $buf{$r}; $sel->remove($r); close $r;

      }
      my $i = localtime . $info;
      while( $buf{$r} =~ s/^(.*)\n// ) {
         print LOG_FILE $i, $1;
      }
   } }
[untested]

This only works on platforms where you can use select() on pipes.

Thus, this works on most unices, but not on windows.

-- 
my $n = 2; print +(split //, 'e,4c3H r ktulrnsJ2tPaeh'
 ."\n1oa! er")[map $n = ($n * 24 + 30) % 31, (42) x 26]


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

Date: Thu, 07 Nov 2002 04:17:31 GMT
From: "Jürgen Exner" <jurgenex@hotmail.com>
Subject: Re: Unix command
Message-Id: <vPly9.9377$Wf5.5717@nwrddc04.gnilink.net>

ry@yokoyama.ws wrote:
> Thanks for people answering my question.  I did not konw perldoc.  I
> will read it.
>
> I have another question. Is there FAQ?  If so, could you tell me
> where I can find it.

perldoc perlfaq

jue




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

Date: Wed, 6 Nov 2002 20:03:16 -0700
From: Kaushik Mallick <kaushik@mho.com>
Subject: WebCalendar, Registry.pm and @INC problem
Message-Id: <20021106200316.78733922.kaushik@mho.com>

I am quite a newbie to perl. I am trying to run a web-based calendar server called 'WebCalendar' (http://www.math.utexas.edu/webcalendar/) using apache and mod-perl on my RH 8.0. I followed the instructions for installation of the WebCalendar but I keep getting this type of message logged into my /var/log/http/error_log whenever I start http service:

[Wed Nov 06 18:09:17 2002] [error] [client 127.0.0.1] Can't locate Apache/Registry.pm in @INC (@INC contains: /usr/lib/perl5/5.8.0/i386-linux-thread-multi /usr/lib/perl5/5.8.0 /usr/lib/perl5/site_perl/5.8.0/i386-linux-thread-multi /usr/lib/perl5/site_perl/5.8.0 /usr/lib/perl5/site_perl /usr/lib/perl5/vendor_perl/5.8.0/i386-linux-thread-multi /usr/lib/perl5/vendor_perl/5.8.0 /usr/lib/perl5/vendor_perl .) at (eval 1) line 3.

I am correct in assuming that perl cannot locate Registry.pm in its path variable @INC. Do I need to modify @INC? If so how?

Also this is what I get when I type 'locate Registry.pm'
/usr/lib/perl5/vendor_perl/5.8.0/i386-linux-thread-multi/ModPerl/Registry.pm

The following is what I  have appended to httpd.conf file as part of webcal installation:

#==webcal==Bgn==
#
<IfModule mod_perl.c>
  Alias /y/ /var/www/webcal/bin/
  <Location /y>
    AllowOverride All
    SetHandler perl-script
    PerlHandler Apache::Registry
    PerlSendHeader On
#    PerlRequire "/var/www/webcal/bin/startup.pl"
    Options +ExecCGI
  </Location>
</IfModule>

Alias /i/ "/var/www/webcal/webcal_icons/"

<Directory "/var/www/webcal/webcal_icons">
    Options Indexes MultiViews
    AllowOverride All
    Order allow,deny
    Allow from all
</Directory>
#
#==webcal==End==

I have commented out the PerRequire line, otherwise http service just won't start saying PerlRequire isforbidden or something like that. Thats a whole new problem!!

I would appreciate any help I can get. Thanks a bunch beforehand.


-- 
The butterfly counts not months but moments, and has time enough. 
-Rabindranath Tagore 


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

Date: Wed, 06 Nov 2002 22:51:01 -0500
From: David Means <dmeans@the-means.net>
Subject: Re: WebCalendar, Registry.pm and @INC problem
Message-Id: <pan.2002.11.07.03.50.59.175060.6681@the-means.net>

On Wed, 06 Nov 2002 22:03:16 -0500, Kaushik Mallick wrote:

{ snip } 

> I am correct in assuming that perl cannot locate Registry.pm in its path
> variable @INC. Do I need to modify @INC? If so how?
> 
{ snip } 


$ perldoc lib

I then did on google:  "use lib" perl

found:

http://www.geocrawler.com/archives/3/182/1997/6/0/1017911/

-- 
David Means

Depression is just Anger without the enthusiasm.



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

Date: Wed, 06 Nov 2002 19:00:06 -0500
From: Benjamin Goldberg <goldbb2@earthlink.net>
Subject: Re: Win32 and unicode
Message-Id: <3DC9AD05.BF6577F0@earthlink.net>

emcee wrote:
> 
> I am using ActivePerl on Windows 2000, XP, and 98, but whenever I use
> a varible with a +, *, or \ (possibily other charactors) in a regex I
> get an error.

What kind of error?  We cant help you if you don't tell us.

> I read it has something to do with unicode support.

Maybe, maybe not.  It depends on what's happening.

-- 
my $n = 2; print +(split //, 'e,4c3H r ktulrnsJ2tPaeh'
 ."\n1oa! er")[map $n = ($n * 24 + 30) % 31, (42) x 26]


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

Date: Thu, 07 Nov 2002 03:24:32 GMT
From: tiltonj@erols.com (Jay Tilton)
Subject: Re: Win32 and unicode
Message-Id: <3dc9dad6.291003431@news.erols.com>

emcee@inorbit.com (emcee) wrote:

: I am using ActivePerl on Windows 2000, XP, and 98, but whenever I use
: a varible with a +, *, or \ (possibily other charactors) in a regex I
: get an error. 

What is the error?

Show some code that does this.

: I read it has something to do with unicode support.

Where did you read that?

: Since normally when I use a varible in a regex its from user input, a
: file, or something from the internet, there is nothing I can do to
: avoid these charactors in my varibles, 

So escape them.  Look at the quotemeta function.



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

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


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