[31185] in Perl-Users-Digest
Perl-Users Digest, Issue: 2430 Volume: 11
daemon@ATHENA.MIT.EDU (Perl-Users Digest)
Wed May 20 21:09:46 2009
Date: Wed, 20 May 2009 18:09:14 -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 Wed, 20 May 2009 Volume: 11 Number: 2430
Today's topics:
Re: ampersand subroutine <uri@StemSystems.com>
Re: ampersand subroutine <RedGrittyBrick@spamweary.invalid>
Re: ampersand subroutine <kkeller-usenet@wombat.san-francisco.ca.us>
Re: Archive::Zip and correct extension (mixture of Word <gcox@freeuk.com>
Re: Archive::Zip and correct extension (mixture of Word <ben@morrow.me.uk>
Re: Archive::Zip and correct extension (mixture of Word <gcox@freeuk.com>
array and filehandle. <rodbass63@gmail.com>
Re: array and filehandle. <1usa@llenroc.ude.invalid>
Re: array and filehandle. <noreply@gunnar.cc>
Re: array and filehandle. <jurgenex@hotmail.com>
Re: array and filehandle. <uri@StemSystems.com>
Re: array and filehandle. <jurgenex@hotmail.com>
Re: array and filehandle. <ben@morrow.me.uk>
Re: array and filehandle. <ben@morrow.me.uk>
Re: beginner array of array... thank you all <tzz@lifelogs.com>
Re: One-liners: single quotes; altering first line only <tadmc@seesig.invalid>
Re: One-liners: single quotes; altering first line only <hjp-usenet2@hjp.at>
Re: One-liners: single quotes; altering first line only <a24061@ducksburg.com>
Re: One-liners: single quotes; altering first line only <a24061@ducksburg.com>
Re: One-liners: single quotes; altering first line only <a24061@ducksburg.com>
Digest Administrivia (Last modified: 6 Apr 01) (Perl-Users-Digest Admin)
----------------------------------------------------------------------
Date: Wed, 20 May 2009 11:51:00 -0400
From: "Uri Guttman" <uri@StemSystems.com>
Subject: Re: ampersand subroutine
Message-Id: <87pre33h97.fsf@quad.sysarch.com>
>>>>> "SP" == Sherm Pendley <spamtrap@dot-app.org> writes:
SP> News123 <news123@free.fr> writes:
>> In the latter case I'd suggest an explicit exit() statement before the
>> function definitions in order to signal to a person reading the code,
>> that no more code outside of function definitions will follow
SP> If you're communicating to a person, comments are far clearer:
SP> # End main code body
SP> # Function definitions follow
SP> Use exit() for its intended purpose, explicitly returning an exit code
SP> to the parent process.
i disagree. in my top level programs i always put an explicit exit()
after the main line code ends. comments can lie and there could be left
over code below that would execute that shouldn't. i teach this style
for top level programs:
* a small number of key lexical declarations
* up to about 5 or so top level sub calls (maybe storing results
in lexicals)
* then exit().
if you start getting too many top level calls, refactor some into
another call and move that sub after the exit.
other layout points include putting arg parsing, usage and similar
support subs at the very bottom where they are out of the way. put the
subs in a general top down calling order (need not to be
perfect). higher level subs can be followed immediately but some subs
they call. of course, keep sub sizes reasonably small so you can
understand them quickly.
this makes for an easy to read and understand program.
uri
--
Uri Guttman ------ uri@stemsystems.com -------- http://www.sysarch.com --
----- Perl Code Review , Architecture, Development, Training, Support ------
--------- Free Perl Training --- http://perlhunter.com/college.html ---------
--------- Gourmet Hot Cocoa Mix ---- http://bestfriendscocoa.com ---------
------------------------------
Date: Wed, 20 May 2009 17:29:54 +0100
From: RedGrittyBrick <RedGrittyBrick@spamweary.invalid>
Subject: Re: ampersand subroutine
Message-Id: <4a143005$0$24013$db0fefd9@news.zen.co.uk>
Andrew DeFaria wrote:
> I've heard that function prototypes don't always work - yet I find them
> useful. Where do they fail?
If you haven't already read Tom's article[1], you might find it of interest.
[1]
http://groups.google.com/group/comp.lang.perl.modules/msg/84484de5eb01085b
--
RGB
------------------------------
Date: Wed, 20 May 2009 11:05:21 -0700
From: Keith Keller <kkeller-usenet@wombat.san-francisco.ca.us>
Subject: Re: ampersand subroutine
Message-Id: <2ctde6x9rk.ln2@goaway.wombat.san-francisco.ca.us>
On 2009-05-20, RedGrittyBrick <RedGrittyBrick@spamweary.invalid> wrote:
>
> Andrew DeFaria wrote:
>
>> I've heard that function prototypes don't always work - yet I find them
>> useful. Where do they fail?
>
> If you haven't already read Tom's article[1], you might find it of interest.
>
> http://groups.google.com/group/comp.lang.perl.modules/msg/84484de5eb01085b
That was a great read, thanks!
--keith
--
kkeller-usenet@wombat.san-francisco.ca.us
(try just my userid to email me)
AOLSFAQ=http://www.therockgarden.ca/aolsfaq.txt
see X- headers for PGP signature information
------------------------------
Date: Wed, 20 May 2009 21:17:37 +0100
From: Geoff Cox <gcox@freeuk.com>
Subject: Re: Archive::Zip and correct extension (mixture of Word and PowerPoint files)?
Message-Id: <m3p8155787hrth7q7vhn87f6fpsj1itlfa@4ax.com>
On Tue, 19 May 2009 12:23:21 -0700, Jim Gibson <jimsgibson@gmail.com>
wrote:
>In other words, if $_ does not end in '.zip', none of the remaining
>statements are executed for that file.
>
>So you are attempting to process non-zip files (or directories) as a
>zip file, hence the later errors. You need to put something like:
>
> return unless /\.zip$/;
>
>as the first line of your subroutine. With this line, only zip files
>will be processed.
Jim,
I'm confused! The code below does extract the files from the zip files
and creates files with the same base names as the zip file but with
the extension of the file in the zip file.
So if file-1.zip contains fred.xls the new file is called fred-1.xls
etc etc for .doc and .ppt files.
How is it that the line
( my $name = $_ ) =~ s/\.zip$/.doc/i or return;
allows this to work?
Geoff
use warnings;
use strict;
use File::Find;
use Archive::Zip;
my $dir = 'c:/a-temp2/doc-test';
find( sub {
( my $name = $_ ) =~ s/\.zip$/.doc/i or return;
my $zip = Archive::Zip->new( $_ );
my($arch_ext) = ($zip->memberNames)[ 0 ];
$arch_ext =~ s/.*\.//; # strip all but the extension
my $basename = $_;
$basename =~ s/\..*//; # strip off the extension
$zip->extractMember( ($zip->memberNames)[ 0 ],
"$basename.$arch_ext" );
unlink $_ or warn "Cannot delete $_: $!";
}, $dir );
------------------------------
Date: Wed, 20 May 2009 21:34:02 +0100
From: Ben Morrow <ben@morrow.me.uk>
Subject: Re: Archive::Zip and correct extension (mixture of Word and PowerPoint files)?
Message-Id: <q26ee6-li8.ln1@osiris.mauzo.dyndns.org>
Quoth Geoff Cox <gcox@freeuk.com>:
> On Tue, 19 May 2009 12:23:21 -0700, Jim Gibson <jimsgibson@gmail.com>
> wrote:
>
> >In other words, if $_ does not end in '.zip', none of the remaining
> >statements are executed for that file.
> >
> >So you are attempting to process non-zip files (or directories) as a
> >zip file, hence the later errors. You need to put something like:
> >
> > return unless /\.zip$/;
> >
> >as the first line of your subroutine. With this line, only zip files
> >will be processed.
>
> I'm confused! The code below does extract the files from the zip files
> and creates files with the same base names as the zip file but with
> the extension of the file in the zip file.
>
> So if file-1.zip contains fred.xls the new file is called fred-1.xls
> etc etc for .doc and .ppt files.
>
> How is it that the line
>
> ( my $name = $_ ) =~ s/\.zip$/.doc/i or return;
>
> allows this to work?
That line does four things (single statements with multiple effects are
common in idiomatic Perl):
- it copies the string in $_ into a new variable $name;
- it checks if $name matches the pattern /\.zip$/;
- if it does, it replaces the portion that matches with ".doc";
- if it doesn't it returns without doing anything else with this
file.
Since you don't use the variable $name anywhere else in the sub, the
only bits that actually matter are the second and the fourth. You can
get just those two effects with
/\.zip$/ or return;
Ben
------------------------------
Date: Wed, 20 May 2009 23:37:45 +0100
From: Geoff Cox <gcox@freeuk.com>
Subject: Re: Archive::Zip and correct extension (mixture of Word and PowerPoint files)?
Message-Id: <r819151mduque9bsbt0cikopoh0jo6dlai@4ax.com>
On Wed, 20 May 2009 21:34:02 +0100, Ben Morrow <ben@morrow.me.uk>
wrote:
>> How is it that the line
>>
>> ( my $name = $_ ) =~ s/\.zip$/.doc/i or return;
>>
>> allows this to work?
>
>That line does four things (single statements with multiple effects are
>common in idiomatic Perl):
>
> - it copies the string in $_ into a new variable $name;
> - it checks if $name matches the pattern /\.zip$/;
> - if it does, it replaces the portion that matches with ".doc";
> - if it doesn't it returns without doing anything else with this
> file.
>
>Since you don't use the variable $name anywhere else in the sub, the
>only bits that actually matter are the second and the fourth. You can
>get just those two effects with
>
> /\.zip$/ or return;
I finally get it! Since $name not used by the rest of the code this
line just ensures that only .zip files are considered and not folders
etc.
Many thanks Ben!
Cheers
Geoff
>
>Ben
------------------------------
Date: Wed, 20 May 2009 14:27:18 -0700 (PDT)
From: Nene <rodbass63@gmail.com>
Subject: array and filehandle.
Message-Id: <e13780b2-d915-4bc1-9cc0-97851e692a29@r3g2000vbp.googlegroups.com>
Hi,
I would like to turn my array into a filehandle like:
open(FILE, @array);
is that possible?
So I can do:
while (my $line = <FILE>) {
my code goes here....
}
------------------------------
Date: Wed, 20 May 2009 22:35:05 GMT
From: "A. Sinan Unur" <1usa@llenroc.ude.invalid>
Subject: Re: array and filehandle.
Message-Id: <Xns9C11BD0BC950Dasu1cornelledu@127.0.0.1>
Nene <rodbass63@gmail.com> wrote in news:e13780b2-d915-4bc1-9cc0-
97851e692a29@r3g2000vbp.googlegroups.com:
> I would like to turn my array into a filehandle like:
>
> open(FILE, @array);
>
> is that possible?
No, that is not possible. Your question is not very clear.
Do you have an array of filenames and you want to open the corresponding
file for each in turn? Or, do you have a filename and you want to read
the contents of the file in to an array?
> So I can do:
>
> while (my $line = <FILE>) {
> my code goes here....
> }
open my $fh, '<', 'filename'
or die "Cannot open 'filename': $!";
while ( my $line = <$fh> ) {
# do something
}
close $fh;
--
A. Sinan Unur <1usa@llenroc.ude.invalid>
(remove .invalid and reverse each component for email address)
comp.lang.perl.misc guidelines on the WWW:
http://www.rehabitation.com/clpmisc/
------------------------------
Date: Thu, 21 May 2009 00:44:48 +0200
From: Gunnar Hjalmarsson <noreply@gunnar.cc>
Subject: Re: array and filehandle.
Message-Id: <77jffhF1h62epU1@mid.individual.net>
Nene wrote:
> I would like to turn my array into a filehandle like:
>
> open(FILE, @array);
>
> is that possible?
>
> So I can do:
>
> while (my $line = <FILE>) {
> my code goes here....
> }
If you are willing to 'convert' the array to a scalar, you can do:
perl -e '
@array = qw/abc def ghi/;
$scalar = join("\n", @array)."\n";
open FH, "<", \$scalar;
print while <FH>;
'
abc
def
ghi
But why not just loop over the array?
--
Gunnar Hjalmarsson
Email: http://www.gunnar.cc/cgi-bin/contact.pl
------------------------------
Date: Wed, 20 May 2009 15:50:28 -0700
From: Jürgen Exner <jurgenex@hotmail.com>
Subject: Re: array and filehandle.
Message-Id: <7v1915ha7uf3nbk0vm3mdk93005b96rok5@4ax.com>
Nene <rodbass63@gmail.com> wrote:
>I would like to turn my array into a filehandle like:
>
>open(FILE, @array);
>
>is that possible?
I haven't tried it, but if the first element of the array is the mode
and the second the file name, then I don't see why there should be a
problem. At least I would assume that the function call semantics for
Perl's build-in functions are the same as for user-defined functions,
i.e. both flatten the argument list.
>So I can do:
>
>while (my $line = <FILE>) {
>my code goes here....
>}
You can always do that, no matter how you opened the file (well, as long
as you open it for reading, but I guess that's obvious).
jue
------------------------------
Date: Wed, 20 May 2009 18:57:41 -0400
From: "Uri Guttman" <uri@StemSystems.com>
Subject: Re: array and filehandle.
Message-Id: <87hbzftmai.fsf@quad.sysarch.com>
>>>>> "JE" == Jürgen Exner <jurgenex@hotmail.com> writes:
JE> Nene <rodbass63@gmail.com> wrote:
>> I would like to turn my array into a filehandle like:
>>
>> open(FILE, @array);
>>
>> is that possible?
JE> I haven't tried it, but if the first element of the array is the mode
JE> and the second the file name, then I don't see why there should be a
JE> problem. At least I would assume that the function call semantics for
JE> Perl's build-in functions are the same as for user-defined functions,
JE> i.e. both flatten the argument list.
nope. remember many functions can take an array as the first arg so they
actually parse things with prototypes. you can find out the prototype of
any builtin with the function prototype. only those funcs that take a
final list do any slurping (splice, push, etc.) from the args list.
perl -le 'print prototype "CORE::open"'
*;$@
so that means open takes a handle and then an optional scalar (the mode
or file name) and then slurps a list. assuming i interpret the prototype
correctly which could be wrong since i rarely use them. actually my
first real use recently was in a test script where i needed to override
sysread/write with a sub to force an error.
but as others have said, the OP's question is not clear. is it an array
of file names or lines? where is the mode?
uri
--
Uri Guttman ------ uri@stemsystems.com -------- http://www.sysarch.com --
----- Perl Code Review , Architecture, Development, Training, Support ------
--------- Free Perl Training --- http://perlhunter.com/college.html ---------
--------- Gourmet Hot Cocoa Mix ---- http://bestfriendscocoa.com ---------
------------------------------
Date: Wed, 20 May 2009 16:11:05 -0700
From: Jürgen Exner <jurgenex@hotmail.com>
Subject: Re: array and filehandle.
Message-Id: <8f39151oo9up2j03lbqckdcednso5srn6e@4ax.com>
"Uri Guttman" <uri@StemSystems.com> wrote:
>>>>>> "JE" == Jürgen Exner <jurgenex@hotmail.com> writes:
>
> JE> Nene <rodbass63@gmail.com> wrote:
> >> I would like to turn my array into a filehandle like:
> >>
> >> open(FILE, @array);
> >>
> >> is that possible?
>
> JE> I haven't tried it, but if the first element of the array is the mode
> JE> and the second the file name, then I don't see why there should be a
> JE> problem. At least I would assume that the function call semantics for
> JE> Perl's build-in functions are the same as for user-defined functions,
> JE> i.e. both flatten the argument list.
>
>nope. remember many functions can take an array as the first arg so they
>actually parse things with prototypes. you can find out the prototype of
>any builtin with the function prototype. only those funcs that take a
>final list do any slurping (splice, push, etc.) from the args list.
Ah, of course, you right.
Thanks
jue
------------------------------
Date: Thu, 21 May 2009 00:13:33 +0100
From: Ben Morrow <ben@morrow.me.uk>
Subject: Re: array and filehandle.
Message-Id: <tdfee6-bc9.ln1@osiris.mauzo.dyndns.org>
Quoth Gunnar Hjalmarsson <noreply@gunnar.cc>:
> Nene wrote:
> > I would like to turn my array into a filehandle like:
> >
> > open(FILE, @array);
> >
> > is that possible?
> >
> > So I can do:
> >
> > while (my $line = <FILE>) {
> > my code goes here....
> > }
>
> If you are willing to 'convert' the array to a scalar, you can do:
>
> perl -e '
> @array = qw/abc def ghi/;
> $scalar = join("\n", @array)."\n";
> open FH, "<", \$scalar;
> print while <FH>;
> '
> abc
> def
> ghi
That's in general not possible, since there is no sequence of characters
to use for $/ which is guaranteed not to appear in the array. (Well, I
suppose since it's a finite array of finite-length strings you could
apply Cantor's Diagonal Argument, but that's likely to come up with
rather a long separator :) ).
> But why not just loop over the array?
One reason might be because s?he has a function that wants to be passed
a filehandle. If it's willing to accept something that merely looks a
bit like a filehandle, and only responds to <>, then this
{
package IO::Fake::FromArray;
use overload "<>" => sub {
my ($self) = @_;
@$self or return;
if (wantarray) {
my @tmp = @$self;
@$self = ();
return @tmp;
}
return shift @$self;
};
sub new { bless $_[1], $_[0] }
}
my @ary = qw/a b c/;
my $fake_fh = IO::Fake::FromArray->new(\@ary);
might do the trick. Note that it will destroy @ary as it is read. There
is probably a module on CPAN that does a better job along these lines,
if you look. Alternatively, you could implement a 'real' filehandle with
tie or PerlIO::via (TMTOWTDI, and when it comes to fake filehandles,
there are *far* too many, none of which are entirely satisfactory).
Ben
------------------------------
Date: Thu, 21 May 2009 00:24:17 +0100
From: Ben Morrow <ben@morrow.me.uk>
Subject: Re: array and filehandle.
Message-Id: <12gee6-bc9.ln1@osiris.mauzo.dyndns.org>
Quoth "Uri Guttman" <uri@StemSystems.com>:
>
> nope. remember many functions can take an array as the first arg so they
> actually parse things with prototypes. you can find out the prototype of
> any builtin with the function prototype. only those funcs that take a
> final list do any slurping (splice, push, etc.) from the args list.
It's important to realise that all the builtins are actually parsed
specially, and many cannot be represented by prototypes, even some of
those that claim to have them. The most obvious example is 'eof' (which
cannot be reimplemented in Perl at all), but there are others like
'stat' and 'chdir' which take a filehandle or a string and get hints
from the parser about which was passed which a user function cannot
access.
Ben
------------------------------
Date: Wed, 20 May 2009 11:30:28 -0500
From: Ted Zlatanov <tzz@lifelogs.com>
Subject: Re: beginner array of array... thank you all
Message-Id: <868wkr68kb.fsf@lifelogs.com>
On Tue, 19 May 2009 14:49:25 -0400 "Uri Guttman" <uri@PerlOnCall.com> wrote:
>>>>>> "TZ" == Ted Zlatanov <tzz@lifelogs.com> writes:
TZ> On Mon, 18 May 2009 18:46:11 -0400 "Uri Guttman" <uri@PerlOnCall.com> wrote:
UG> you should learn it now. indexing fields by number and not name is prone
UG> to bugs and misunderstanding later. this will bite you one day for sure
UG> so learn hashes now.
TZ> Arrays use less memory, which can become important for some
TZ> applications. If I face that situation, I usually use constants to
TZ> reduce the chance of error:
TZ> use constant NAME => 0;
TZ> use constant SURNAME => 1;
UG> and that will bite you even harder when you need to add/delete/change
UG> keys. trust me, i have seen it. if you want proof, look at the modules
UG> which give ordering to hashes. all have major fails when things change.
Assuming you need to modify the key order, yes, this is a bad idea. But
there are many cases where the key order does not need to be modified.
For example, do you expect the TCP packet fields to get reordered often?
getpwent() fields?
UG> if you are ram sensitive, usually you can find other ways to save space
UG> other than using arrays over hashes.
Perhaps, this really depends on the problem. Let's not forget array
lookups are significantly faster than hash lookups. I would rather
evaluate on a case-by-case basis than make a overly general statement.
Ted
------------------------------
Date: Wed, 20 May 2009 08:17:28 -0500
From: Tad J McClellan <tadmc@seesig.invalid>
Subject: Re: One-liners: single quotes; altering first line only; printing the changes?
Message-Id: <slrnh180n8.l1u.tadmc@tadmc30.sbcglobal.net>
Adam Funk <a24061@ducksburg.com> wrote:
> I had some very large RDF-XML files that had been incorrectly
> generated with the prolog
>
><?xml version='1.0' encoding='UTF8'?>
>
> which I wanted to change to
>
><?xml version='1.0' encoding='UTF-8'?>
>
> so I used the following command.
>
> perl -pi.bak -e 's!^(<\?xml version=.1\.0. encoding=.)UTF8(.\?\>)!\1UTF-8\2!' *.rdf
>
>
> It worked, but I have three questions about doing it better.
>
> 1. Is there any way to specify single quotes (') in the pattern? (I
> realize this is at least as much of a shell problem as a Perl
> problem; this is in bash on GNU/Linux.)
Single quotes won't bother the shell if you use double quotes on
the argument instead of single quotes.
Most people stick with slash for the delimiter unless slash is
part of their pattern.
perl -pi.bak -e "s/\Q<?xml version='1.0' encoding='UTF8'?>/<?xml version='1.0' encoding='UTF-8'?>/"
You should use $1 instead of \1 in replacement strings.
You could use an escape sequence for the single quote character
(see "Quote and Quote-like Operators" in perlop):
perl -pi.bak -e 's!^(<\?xml version=\x271\.0\x27 encoding=\x27)UTF8(\x27\?\>)!$1UTF-8$2!'
> 2. Is it possible to tell the command to look at the first line of
> each file only? (These were very large files.)
That depends on what you mean by "look at".
If you mean: only attempt the s/// on the 1st line,
then yes, that is easy to do:
s/// if $. == 1;
If you mean: process only the 1st line, then no, you can't do that
in conjunction with in-place editing or you'd end up with a 1-line file.
> 3. Is it possible to make a perl -i command print to STDOUT the
> changes it makes (and only the changed lines)?
Not in the general case, but you can for this particular case:
perl -pi.bak -e "print STDOUT if s/\Q<?xml version='1.0' encoding='UTF8'?>/<?xml version='1.0' encoding='UTF-8'?>/"
--
Tad McClellan
email: perl -le "print scalar reverse qq/moc.noitatibaher\100cmdat/"
------------------------------
Date: Wed, 20 May 2009 16:54:25 +0200
From: "Peter J. Holzer" <hjp-usenet2@hjp.at>
Subject: Re: One-liners: single quotes; altering first line only; printing the changes?
Message-Id: <slrnh186d2.6ml.hjp-usenet2@hrunkner.hjp.at>
On 2009-05-20 11:20, Adam Funk <a24061@ducksburg.com> wrote:
> perl -pi.bak -e 's!^(<\?xml version=.1\.0. encoding=.)UTF8(.\?\>)!\1UTF-8\2!' *.rdf
>
>
> It worked, but I have three questions about doing it better.
>
> 1. Is there any way to specify single quotes (') in the pattern? (I
> realize this is at least as much of a shell problem as a Perl
> problem; this is in bash on GNU/Linux.)
\x{27} (or \047 if you prefer to think in octal) should work.
> 2. Is it possible to tell the command to look at the first line of
> each file only? (These were very large files.)
You have to read and copy the whole file to change a line in it - that's
unavoidable. You can only do the substitution on line 1 with something
like
$. == 1 and s!...!...!
I'm not sure if this is much of s speedup.
> 3. Is it possible to make a perl -i command print to STDOUT the
> changes it makes (and only the changed lines)?
I haven't tested it, but the docs say that the output file is
"selected". I take that to mean that STDOUT is unchanged and
print STDOUT "whatever"
should do what you want.
hp
------------------------------
Date: Wed, 20 May 2009 19:56:42 +0100
From: Adam Funk <a24061@ducksburg.com>
Subject: Re: One-liners: single quotes; altering first line only; printing the changes?
Message-Id: <ac0ee6xtsc.ln2@news.ducksburg.com>
On 2009-05-20, John W. Krahn wrote:
> Adam Funk wrote:
>> perl -pi.bak -e 's!^(<\?xml version=.1\.0. encoding=.)UTF8(.\?\>)!\1UTF-8\2!' *.rdf
>
> You should use $1 and $2 in the replacement string instead of \1 and \2.
>
>
>> It worked, but I have three questions about doing it better.
>>
>> 1. Is there any way to specify single quotes (') in the pattern? (I
>> realize this is at least as much of a shell problem as a Perl
>> problem; this is in bash on GNU/Linux.)
>
> $ perl -le'print "\047" x 10'
> ''''''''''
Aha, thanks!
--
It is probable that television drama of high caliber and produced by
first-rate artists will materially raise the level of dramatic taste
of the nation. (David Sarnoff, CEO of RCA, 1939; in Stoll 1995)
------------------------------
Date: Wed, 20 May 2009 19:59:46 +0100
From: Adam Funk <a24061@ducksburg.com>
Subject: Re: One-liners: single quotes; altering first line only; printing the changes?
Message-Id: <2i0ee6xtsc.ln2@news.ducksburg.com>
On 2009-05-20, Tad J McClellan wrote:
> Single quotes won't bother the shell if you use double quotes on
> the argument instead of single quotes.
They sure wouldn't work for me.
> Most people stick with slash for the delimiter unless slash is
> part of their pattern.
>
> perl -pi.bak -e "s/\Q<?xml version='1.0' encoding='UTF8'?>/<?xml version='1.0' encoding='UTF-8'?>/"
I know. I tend to use "!" with XML files because I often need the "/"
in the "<.../>".
> You should use $1 instead of \1 in replacement strings.
[reminds himself again]
> You could use an escape sequence for the single quote character
> (see "Quote and Quote-like Operators" in perlop):
>
> perl -pi.bak -e 's!^(<\?xml version=\x271\.0\x27 encoding=\x27)UTF8(\x27\?\>)!$1UTF-8$2!'
Thanks.
>> 2. Is it possible to tell the command to look at the first line of
>> each file only? (These were very large files.)
>
>
> That depends on what you mean by "look at".
>
> If you mean: only attempt the s/// on the 1st line,
> then yes, that is easy to do:
>
> s/// if $. == 1;
That's what I mean. If I'd thought of that, I wouldn't have
conglomerated such a long substitution pattern (and felt the need to
check that the file sizes all differed by 1 afterwards).
>> 3. Is it possible to make a perl -i command print to STDOUT the
>> changes it makes (and only the changed lines)?
>
>
> Not in the general case, but you can for this particular case:
>
> perl -pi.bak -e "print STDOUT if s/\Q<?xml version='1.0' encoding='UTF8'?>/<?xml version='1.0' encoding='UTF-8'?>/"
I'll take a look at that. Thanks.
--
When Elaine turned 11, her mother sent her to train under
Donald Knuth in his mountain hideaway. [XKCD 342]
------------------------------
Date: Wed, 20 May 2009 20:01:18 +0100
From: Adam Funk <a24061@ducksburg.com>
Subject: Re: One-liners: single quotes; altering first line only; printing the changes?
Message-Id: <uk0ee6xtsc.ln2@news.ducksburg.com>
On 2009-05-20, Peter J. Holzer wrote:
> On 2009-05-20 11:20, Adam Funk <a24061@ducksburg.com> wrote:
>> perl -pi.bak -e 's!^(<\?xml version=.1\.0. encoding=.)UTF8(.\?\>)!\1UTF-8\2!' *.rdf
>>
>>
>> It worked, but I have three questions about doing it better.
>>
>> 1. Is there any way to specify single quotes (') in the pattern? (I
>> realize this is at least as much of a shell problem as a Perl
>> problem; this is in bash on GNU/Linux.)
>
> \x{27} (or \047 if you prefer to think in octal) should work.
Right, thanks.
>> 2. Is it possible to tell the command to look at the first line of
>> each file only? (These were very large files.)
>
> You have to read and copy the whole file to change a line in it - that's
> unavoidable. You can only do the substitution on line 1 with something
> like
>
> $. == 1 and s!...!...!
>
> I'm not sure if this is much of s speedup.
I'd expect it to speed things up a bit, since the first test should be
faster than the regex-matching. It would also eliminate the risk of
changing anything past the XML prolog.
--
It is probable that television drama of high caliber and produced by
first-rate artists will materially raise the level of dramatic taste
of the nation. (David Sarnoff, CEO of RCA, 1939; in Stoll 1995)
------------------------------
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 2430
***************************************