Re: getting STDERR output...

2002-08-08 Thread Peter Scott

At 05:29 PM 8/8/02 -0400, Sylvanie, Jean-Pierre wrote:
>Hi guys,
>
>Actually I want to get STDERR ouput to have more details about systems call
>errors, and write the result to a log file...
>
>I spent a couple of hours writing (well... Finding how to write ! :)
>the "sample code", but I have the feeling that all that stuff is overkill...
>Is there something more simple ?

Eek.  It seems to me that you could just do

 $output = `$cmd 2>&1`;

and then pattern match on $output after checking return status in $?.

>Thanks for your time !
>
>
>SAMPLE CODE
>
>#
># The following code is for reading STDERR output
># from a filehandle called GET_STDERR
>
># Opening a pipe to get STDERR output.
>pipe(GET_STDERR, FROM_STDERR) or die "pipe $!";
>
># setting GET_STDERR non blocking :
># we don't want to wait for data if nothing's ready
>my $flags = fcntl(GET_STDERR, F_GETFL, 0 ) or die "can't get flags";
>fcntl(GET_STDERR, F_SETFL, $flags | O_NONBLOCK) || die "can't set flags";
>
># setting autoflush to all STDERR related filehandles
>select((select(STDERR)  , $|=1)[0]);
>select((select(GET_STDERR)  , $|=1)[0]);
>select((select(FROM_STDERR) , $|=1)[0]);
>
># redirect STDERR to pipe input
>open(STDERR, ">&FROM_STDERR") || die "Can't dup stderr";
>
># buffer for reading pipe output
>my @err = ();
>
>
>#... bla bla bla ...
>
> my $cmd = "$ZIP_CMD $filename";
>
> if ( system($cmd) != 0 ){
>   chomp(@err = );
>   log_("0310E-Can't zip file Cmd=[$cmd]; stderr=[".join("; ",@err)."]");
> }
>
>#... bla bla bla ...
>
>#
>
>
>
>jp.
>---
>Jean-Pierre Sylvanie
>Microcell Telecom - IT Dev  (o_
>Assurance des Revenus & Fraudes //\
>Revenue Assurance & Fraud   v_/
>(514) 937-0102 (ext: 6062)
>[EMAIL PROTECTED]
>---
>
>
>
>--
>To unsubscribe, e-mail: [EMAIL PROTECTED]
>For additional commands, e-mail: [EMAIL PROTECTED]

--
Peter Scott
Pacific Systems Design Technologies
http://www.perldebugged.com/


-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]




Re: getting STDERR output...

2002-08-08 Thread John W. Krahn

Jean-Pierre Sylvanie wrote:
> 
> Hi guys,

Hello,

> Actually I want to get STDERR ouput to have more details about systems call
> errors, and write the result to a log file...
> 
> I spent a couple of hours writing (well... Finding how to write ! :)
> the "sample code", but I have the feeling that all that stuff is overkill...
> Is there something more simple ?


You could probably use IPC::Open3.

my $pid = open3( \*WTRFH, \*RDRFH, \*ERRFH, $ZIP_CMD, $filename );

Do a Google groups search for open3 on comp.lang.perl.misc and you
should find some examples.



John
-- 
use Perl;
program
fulfillment

-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]




Re: getting STDERR output...

2002-08-08 Thread drieux


On Thursday, August 8, 2002, at 02:29 , Sylvanie, Jean-Pierre wrote:

> Hi guys,
>
> Actually I want to get STDERR ouput to have more details about systems 
> call
> errors, and write the result to a log file...
[..]
> my $cmd = "$ZIP_CMD $filename";
>
> if ( system($cmd) != 0 ){
>   chomp(@err = );
>   log_("0310E-Can't zip file Cmd=[$cmd]; stderr=[".join("; ",@err)."]
> ");
> }

this is where you may want to deal with the
alternative strategy and not use 'system' -
if you really want to cope with the information
that comes back from $cmd

I prefer the classic 'popen()' strategy perl offers
Hence I make sure that the stderr is attached to stdout
of the $cmd - so that it comes back into me with the
classic 2>&1 notation...

open(CMD, "$cmd 2>&1 |")
or die "unable to open $cmd :$!\n"
while() {
#
# if it would only send stuff on stderr then
# you only have to grot it out - otherwise you
# can also grot out the stdout stuff
#
}
close(CMD) or deal_with_error_case($cmd);

ciao
drieux

---


-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]




