[25352] in Perl-Users-Digest
Perl-Users Digest, Issue: 7596 Volume: 10
daemon@ATHENA.MIT.EDU (Perl-Users Digest)
Mon Jan 3 21:53:36 2005
Date: Mon, 3 Jan 2005 15:16:47 -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 Mon, 3 Jan 2005 Volume: 10 Number: 7596
Today's topics:
Getting stderr when using '-|' <please_post@nomail.edu>
Re: Getting stderr when using '-|' <karlUNDERSCOREkramsch@yahooPERIODcom.invalid>
Hot 22 Year Old Looking For Love Or More.....Please Con logdhvjc@freedvd.com
how to download pictures in my web page <kmansoory6323@yahoo.com>
Re: how to download pictures in my web page <abigail@abigail.nl>
Re: how to download pictures in my web page <matthew.garrish@sympatico.ca>
Re: how to download pictures in my web page <nospam@nospam.com>
Re: How to write a program to ... <bik.mido@tiscalinet.it>
Re: inheritance and order of packages ioneabu@yahoo.com
Digest Administrivia (Last modified: 6 Apr 01) (Perl-Users-Digest Admin)
----------------------------------------------------------------------
Date: Thu, 30 Dec 2004 05:52:41 +0000 (UTC)
From: bill <please_post@nomail.edu>
Subject: Getting stderr when using '-|'
Message-Id: <cr0539$so4$1@reader1.panix.com>
I want to use exec($prog, @args) to execute a system command without
worrying about shell escapes, but I also want to capture both stdout
and stderr from this system command. If I only wanted stdout, I
could do something like this
die "Can't fork: $!" unless defined(my $pid = open(my $read_child, '-|'));
if ($pid == 0) {
exec($prog, @argv) or die "Can't exec $prog: $!\n";
exit 0; # superfluous
}
my $output = do { local $/ = undef;, <$read_child> };
close $read_child;
if ($?) {
die "Child failed: $?"
}
In the code above, $output captures the stdout from the command
run through exec. But how does one capture stderr as well? Not
surprisingly adding the string '2>&1' to exec's arguments fails
because exec just passes this string along as one more argument to
$prog.
Any help would be much appreciated.
bill
------------------------------
Date: Thu, 30 Dec 2004 05:55:17 +0000 (UTC)
From: KKramsch <karlUNDERSCOREkramsch@yahooPERIODcom.invalid>
Subject: Re: Getting stderr when using '-|'
Message-Id: <cr0584$so6$1@reader1.panix.com>
In <cr0539$so4$1@reader1.panix.com> bill <please_post@nomail.edu> writes:
>I want to use exec($prog, @args) to execute a system command without
>worrying about shell escapes, but I also want to capture both stdout
>and stderr from this system command. If I only wanted stdout, I
>could do something like this
>die "Can't fork: $!" unless defined(my $pid = open(my $read_child, '-|'));
>if ($pid == 0) {
open (STDERR, '>&STDOUT') or die "Can't dup STDOUT\n";
> exec($prog, @argv) or die "Can't exec $prog: $!\n";
> exit 0; # superfluous
>}
>my $output = do { local $/ = undef;, <$read_child> };
>close $read_child;
>if ($?) {
> die "Child failed: $?"
>}
HTH,
Karl
--
Sent from a spam-bucket account; I check it once in a blue moon. If
you still want to e-mail me, cut out the extension from my address,
and make the obvious substitutions on what's left.
------------------------------
Date: Thu, 30 Dec 2004 03:44:47 GMT
From: logdhvjc@freedvd.com
Subject: Hot 22 Year Old Looking For Love Or More.....Please Contact Me.......... 433Q
Message-Id: <13133724653219840@freedvd.com>
Hi
Im 22 and I really need someone to take care of me. Please take a look at my home page, you can email me and maybe we could get in touch and have some fun.
My home page is here http://www.sonicboards.com/dianehome.htm - Please get in touch soon :)
----------------------------------------------------------------------------
---
Sibebo tab na fen dicol hemadita binofelam pudikeceref dec mera tobori nisexel mepof reduxosib sol sac .
------------------------------
Date: 29 Dec 2004 16:11:22 -0800
From: "kmansoory" <kmansoory6323@yahoo.com>
Subject: how to download pictures in my web page
Message-Id: <1104365482.686460.151170@c13g2000cwb.googlegroups.com>
i'm trying to make a web side and need to know how to download pictures.
------------------------------
Date: 30 Dec 2004 00:16:46 GMT
From: Abigail <abigail@abigail.nl>
Subject: Re: how to download pictures in my web page
Message-Id: <slrnct6i7e.h3.abigail@alexandra.abigail.nl>
kmansoory (kmansoory6323@yahoo.com) wrote on MMMMCXXXIX September
MCMXCIII in <URL:news:1104365482.686460.151170@c13g2000cwb.googlegroups.com>:
$$ i'm trying to make a web side and need to know how to download pictures.
$$
And? Do you have a Perl question?
Abigail
--
perl -wle 'sub _ "Just another Perl Hacker"; print prototype \&_'
------------------------------
Date: Wed, 29 Dec 2004 19:15:12 -0500
From: "Matt Garrish" <matthew.garrish@sympatico.ca>
Subject: Re: how to download pictures in my web page
Message-Id: <hMHAd.26014$nV.876367@news20.bellglobal.com>
"kmansoory" <kmansoory6323@yahoo.com> wrote in message
news:1104365482.686460.151170@c13g2000cwb.googlegroups.com...
>
> i'm trying to make a web side and need to know how to download pictures.
>
I don't see how the two are related to Perl in any way. I suppose you could
check out the LWP::Simple module, or perhaps provide a better explanation of
what your problem is.
Matt
------------------------------
Date: Wed, 29 Dec 2004 20:45:38 -0500
From: "daniel kaplan" <nospam@nospam.com>
Subject: Re: how to download pictures in my web page
Message-Id: <1104371127.769596@nntp.acecape.com>
"kmansoory" <kmansoory6323@yahoo.com> wrote in message
> i'm trying to make a web side and need to know how to download pictures.
as you can probably already tell, you are in the wrong forum. also, i'm not
really sure what you mean by your question.
are you doing some programming, and would like to allow vistors to your
website to upload pictures via your website?
are you making a website and not sure how to download your picutres to the
server?
etc...etc...etc...
not tryign to give you a hard time, just be a little clearer where you are
so someone can better point you out to the "correct" resources.
daniel
------------------------------
Date: Mon, 03 Jan 2005 23:22:24 +0100
From: Michele Dondi <bik.mido@tiscalinet.it>
Subject: Re: How to write a program to ...
Message-Id: <bt9jt0hg3mr03vp6mbc7te6gbqjtjvhgoj@4ax.com>
On 2 Jan 2005 07:02:46 -0800, bart@nijlen.com (Bart Van der Donck)
wrote:
>This could be an example. Your input file must be readable ($fileA)
[snip]
>-------------------------------------
>PROGRAM START
>-------------------------------------
Incidentally the shebang line is much like an "hello" and the __END__
token is much like a "bye" (unless you "have DATA"). So there's hardly
any need for a dedicated marker. And if you indent the whole code a
few spaces it will be even more visually distinguished...
>#!/usr/bin/perl
>use strict;
>use warnings;
Good! (And good practice to show this to the OP!!)
># init
IMHO hardly any need for unnecessary cmts. Good code should be mostly
self-commenting.
>my $fileA = "initialfile.txt";
>my $fileB = "alteredfile.txt";
Why hardcoding them, then? Even if this is only an example
length('shift')==5. But then of course one should take care of error
checking @ARGV, etc. so I'll take your point...
>my $string1 = "XXXX";
>my $string2 = "YYYY";
Also
my ($string1, $string2) = qw/XXXX YYYY/;
But then both strings may contain spaces, so I'll take your point
again. But it's worth noting that one can declare and initialize more
variables at a time...
>my $tenspaces;
>my @initialfile;
>my @alteredfile;
>my $alteredline;
No need to declare all of the required variables at one point (in
Perl, that is).
>for (1..10) {$tenspaces.=' '}
Yuk!
$tenspaces = ' ' x 10;
># read $fileA and do regexp in each line
Ditto as above wrt cmts.
>open (R, "$fileA")||die"$!";
Better to use lexical fhs nowadays. Better to use the three-args form
of open() anyway. No need to quote all variables (see
perldoc -q quoting
for more info).
Better to use low-level precedence logical operators for flow control
(as a bonus you can avoid parenthesis).
># following code line doesn't always work on Win OS
># I 'ld suggest to uncomment when on unix:
># flock(W,1) || die "Cant get LOCK_SH on file: $!";
Would this be really required, especially in a minimal example like
this?
>while (<R>)
>{
Quite a weird indenting style, isn't it? Well it's a matter of
personal tastes anyway...
> push @initialfile, $_;
Why on heart should one do so? If you really need to read all of a
file's contents into an array then
@initialfile = <R>;
would be the commonest (and better!) way to do it. But in this kind of
situations one hardly would really want to do so. Perl's typical (and
efficient!) idiom for iterating over all lines of a file is
while (<$file>) {
# ...
}
It's not good to spread awkward, clumsy snippets of code like that
above, instead.
> $alteredline=$_;
> $alteredline=~s/^ {10}$string1/$tenspaces$string2/;
> push @alteredfile, $alteredline;
Not such a great strategy to work on $alteredline, since s/// acts by
default on $_.
More importantly, it's not required in this case, but I'd use
s/^ {10}\Q$string1/$tenspaces$string2/;
in any case.
Also, I'd really use
s/(?=^ {10})\Q$string1/$string2/;
instead.
># write new content to $fileB
Please note that the OP never wrote that he wants to write to $fileB
># small report utility to screen
>print "\n------------------\n";
>print "Initial file:";
>print "\n------------------\n";
>print @initialfile;
>print "\n------------------\n";
>print "Altered file:";
>print "\n------------------\n";
>print @alteredfile;
If I really were to adopt such a "report utility" (which I wouldn't!),
I'd use an here doc:
$"='';
print <<"EOF";
------------------
Initial file:
------------------
@initialfile
------------------
Altered file:
------------------
@alteredfile
EOF
An IMHO better example script along the lines of yours may be:
#!/usr/bin/perl
use strict;
use warnings;
my ($str1, $str2)=qw/XXXX YYYY/;
die "Usage: $0 <infile> <outfile>\n"
unless @ARGV==2;
my $out=pop;
open my $fh, '>', $out or
die "Can't open `$out': $!\n";
select $fh;
s/(?<=^ {10})\Q$str1/$str2/, print while <>;
__END__
But then I guess that the best answer for the OP may be in terms of
the simple one-liner:
perl -pe 's/(?<=^ {10})XXXX/YYYY/' fileA >fileB
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: 3 Jan 2005 12:33:51 -0800
From: ioneabu@yahoo.com
Subject: Re: inheritance and order of packages
Message-Id: <1104784431.388677.90940@c13g2000cwb.googlegroups.com>
I think I am getting the hang of the concept. This is another example
that was in question, but I think it's the same idea. Does this look
right? Before putting in the BEGIN block, @init was recognized as a
lexical but the data was not there when the constructor made use of it.
The BEGIN block solved the problem just like in the previous example.
#!/usr/bin/perl
use warnings;
use strict;
my $a = one->new;
print $a->to_string(),"\n";
package one;
my @init;
BEGIN
{
@init = (one=>1,two=>2);
}
sub new
{
my $class = shift;
my $self = { @init };
bless($self, $class);
return $self;
}
sub to_string
{
my $self = shift;
join '', map {"$_ = ".$self->{$_}."\n"} keys %{$self};
}
output:
one = 1
two = 2
Thanks!
wana
------------------------------
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 7596
***************************************