[29000] in Perl-Users-Digest
Perl-Users Digest, Issue: 244 Volume: 11
daemon@ATHENA.MIT.EDU (Perl-Users Digest)
Tue Mar 20 18:14:29 2007
Date: Tue, 20 Mar 2007 15:14:22 -0700 (PDT)
From: Perl-Users Digest <Perl-Users-Request@ruby.OCE.ORST.EDU>
To: Perl-Users@ruby.OCE.ORST.EDU (Perl-Users Digest)
Perl-Users Digest Tue, 20 Mar 2007 Volume: 11 Number: 244
Today's topics:
Re: perl: adding lines and replacing stings <mritty@gmail.com>
Re: perl: adding lines and replacing stings <mritty@gmail.com>
Re: perl: adding lines and replacing stings <ben@morrow.me.uk>
Re: perl: adding lines and replacing stings <uri@stemsystems.com>
Re: perl: adding lines and replacing stings <someone@example.com>
Re: Server/Clients system <hjp-usenet2@hjp.at>
Re: Server/Clients system <deadpickle@gmail.com>
Re: Server/Clients system <deadpickle@gmail.com>
Digest Administrivia (Last modified: 6 Apr 01) (Perl-Users-Digest Admin)
----------------------------------------------------------------------
Date: 20 Mar 2007 11:20:16 -0700
From: "Paul Lalli" <mritty@gmail.com>
Subject: Re: perl: adding lines and replacing stings
Message-Id: <1174414816.471386.261540@y66g2000hsf.googlegroups.com>
On Mar 20, 1:20 pm, Uri Guttman <u...@stemsystems.com> wrote:
> >>>>> "PL" == Paul Lalli <mri...@gmail.com> writes:
>
> PL> Gah. You are suggesting the OP read the entire file into memory,
> PL> modify the array, and then print the entire array back to the file?
> PL> Please don't do this. Ever.
>
> why not? if the file is small enough (and small is pretty big by today's
> ram standards) it is simpler and faster to slurp in many cases.
Because far too many people program via the "copy-and-paste" method,
rather than the "think" method, and when they see a piece of code that
modifies a file by slurping it, they won't stop to think that the
method isn't as valid for their situation just because their file is
obscenely larger.
I just don't see the point of using a method that's only sometimes
valid as opposed to using one that's always valid.
Paul Lalli
------------------------------
Date: 20 Mar 2007 11:22:46 -0700
From: "Paul Lalli" <mritty@gmail.com>
Subject: Re: perl: adding lines and replacing stings
Message-Id: <1174414966.212652.266820@y66g2000hsf.googlegroups.com>
On Mar 20, 12:29 pm, Ben Morrow <b...@morrow.me.uk> wrote:
> Quoth "erobinson32" <samdani...@gmail.com>:
>
>
>
>
>
>
>
> > I would like to do three things in a single Perl script:
> > 1. Add the text "FirstLine" to the very first line of a sample file.
> > 2. Add the test "LastLine" to the very last line of a sample file.
> > 3. Replace all of the instances of 'California' to 'Nevada' in a file.
>
> > I've just been running a few commands to accomplish this, but would
> > like to simplify the process:
>
> > perl -pi -e 's/California/Nevada/g' testfile
> > sed '1i\
> > FirstLine' testfile > temp_file
> > mv temp_file testfile
> > sed '$a\
> > LastLine' testfile > temp_file
> > mv temp_file testfile
>
> You're nearly there :)
>
> perl -pi -le'
> BEGIN { print "FirstLine" }
> s/California/Nevada/g;
> END { print "LastLine" }'
>
Uhm, you're not, unfortunately. :-P Did you actually try this? The -
i feature takes affect only during the while(<>) {} loop created by -
p, and BEGIN{} and END{} blocks happen outside that loop. End result
- the two blocks print to STDOUT rather than the file.
Paul Lalli
------------------------------
Date: Tue, 20 Mar 2007 18:53:36 +0000
From: Ben Morrow <ben@morrow.me.uk>
Subject: Re: perl: adding lines and replacing stings
Message-Id: <g6n5d4-pcb.ln1@osiris.mauzo.dyndns.org>
Quoth "Paul Lalli" <mritty@gmail.com>:
> On Mar 20, 12:29 pm, Ben Morrow <b...@morrow.me.uk> wrote:
> > Quoth "erobinson32" <samdani...@gmail.com>:
> >
> > > I would like to do three things in a single Perl script:
> > > 1. Add the text "FirstLine" to the very first line of a sample file.
> > > 2. Add the test "LastLine" to the very last line of a sample file.
> > > 3. Replace all of the instances of 'California' to 'Nevada' in a file.
> >
> > You're nearly there :)
> >
> > perl -pi -le'
> > BEGIN { print "FirstLine" }
> > s/California/Nevada/g;
> > END { print "LastLine" }'
> >
>
> Uhm, you're not, unfortunately. :-P Did you actually try this?
Well, clearly not. Sorry about that :(.
> The -i feature takes affect only during the while(<>) {} loop created
> by -p, and BEGIN{} and END{} blocks happen outside that loop. End
> result - the two blocks print to STDOUT rather than the file.
Yes, of course... and there I thought I was being so clever :(. Ach
well.
Ben
--
#!/bin/sh
quine="echo 'eval \$quine' >> \$0; echo quined"
eval $quine
# [ben@morrow.me.uk]
------------------------------
Date: Tue, 20 Mar 2007 14:31:24 -0500
From: Uri Guttman <uri@stemsystems.com>
Subject: Re: perl: adding lines and replacing stings
Message-Id: <x7vegvlnjn.fsf@mail.sysarch.com>
>>>>> "PL" == Paul Lalli <mritty@gmail.com> writes:
PL> On Mar 20, 1:20 pm, Uri Guttman <u...@stemsystems.com> wrote:
>> >>>>> "PL" == Paul Lalli <mri...@gmail.com> writes:
>>
PL> Gah. You are suggesting the OP read the entire file into memory,
PL> modify the array, and then print the entire array back to the file?
PL> Please don't do this. Ever.
>>
>> why not? if the file is small enough (and small is pretty big by today's
>> ram standards) it is simpler and faster to slurp in many cases.
PL> Because far too many people program via the "copy-and-paste" method,
PL> rather than the "think" method, and when they see a piece of code that
PL> modifies a file by slurping it, they won't stop to think that the
PL> method isn't as valid for their situation just because their file is
PL> obscenely larger.
PL> I just don't see the point of using a method that's only sometimes
PL> valid as opposed to using one that's always valid.
for those who do think and like/need speed and in many cases simplicity,
it is more than just valid. like i said teaching line by line is good
but not the only nor always the best way.
and the determination of obscenely large is very vague. most text files
that need processing are articles, code source, config files, etc. the
large ones are notably logs and genetic data. so in most cases it is
pretty easy to know if slurping will work fine. you general know the
type of file you are processing unless you are making a truly general
program like grep.
uri
--
Uri Guttman ------ uri@stemsystems.com -------- http://www.stemsystems.com
--Perl Consulting, Stem Development, Systems Architecture, Design and Coding-
Search or Offer Perl Jobs ---------------------------- http://jobs.perl.org
------------------------------
Date: Tue, 20 Mar 2007 21:50:15 GMT
From: "John W. Krahn" <someone@example.com>
Subject: Re: perl: adding lines and replacing stings
Message-Id: <rGYLh.67598$lY6.59391@edtnps90>
Uri Guttman wrote:
>>>>>>"PL" == Paul Lalli <mritty@gmail.com> writes:
>
> >> perldoc -f push ( to add a last line to your existing file)..
>
> PL> Gah. You are suggesting the OP read the entire file into memory,
> PL> modify the array, and then print the entire array back to the file?
> PL> Please don't do this. Ever.
>
> why not? if the file is small enough (and small is pretty big by today's
> ram standards) it is simpler and faster to slurp in many cases. this
> could be done with:
>
> use File::Slurp ;
>
> my $text = read_file( 'file' ) ;
> $text =~ s/Nevada/California/g ;
> write_file( 'file', "FirstLine\n", $text, "LastLine\n" ) ;
perl -i -0777ne's/Nevada/California/g; print "FirstLine\n${_}LastLine\n"'
John
--
Perl isn't a toolbox, but a small machine shop where you can special-order
certain sorts of tools at low cost and in short order. -- Larry Wall
------------------------------
Date: Tue, 20 Mar 2007 19:48:55 +0100
From: "Peter J. Holzer" <hjp-usenet2@hjp.at>
Subject: Re: Server/Clients system
Message-Id: <slrnf00b4n.l87.hjp-usenet2@yoyo.hjp.at>
On 2007-03-20 17:20, deadpickle <deadpickle@gmail.com> wrote:
> On Mar 20, 9:23 am, Ted Zlatanov <t...@lifelogs.com> wrote:
>> On 20 Mar 2007 07:04:30 -0700 "deadpickle" <deadpic...@gmail.com> wrote:
>> d> First of all I havent wrote any code for this yet, I'm still in the
>> d> brainstorming section. What I want to do is have a server that resides
>> d> on a networked computer somewhere. This server will recieve files from
>> d> 2 clients. Then these 2 clients will ask the server for another file
>> d> and the server will send it. So in summary I have 2 Clients that can
>> d> send and recieve files and a Server that can recieve and send files.
>> d> Hope that makes sense. I am not sure on how to do this, or how to get
>> d> started, anyone got any ideas?
>>
>> Well, you could implement this with FTP or HTTP (HTTP has a "PUT"
>> command), reimplementing as much of the protocol as you desire. Are
>> you trying to implement something new as a fun project, or is this
>> real work? In the real world I'd avoid writing new protocols when so
>> many good ones exist already (implemented in C, bug-free, etc.).
[...]
> This is for a undergraduate project at my university. I'm thinking
> about using BitTorrent. I want this whole system to be autonomous and
> to make many transfers every minute, can BitTorrent do this?
BitTorrent doesn't sound like a good choice: Firstly, it doesn't have an
upload capability (AFAIK). Secondly, it isn't designed to transfer many
small files between a server and a small number of clients - it is
designed to distribute large files to to a large number of clients.
Writing a bittorrent implementation might be fun and instructive, but
for your (stated) needs it sounds like overkill. HTTP is probably the
simplest protocol for that purpose (as long as you don't implement all
of RFC 2616).
hp
--
_ | Peter J. Holzer | Blaming Perl for the inability of programmers
|_|_) | Sysadmin WSR | to write clearly is like blaming English for
| | | hjp@hjp.at | the circumlocutions of bureaucrats.
__/ | http://www.hjp.at/ | -- Charlton Wilbur in clpm
------------------------------
Date: 20 Mar 2007 12:05:30 -0700
From: "deadpickle" <deadpickle@gmail.com>
Subject: Re: Server/Clients system
Message-Id: <1174417530.124817.24760@l75g2000hse.googlegroups.com>
On Mar 20, 1:48 pm, "Peter J. Holzer" <hjp-usen...@hjp.at> wrote:
> On 2007-03-20 17:20, deadpickle <deadpic...@gmail.com> wrote:
>
>
>
>
>
> > On Mar 20, 9:23 am, Ted Zlatanov <t...@lifelogs.com> wrote:
> >> On 20 Mar 2007 07:04:30 -0700 "deadpickle" <deadpic...@gmail.com> wrote:
> >> d> First of all I havent wrote any code for this yet, I'm still in the
> >> d> brainstorming section. What I want to do is have a server that resides
> >> d> on a networked computer somewhere. This server will recieve files from
> >> d> 2 clients. Then these 2 clients will ask the server for another file
> >> d> and the server will send it. So in summary I have 2 Clients that can
> >> d> send and recieve files and a Server that can recieve and send files.
> >> d> Hope that makes sense. I am not sure on how to do this, or how to get
> >> d> started, anyone got any ideas?
>
> >> Well, you could implement this with FTP or HTTP (HTTP has a "PUT"
> >> command), reimplementing as much of the protocol as you desire. Are
> >> you trying to implement something new as a fun project, or is this
> >> real work? In the real world I'd avoid writing new protocols when so
> >> many good ones exist already (implemented in C, bug-free, etc.).
> [...]
> > This is for a undergraduate project at my university. I'm thinking
> > about using BitTorrent. I want this whole system to be autonomous and
> > to make many transfers every minute, can BitTorrent do this?
>
> BitTorrent doesn't sound like a good choice: Firstly, it doesn't have an
> upload capability (AFAIK). Secondly, it isn't designed to transfer many
> small files between a server and a small number of clients - it is
> designed to distribute large files to to a large number of clients.
>
> Writing a bittorrent implementation might be fun and instructive, but
> for your (stated) needs it sounds like overkill. HTTP is probably the
> simplest protocol for that purpose (as long as you don't implement all
> of RFC 2616).
>
> hp
>
> --
> _ | Peter J. Holzer | Blaming Perl for the inability of programmers
> |_|_) | Sysadmin WSR | to write clearly is like blaming English for
> | | | h...@hjp.at | the circumlocutions of bureaucrats.
> __/ |http://www.hjp.at/| -- Charlton Wilbur in clpm- Hide quoted text -
>
> - Show quoted text -
How about SFTP. It seems prity easy to write a script in perl.
------------------------------
Date: 20 Mar 2007 12:10:43 -0700
From: "deadpickle" <deadpickle@gmail.com>
Subject: Re: Server/Clients system
Message-Id: <1174417843.117866.189200@e65g2000hsc.googlegroups.com>
On Mar 20, 2:05 pm, "deadpickle" <deadpic...@gmail.com> wrote:
> On Mar 20, 1:48 pm, "Peter J. Holzer" <hjp-usen...@hjp.at> wrote:
>
>
>
>
>
> > On 2007-03-20 17:20, deadpickle <deadpic...@gmail.com> wrote:
>
> > > On Mar 20, 9:23 am, Ted Zlatanov <t...@lifelogs.com> wrote:
> > >> On 20 Mar 2007 07:04:30 -0700 "deadpickle" <deadpic...@gmail.com> wrote:
> > >> d> First of all I havent wrote any code for this yet, I'm still in the
> > >> d> brainstorming section. What I want to do is have a server that resides
> > >> d> on a networked computer somewhere. This server will recieve files from
> > >> d> 2 clients. Then these 2 clients will ask the server for another file
> > >> d> and the server will send it. So in summary I have 2 Clients that can
> > >> d> send and recieve files and a Server that can recieve and send files.
> > >> d> Hope that makes sense. I am not sure on how to do this, or how to get
> > >> d> started, anyone got any ideas?
>
> > >> Well, you could implement this with FTP or HTTP (HTTP has a "PUT"
> > >> command), reimplementing as much of the protocol as you desire. Are
> > >> you trying to implement something new as a fun project, or is this
> > >> real work? In the real world I'd avoid writing new protocols when so
> > >> many good ones exist already (implemented in C, bug-free, etc.).
> > [...]
> > > This is for a undergraduate project at my university. I'm thinking
> > > about using BitTorrent. I want this whole system to be autonomous and
> > > to make many transfers every minute, can BitTorrent do this?
>
> > BitTorrent doesn't sound like a good choice: Firstly, it doesn't have an
> > upload capability (AFAIK). Secondly, it isn't designed to transfer many
> > small files between a server and a small number of clients - it is
> > designed to distribute large files to to a large number of clients.
>
> > Writing a bittorrent implementation might be fun and instructive, but
> > for your (stated) needs it sounds like overkill. HTTP is probably the
> > simplest protocol for that purpose (as long as you don't implement all
> > of RFC 2616).
>
> > hp
>
> > --
> > _ | Peter J. Holzer | Blaming Perl for the inability of programmers
> > |_|_) | Sysadmin WSR | to write clearly is like blaming English for
> > | | | h...@hjp.at | the circumlocutions of bureaucrats.
> > __/ |http://www.hjp.at/| -- Charlton Wilbur in clpm- Hide quoted text -
>
> > - Show quoted text -
>
> How about SFTP. It seems prity easy to write a script in perl.- Hide quoted text -
>
> - Show quoted text -
Also, how can I use SFTP on a windows machine?
------------------------------
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 244
**************************************