Re: Copying more than one file using File::Copy

2002-08-08 Thread drieux


On Thursday, August 8, 2002, at 02:00 , <[EMAIL PROTECTED]> wrote:
[..]
> Maybe I'm missing something obvious... but in Perldoc for File::Copy I
> read: "The copy function takes two parameters: a file to copy from and
> a file to copy to."

for fun you might want to do the

perldoc -m File::Copy

and see how the sausage is made...

since it is basically about 'read the input file' and
write it out to the output file - using 'syscopy' - which
is implemented in the Win32 space with

Win32::CopyFile(@_, 1);

which you may wish to look at...

alternatively let's move along

what you want is something that does

dump_across file1 file2 file3 outputfile

that would be:

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

my $outFile = pop @ARGV;

open(OUT, "> $outFile") or die "unable to open output file $outFile 
:$!\n";
for my $file (@ARGV) {

open(IN, "$file") or die "unable to open input file $file :$!\n";
print $OUT $_ while() ;
close(IN);
}
close(OUT);

you wil of course need to tweek that for the Win32 environment,
since you will probably want to deal with how to make sure that
it reads and writes appropriately - but I think you get the main drift.

[..]

ciao
drieux

---


-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]




Re: Copying more than one file using File::Copy

2002-08-08 Thread John W. Krahn

[EMAIL PROTECTED] wrote:
> 
> Good day;

Hello,

> I apologize in advance if this is a very stupid question?
> 
> I'm trying to concatenate two files and have the results written to a
> third file.
> Maybe I'm missing something obvious... but in Perldoc for File::Copy I
> read: "The copy function takes two parameters: a file to copy from and
> a file to copy to."
> 
> What can I do if I have a second file I want to copy from? (i.e. Like
> DOS copy command: c:\ file1.txt + file2.txt file3.txt will copy the
> contents of file1.txt & file2.txt and put the combined contents into
> file3.txt)


One possible solution:

use File::Copy;

my @from = qw/file1 file2 file3/;
my $to = 'newfile';

open NEW, ">$to" or die "Cannot open $to: $!";
binmode NEW;

for my $file ( @from ) {
copy( $file, \*NEW ) or die "Cannot copy $file: $!";
}

__END__



John
-- 
use Perl;
program
fulfillment

-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]




Re: "script.pl textfile.txt" at command line

2002-08-08 Thread Kevin Pfeiffer

I wrote earlier today:
> I'm working on a better version which I'll post tonight. It now splits on
> the semicolon and then lets me process the username separately.

Here's what I came up with (after peeking at the tips you all posted here). 
Be kind - it only looks simple to you! ;-)
---
#!/usr/bin/perl -w
use strict;

# data format:
# [id];[username]
# 12;i.longlastname  (if period, remove and truncate to 11 char)
# 999;namewithoutperiod   (do not truncate)

while (<>) {
chomp;  #chomp because of truncation
my ($userid, $username) = split (/;/,$_,2);
if ($username =~ tr/.//d) {
$username = substr($username,0,11);
}
print "$userid;$username\n";  #print to screen for now
}

-- 
Kevin Pfeiffer <[EMAIL PROTECTED]> - www.iu-bremen.de



-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]




Re: Copying more than one file using File::Copy

2002-08-08 Thread John Pitchko

Also being a Perl n00b myself, my suggestion is that you need to do some more work. 
What about something like:

open FILE, "< filename" or die("blahblahblah");
open FILE2, "< filename2" or die("more blahblahblah");
open FILE3, "> filename3" or die ("still blahblahblah");

my $foo = join ("", );
my $bar = join ("", );

my $newbar = $foo . $bar;

print  $newbar;

I'm sure I'm re-inventing the wheel here, and I realize I am not using File::Copy at 
all like you desired, and this is probably a pretty cheesy way to do this, but it is 
something to ponder, and I'm sure some of the more knowledgeable Perl-keteers can give 
better suggestions.




John Pitchko
Data Services
Saskatchewan Government Insurance

>>> <[EMAIL PROTECTED]> 08/08/02 03:00pm >>>
Good day;

I apologize in advance if this is a very stupid question*

