[30990] in Perl-Users-Digest
Perl-Users Digest, Issue: 2235 Volume: 11
daemon@ATHENA.MIT.EDU (Perl-Users Digest)
Wed Feb 25 16:09:46 2009
Date: Wed, 25 Feb 2009 13:09: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 Wed, 25 Feb 2009 Volume: 11 Number: 2235
Today's topics:
Re: debugging on stdout (Tim McDaniel)
Re: Email Purge <tod@asgweb.net>
Re: Email Purge <jimsgibson@gmail.com>
Re: obtaining permission <larry@example.invalid>
Perl as a testengine, problems analyzing results time_error@hotmail.com
Re: Perl as a testengine, problems analyzing results <perl@marc-s.de>
Re: Strange behavior with MIME::Lite <rvtol+usenet@xs4all.nl>
Re: Strange behavior with MIME::Lite <edMbj@aes-intl.com>
Re: Strange behavior with MIME::Lite <edMbj@aes-intl.com>
Re: Strange behavior with MIME::Lite <perl@marc-s.de>
Re: utf8 and chomp <rvtol+usenet@xs4all.nl>
Re: utf8 and chomp <rvtol+usenet@xs4all.nl>
very simple file stitching problem <brianwilliambishop@gmail.com>
Re: very simple file stitching problem (Greg Bacon)
Re: very simple file stitching problem <jurgenex@hotmail.com>
Re: very simple file stitching problem <someone@example.com>
Digest Administrivia (Last modified: 6 Apr 01) (Perl-Users-Digest Admin)
----------------------------------------------------------------------
Date: Wed, 25 Feb 2009 17:20:57 +0000 (UTC)
From: tmcd@panix.com (Tim McDaniel)
Subject: Re: debugging on stdout
Message-Id: <go3ulp$a2m$1@reader1.panix.com>
In article <ldhe76-1cp.ln1@osiris.mauzo.dyndns.org>,
Ben Morrow <ben@morrow.me.uk> wrote:
>I would avoid using Open2 directly in favour of IPC::Run, which has a
>much cleaner interface and is more portable to systems like Win32.
One review says <http://cpanratings.perl.org/dist/IPC-Run>
In words of the author of IPC::Run
> I would personally recommend IPC::Cmd rather than IPC::Run right now.
> It seems more stable and well-supported.
> It's not obsolete -- still works for many people, even with test
> failures -- but IPC::Cmd seems to be more actively maintained.
My review:
It works well and is easy to use but I do not like its interface.
Roberto - 2007-01-31 06:48:19
Is there a reason to go for IPC::Run instead of IPC::Cmd?
--
Tim McDaniel, tmcd@panix.com
------------------------------
Date: Wed, 25 Feb 2009 12:09:47 -0800 (PST)
From: perl <tod@asgweb.net>
Subject: Re: Email Purge
Message-Id: <cd3c0293-9513-4fa6-a203-f23f92cfdb52@w34g2000yqm.googlegroups.com>
Ok, thanks for the helpful responses. I modified the code with 2
different suggestions. I'm not using strict. Any more ideas?
With this code I get this error
Can't modify concatenation (.) or string in scalar assignment at
email.cgi line 535, near "<$list>;"
with this code: %emails = map { chomp; $_, 1 } <$list>;
And I get this error
Can't modify concatenation (.) or string in scalar assignment at
email.cgi line 534, near "1 if"
with this code: $emails{lc $_} = 1 if ! exists $emails{lc $_};
Here is the whole routine...
my $list2 = "$userpath/files/$list";
my %emails;
open(my $list, '<', $list2)
or error("Open failed $list2 $!");
while (<$list>) {
chomp if defined;
next if /^\s*$/;
# do a check here maybe and a "next" if not a valid email
address.
$emails{lc $_} = 1 if ! exists $emails{lc $_};
#%emails = map { chomp; $_, 1 } <$list>;
}
close($list) or warn $!;
------------------------------
Date: Wed, 25 Feb 2009 12:41:27 -0800
From: Jim Gibson <jimsgibson@gmail.com>
Subject: Re: Email Purge
Message-Id: <250220091241275358%jimsgibson@gmail.com>
In article
<cd3c0293-9513-4fa6-a203-f23f92cfdb52@w34g2000yqm.googlegroups.com>,
perl <tod@asgweb.net> wrote:
> Ok, thanks for the helpful responses. I modified the code with 2
> different suggestions. I'm not using strict. Any more ideas?
>
> With this code I get this error
> Can't modify concatenation (.) or string in scalar assignment at
> email.cgi line 535, near "<$list>;"
> with this code: %emails = map { chomp; $_, 1 } <$list>;
>
> And I get this error
> Can't modify concatenation (.) or string in scalar assignment at
> email.cgi line 534, near "1 if"
> with this code: $emails{lc $_} = 1 if ! exists $emails{lc $_};
>
> Here is the whole routine...
> my $list2 = "$userpath/files/$list";
> my %emails;
> open(my $list, '<', $list2)
> or error("Open failed $list2 $!");
> while (<$list>) {
> chomp if defined;
> next if /^\s*$/;
> # do a check here maybe and a "next" if not a valid email
> address.
You need to put a '#' character at the beginning of the previous line.
> $emails{lc $_} = 1 if ! exists $emails{lc $_};
> #%emails = map { chomp; $_, 1 } <$list>;
> }
>
> close($list) or warn $!;
--
Jim Gibson
------------------------------
Date: Wed, 25 Feb 2009 13:09:43 -0700
From: Larry Gates <larry@example.invalid>
Subject: Re: obtaining permission
Message-Id: <1kk24in7j8igq$.gv06zxh1qo6p$.dlg@40tude.net>
On Tue, 24 Feb 2009 14:25:24 -0800, Tim Greer wrote:
>> First of all, I don't think of extensions of my keyboard as a robot.
>> Robots don't consist of 2 sprained hands and no spares.
>>
>> Secondly, I send these sites many fewer keystrokes with perl than I do
>> with my browser.
>>
>> Thirdly, I've got all the time in the world to obtain explicit legal
>> permission to do what I want with either of these entities.
>>
>> How do I do this?
>
> If you automate something to obtain data from a remote site
> infrequently, you are likely fine, if it's just a replacement for using
> your browser and saving a file. If you plan to do it frequently or
> have a site that enacts the connection/download each time someone
> visits a script/area of your site, then it could be (or will be) more
> frequent, even creating a load on the remote site. So, ask that site
> admin or webmaster contact. I don't understand the question about how
> you get permission, other than to suggest you contact the appropriate
> contact person at said site/company and ask. Another issue is if they
> block any non common user agents in an effort to prevent automated
> scripts. There could be a number of reasons beyond that about why they
> forbid it on their site.
I think the prohibition has much more to do with sending the page a lot of
information and gumming up the works.
I found a link to the webmaster of the fourmilab site and said as much.
We'll see what he says.
--
larry gates
There are still some other things to do, so don't think if I didn't fix
your favorite bug that your bug report is in the bit bucket. (It may be,
but don't think it. :-) Larry Wall in <7238@jpl-devvax.JPL.NASA.GOV>
------------------------------
Date: Wed, 25 Feb 2009 05:42:33 -0800 (PST)
From: time_error@hotmail.com
Subject: Perl as a testengine, problems analyzing results
Message-Id: <75dbd742-56bc-40da-8783-538b22cc1e71@v13g2000vbb.googlegroups.com>
Hi
I have previously used perl as a framework for testing an embedded
device via. telnet. It was quite easy, just send input data (commands,
mails, network traffic) and analyze logfiles and telnet output for
expected output =96 obvious it was a little more complex than that, but
I think you get the idea.
My new project is a pure windows console application. So what I want
is to stimulate my application with input (mails, SMS=B4s, UDP data,
direct application input (it has a built-in command shell)) and
analyze the console output and logfiles.
My test starts with an empty prompt - I write something like this: >
perl framework.pl test_file1.pl and hit <enter>. The framework then
does a lot of stuff and finally launches the application.
1)
The application writes output to the console, but I cannot figure out
how to read the output.
2)
I would like to read a log file generated by the application. If I try
something like this: system(=93type logfile.log=94) nothing happens.
Perhaps I can use some Perl specific fileroutines to read filecontent?
I suspect that the Perlscript is blocked until the application exists.
So my real question is in fact: Can I start and stop the application
in another prompt (context) and how is this accomplished using Perl?
Then I just analyze logfiles for test results (and abandon reading
console output).
------------------------------
Date: Wed, 25 Feb 2009 14:49:48 +0100
From: Marc Lucksch <perl@marc-s.de>
Subject: Re: Perl as a testengine, problems analyzing results
Message-Id: <go3i9l$qb5$1@ariadne.rz.tu-clausthal.de>
time_error@hotmail.com schrieb:
> 1)
> The application writes output to the console, but I cannot figure out
> how to read the output.
Maybe this can help, (from perlfaq8):
http://perldoc.perl.org/perlfaq8.html#How-can-I-capture-STDERR-from-an-external-command%3f
> 2)
> I would like to read a log file generated by the application. If I try
> something like this: system(“type logfile.log”) nothing happens.
> Perhaps I can use some Perl specific fileroutines to read filecontent?
Maybe this can help, (from perlfaq8):
http://perldoc.perl.org/perlfaq8.html#Why-can%27t-I-get-the-output-of-a-command-with-system()%3f
Marc "Maluku" Lucksch
------------------------------
Date: Wed, 25 Feb 2009 13:26:17 +0100
From: "Dr.Ruud" <rvtol+usenet@xs4all.nl>
Subject: Re: Strange behavior with MIME::Lite
Message-Id: <49a538e9$0$192$e4fe514c@news.xs4all.nl>
Ed Jay wrote:
> eval { $msg->send };
> die "MIME::Lite->send failed: $@\n" if $@;
I would write that as:
eval {
$msg->send;
1; # success
}
or do {
my $err = $@ || "Unknown error";
die "MIME::Lite->send failed: $err\n";
};
--
Ruud
------------------------------
Date: Wed, 25 Feb 2009 07:27:14 -0800
From: Ed Jay <edMbj@aes-intl.com>
Subject: Re: Strange behavior with MIME::Lite
Message-Id: <lpoaq4h8nktefooth6ge754m1uj13ga3jq@4ax.com>
Dr.Ruud wrote:
>Ed Jay wrote:
>
>> eval { $msg->send };
>> die "MIME::Lite->send failed: $@\n" if $@;
>
>I would write that as:
>
> eval {
> $msg->send;
> 1; # success
> }
> or do {
> my $err = $@ || "Unknown error";
> die "MIME::Lite->send failed: $err\n";
> };
Thank you.
What does the '1' do?
--
Ed Jay (remove 'M' to reply by email)
Win the War Against Breast Cancer.
Knowing the facts could save your life.
http://www.breastthermography.info
------------------------------
Date: Wed, 25 Feb 2009 07:46:16 -0800
From: Ed Jay <edMbj@aes-intl.com>
Subject: Re: Strange behavior with MIME::Lite
Message-Id: <nqpaq4trka6an6m62c7odi1l3pt5ts4247@4ax.com>
Dr.Ruud wrote:
>Ed Jay wrote:
>
>> eval { $msg->send };
>> die "MIME::Lite->send failed: $@\n" if $@;
>
>I would write that as:
>
> eval {
> $msg->send;
> 1; # success
> }
> or do {
> my $err = $@ || "Unknown error";
> die "MIME::Lite->send failed: $err\n";
> };
Get a server error, unless I remove the '1'. When I removed the '1', no
server error, but if the mime type is text/XX, it fails to send the email
and no error is returned. If I use Type = application/XX, it works.
--
Ed Jay (remove 'M' to reply by email)
Win the War Against Breast Cancer.
Knowing the facts could save your life.
http://www.breastthermography.info
------------------------------
Date: Wed, 25 Feb 2009 17:35:37 +0100
From: Marc Lucksch <perl@marc-s.de>
Subject: Re: Strange behavior with MIME::Lite
Message-Id: <go3s0h$10dv$1@ariadne.rz.tu-clausthal.de>
Ed Jay schrieb:
> Dr.Ruud wrote:
>> eval {
>> $msg->send;
>> 1; # success
>> }
>> or do {
>> my $err = $@ || "Unknown error";
>> die "MIME::Lite->send failed: $err\n";
>> };
> What does the '1' do?
There might be some cases where eval {..}; if ($@) {} does not work.
See this:
http://search.cpan.org/dist/Perl-Critic/lib/Perl/Critic/Policy/ErrorHandling/RequireCheckingReturnValueOfEval.pm
Marc "Maluku" Lucksch
------------------------------
Date: Wed, 25 Feb 2009 13:41:53 +0100
From: "Dr.Ruud" <rvtol+usenet@xs4all.nl>
Subject: Re: utf8 and chomp
Message-Id: <49a53c92$0$192$e4fe514c@news.xs4all.nl>
Eric Pozharski wrote:
> I've just gone through your original script with debugger, and found out
> that after C<$line = <>;> I<$line> is pure byte string. And then after
> C<chomp $line;> it automagically decodes into utf8 character(!) string.
> Should I keep on explaining? (No, no spoiler this time.)
Spoiler:
$ perl -Mencoding=utf8 -wle '
my $c;
{ use bytes;
$c = "EUR:\xE2\x82\xAC";
print length $c;
}
$c .= "";
print length $c;
'
7
5
--
Ruud
------------------------------
Date: Wed, 25 Feb 2009 14:07:33 +0100
From: "Dr.Ruud" <rvtol+usenet@xs4all.nl>
Subject: Re: utf8 and chomp
Message-Id: <49a54295$0$184$e4fe514c@news.xs4all.nl>
Dr.Ruud wrote:
> Eric Pozharski:
>> I've just gone through your original script with debugger, and found out
>> that after C<$line = <>;> I<$line> is pure byte string. And then after
>> C<chomp $line;> it automagically decodes into utf8 character(!) string.
>> Should I keep on explaining? (No, no spoiler this time.)
>
> Spoiler:
>
> $ perl -Mencoding=utf8 -wle '
> my $c;
> { use bytes;
> $c = "EUR:\xE2\x82\xAC";
> print length $c;
> }
> $c .= "";
> print length $c;
> '
> 7
> 5
Even more impressive:
$ perl -Mencoding=utf8 -wle '
my $c;
{ use bytes;
$c = "EUR:\xE2\x82\xAC";
print length $c;
}
print length $c;
$c .= "";
print length $c;
'
7
7
5
(perl 5.8.5)
--
Ruud
------------------------------
Date: Wed, 25 Feb 2009 12:07:43 -0800 (PST)
From: Brian <brianwilliambishop@gmail.com>
Subject: very simple file stitching problem
Message-Id: <94833ba6-1e9f-485b-a461-5a9bb63d1e4b@r3g2000vbp.googlegroups.com>
it may not be obvious from this novitiate perl code below, but i am
trying to add the lines of one file to the lines of another file to
create a third file. the lines in each file are semantically related,
and will always 'line up.'
use strict;
$ARGV[2] or die "Usage: perl stitch2files.pl [infile1] [infile2]
[outfile] - attaches all values from the list of equal values to ea
line of another list ";
my $infile1 = $ARGV[0];
my $infile2 = $ARGV[1];
my $outfile = $ARGV[2];
open INFILE1, $infile1 or die "Couldn't open input file: $infile1\n";
open INFILE2, $infile2 or die "Couldn't open input file: $infile2\n";
open OUTPUTFILE, ">$outfile" or die "Couldn't open output file:
$outfile\n";
my @data1 = <INFILE1>;
my @data2 = <INFILE2>;
close INFILE1;
close INFILE2;
my $sLine;
my $iLineCount = 0;
foreach $sLine (@data1)
{
my $sAddLine = "";
$iLineCount = $iLineCount + 1;
chomp($sLine);
$sAddLine = @data2[$iLineCount];
$sLine = $sLine . "," . $sAddLine;
printf $sLine;
print OUTPUTFILE $sLine;
}
close OUTPUTFILE;
exit(0);
problem is, i can't seem to address the individual items in the array
formed from the second file, from within a loop designed to process
each item in the first array (formed from the first file).
the error message, while understandable, seems unavoidable:
Use of uninitialized value in concatenation (.) or string at
stitch2files.pl lin
e 28.
any help would be appreciated
thanks
------------------------------
Date: Wed, 25 Feb 2009 14:44:26 -0600
From: gbacon@hiwaay.net (Greg Bacon)
Subject: Re: very simple file stitching problem
Message-Id: <Edydnfjv8oc3MDjUnZ2dnUVZ_iyWnZ2d@posted.hiwaay2>
Brian wrote
: it may not be obvious from this novitiate perl code below, but i am
: trying to add the lines of one file to the lines of another file to
: create a third file. the lines in each file are semantically related,
: and will always 'line up.'
:
: [...]
: foreach $sLine (@data1)
: {
: my $sAddLine = "";
: $iLineCount = $iLineCount + 1;
: chomp($sLine);
: $sAddLine = @data2[$iLineCount];
: $sLine = $sLine . "," . $sAddLine;
: printf $sLine;
: print OUTPUTFILE $sLine;
: }
You're incrementing $iLineCount too soon. Move it to the end of
your loop to get the output you expect.
Style tips:
* Instead of
$iLineCount = $iLineCount + 1;
use the increment operator and write
++$iLineCount;
* Instead of getting a single-element slice with
$sAddLine = @data2[$iLineCount];
you should write
$sAddLine = $data2[$iLineCount];
If you'd enabled warnings, perl would have already
told you this.
Below is another way to do what you want:
#! /usr/bin/perl
use warnings;
use strict;
sub usage { "Usage: $0 file ..\n" }
die usage unless @ARGV;
my @fh;
for (@ARGV) {
open my $fh, $_ or die "$0: open $_: $!\n";
push @fh => $fh;
}
while (1) {
my @lines = map scalar readline $_, @fh;
last if grep !defined, @lines;
chomp @lines;
print join("," => @lines), "\n";
}
Note that it accepts one or more files to zip together and reads a
line at a time from each rather than everything all at once from
the inputs. Because it passes user input directly to Perl's open
operator, you have a little more expressive power:
$ ./try input 'head -2 input |'
1,1
2,2
$ ./try input 'perl -e "print reverse <>" input |'
1,5
2,4
3,3
4,2
5,1
Enjoy,
Greg
--
Laws are made for men of ordinary understanding and should, therefore, be
construed by the ordinary rules of common sense. Their meaning is not to be
sought for in metaphysical subtleties which may make anything mean everything
or nothing at pleasure. -- Thomas Jefferson
------------------------------
Date: Wed, 25 Feb 2009 12:55:50 -0800
From: Jürgen Exner <jurgenex@hotmail.com>
Subject: Re: very simple file stitching problem
Message-Id: <ehbbq4lnkded19a8le08lp83hc5udtn4aa@4ax.com>
Brian <brianwilliambishop@gmail.com> wrote:
>it may not be obvious from this novitiate perl code below, but i am
>trying to add the lines of one file to the lines of another file to
>create a third file. the lines in each file are semantically related,
>and will always 'line up.'
>
>use strict;
>
>$ARGV[2] or die "Usage: perl stitch2files.pl [infile1] [infile2]
>[outfile] - attaches all values from the list of equal values to ea
>line of another list ";
>
>my $infile1 = $ARGV[0];
>my $infile2 = $ARGV[1];
>my $outfile = $ARGV[2];
>
>open INFILE1, $infile1 or die "Couldn't open input file: $infile1\n";
>open INFILE2, $infile2 or die "Couldn't open input file: $infile2\n";
>open OUTPUTFILE, ">$outfile" or die "Couldn't open output file:
>$outfile\n";
>
>my @data1 = <INFILE1>;
>my @data2 = <INFILE2>;
>close INFILE1;
>close INFILE2;
If you really want to slurp in both files then replace your loop with
the three lines below:
chomp @data1;
foreach my $index (0..@data1) {
print OUTPUTFILE "$data1[$index],$data2[$index]";
}
However there is really no need because you can read line by line and
print each line as you read it, too (untested):
while (<INFILE1>){
chomp;
print OUTPUTFILE $_ , ",", scalar(<INFILE2>);
}
Again, this assumes that your statement "the lines in each file [...]
will always 'line up.'"" is true.
jue
------------------------------
Date: Wed, 25 Feb 2009 13:01:21 -0800
From: "John W. Krahn" <someone@example.com>
Subject: Re: very simple file stitching problem
Message-Id: <Ckipl.21485$Hy7.17439@newsfe25.iad>
Brian wrote:
> it may not be obvious from this novitiate perl code below, but i am
> trying to add the lines of one file to the lines of another file to
> create a third file. the lines in each file are semantically related,
> and will always 'line up.'
>
> use strict;
use warnings;
> $ARGV[2] or die "Usage: perl stitch2files.pl [infile1] [infile2]
> [outfile] - attaches all values from the list of equal values to ea
> line of another list ";
@ARGV == 3 or die "Usage: ...";
> my $infile1 = $ARGV[0];
> my $infile2 = $ARGV[1];
> my $outfile = $ARGV[2];
my ( $infile1, $infile2, $outfile ) = @ARGV;
> open INFILE1, $infile1 or die "Couldn't open input file: $infile1\n";
> open INFILE2, $infile2 or die "Couldn't open input file: $infile2\n";
> open OUTPUTFILE, ">$outfile" or die "Couldn't open output file:
> $outfile\n";
You should include the $! variable in the error message(s) so you know
why open failed.
> my @data1 = <INFILE1>;
> my @data2 = <INFILE2>;
> close INFILE1;
> close INFILE2;
>
> my $sLine;
> my $iLineCount = 0;
> foreach $sLine (@data1)
foreach my $sLine (@data1)
> {
> my $sAddLine = "";
> $iLineCount = $iLineCount + 1;
$iLineCount++;
> chomp($sLine);
> $sAddLine = @data2[$iLineCount];
You have just incremented $iLineCount so it will miss the first element
of @data2 and try to read one element past the last element of @data2.
Also, you are using an array slice when you should be using a scalar,
which perl would have warned you about if you had used the warnings pragma.
$sAddLine = $data2[$iLineCount];
$iLineCount++;
Or simply:
$sAddLine = $data2[$iLineCount++];
> $sLine = $sLine . "," . $sAddLine;
$sLine .= ",$sAddLine";
> printf $sLine;
You are using printf which treats its first argument as a format string
so if you have any '%' characters in $sLine this will not work. Just
use print:
print $sLine;
> print OUTPUTFILE $sLine;
> }
>
> close OUTPUTFILE;
>
> exit(0);
>
> problem is, i can't seem to address the individual items in the array
> formed from the second file, from within a loop designed to process
> each item in the first array (formed from the first file).
>
> the error message, while understandable, seems unavoidable:
>
> Use of uninitialized value in concatenation (.) or string at
> stitch2files.pl lin
> e 28.
John
--
Those people who think they know everything are a great
annoyance to those of us who do. -- Isaac Asimov
------------------------------
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 V11 Issue 2235
***************************************