I'm trying to concatenate two files and have the results written to a 
third file. 
Maybe I'm missing something obvious... but in Perldoc for File::Copy I 
read: "The copy function takes two parameters: a file to copy from and 
a file to copy to."

What can I do if I have a second file I want to copy from? (i.e. Like 
DOS copy command: c:\ file1.txt + file2.txt file3.txt will copy the 
contents of file1.txt & file2.txt and put the combined contents into 
file3.txt)

Thank you all for your time.
Carl



--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]



getting STDERR output...

2002-08-08 Thread Sylvanie, Jean-Pierre

Hi guys,

Actually I want to get STDERR ouput to have more details about systems call
errors, and write the result to a log file...

I spent a couple of hours writing (well... Finding how to write ! :) 
the "sample code", but I have the feeling that all that stuff is overkill...
Is there something more simple ?

Thanks for your time !


SAMPLE CODE

#
# The following code is for reading STDERR output 
# from a filehandle called GET_STDERR

# Opening a pipe to get STDERR output.
pipe(GET_STDERR, FROM_STDERR) or die "pipe $!";

# setting GET_STDERR non blocking :
# we don't want to wait for data if nothing's ready
my $flags = fcntl(GET_STDERR, F_GETFL, 0 ) or die "can't get flags";
fcntl(GET_STDERR, F_SETFL, $flags | O_NONBLOCK) || die "can't set flags";

# setting autoflush to all STDERR related filehandles
select((select(STDERR)  , $|=1)[0]);
select((select(GET_STDERR)  , $|=1)[0]);
select((select(FROM_STDERR) , $|=1)[0]);

# redirect STDERR to pipe input
open(STDERR, ">&FROM_STDERR") || die "Can't dup stderr";

# buffer for reading pipe output
my @err = ();


#... bla bla bla ...

my $cmd = "$ZIP_CMD $filename";

if ( system($cmd) != 0 ){
  chomp(@err = );
  log_("0310E-Can't zip file Cmd=[$cmd]; stderr=[".join("; ",@err)."]");
}

#... bla bla bla ...

#



jp.
---
Jean-Pierre Sylvanie
Microcell Telecom - IT Dev  (o_
Assurance des Revenus & Fraudes //\
Revenue Assurance & Fraud   v_/ 
(514) 937-0102 (ext: 6062)
[EMAIL PROTECTED]  
---



-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]




Copying more than one file using File::Copy

2002-08-08 Thread crogers3

Good day;

I apologize in advance if this is a very stupid questionÂ…

I'm trying to concatenate two files and have the results written to a 
third file. 
Maybe I'm missing something obvious... but in Perldoc for File::Copy I 
read: "The copy function takes two parameters: a file to copy from and 
a file to copy to."

What can I do if I have a second file I want to copy from? (i.e. Like 
DOS copy command: c:\ file1.txt + file2.txt file3.txt will copy the 
contents of file1.txt & file2.txt and put the combined contents into 
file3.txt)

Thank you all for your time.
Carl



--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]




Re: A better way for seeding an annoymous hash

2002-08-08 Thread Peter Scott

At 11:44 AM 8/8/02 -0700, drieux wrote:

>On Thursday, August 8, 2002, at 11:04 , Peter Scott wrote:
>
>>At 10:38 AM 8/8/02 -0700, drieux wrote:
>>>I'm not sure the average normal person would feel at home with say:
>>>
>>> %{$by_os{$os_key}}->{$_}++ for(@$found);
>>
>>Especially since it's only by accident that
>>
>> %hash->{key}
>>
>>happens to do the same thing as
>>
>> $hash{key}
>>
>>Many people think it's a bug and plan on eliminating it.
>
>so how do I work around that

Simple; instead of typing

 %hash->{key}

or any more complex variant of it, use

 $hash{key}

>should I use
>
> ${$by_os{$os_key}}{$_}++ for(@$found);
>
>which is only marginally more Dense???

 $by_os{$os_key}{$_}++ for @$found

(Pity that @{$by_os{$os_key}}{@$found}++ doesn't work...)

See perlref.

>we're on the back side of this coding game
>where I clean up all of the sillies that seemed
>useful as I tested them out - and were suppose to
>make the code simpler to read - hence also to maintain...

--
Peter Scott
Pacific Systems Design Technologies
http://www.perldebugged.com/


-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]