[25279] in Perl-Users-Digest
Perl-Users Digest, Issue: 7524 Volume: 10
daemon@ATHENA.MIT.EDU (Perl-Users Digest)
Thu Dec 16 09:06:01 2004
Date: Thu, 16 Dec 2004 06:05:08 -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 Thu, 16 Dec 2004 Volume: 10 Number: 7524
Today's topics:
Re: (quickie) (Aliasing) Possible to get this to work i <tassilo.von.parseval@rwth-aachen.de>
Re: (quickie) (Aliasing) Possible to get this to work i <do-not-use@invalid.net>
Re: (quickie) (Aliasing) Possible to get this to work i <tassilo.von.parseval@rwth-aachen.de>
Re: (quickie) (Aliasing) Possible to get this to work i <do-not-use@invalid.net>
Re: (quickie) (Aliasing) Possible to get this to work i (Anno Siegel)
Re: (quickie) (Aliasing) Possible to get this to work i (Peter Scott)
Re: Can't get past 'use strict' :( <webmaster @ infusedlight.net>
Re: Can't get past 'use strict' :( <wyzelli@yahoo.com>
Re: Can't get past 'use strict' :( <_karlo_@_mosor.net_>
Re: Can't get past 'use strict' :( <tadmc@augustmail.com>
Re: Extracting part of strings? <wyzelli@yahoo.com>
Re: Follow-up:go to a link, save the page and email it (Anno Siegel)
Installing DBI module on Solaris 8 <nani3skip45@hotmail.com>
Re: Open file, print into file, what variable is used? (Anno Siegel)
Re: Regarding ISA and Inheritance <spamtrap@dot-app.org>
Re: Regarding ISA and Inheritance <spamtrap@dot-app.org>
replace multiple lines in a file with Perl <tzhai2002@yahoo.com>
Re: replace multiple lines in a file with Perl <phaylon@dunkelheit.at>
Re: replace multiple lines in a file with Perl <do-not-use@invalid.net>
Re: replace multiple lines in a file with Perl <tadmc@augustmail.com>
Re: Session management for cgi, ldap, oracle? <tintin@invalid.invalid>
Re: use external files as "HERE-docs" (and avoid the un <abrey@gmx.net>
Re: use external files as "HERE-docs" (and avoid the un <abrey@gmx.net>
Digest Administrivia (Last modified: 6 Apr 01) (Perl-Users-Digest Admin)
----------------------------------------------------------------------
Date: Thu, 16 Dec 2004 10:03:13 +0100
From: "Tassilo v. Parseval" <tassilo.von.parseval@rwth-aachen.de>
Subject: Re: (quickie) (Aliasing) Possible to get this to work in strict?
Message-Id: <slrncs2jqh.qv.tassilo.von.parseval@localhost.localdomain>
Also sprach Michele Dondi:
> On Tue, 14 Dec 2004 09:54:16 +0100, "Tassilo v. Parseval"
><tassilo.von.parseval@rwth-aachen.de> wrote:
>
>>You can circumvent that by pre-declaring $r using our() or 'use vars':
>>
>> use strict;
>>
>> our $r; # pre-5.6.0 perls: use vars qw/$r/;
>> my $n = 5;
>>
>> *r = \$n;
>> $r++;
>> print "$n - $r\n";
>> __END__
>> 6 - 6
>
> Speaking of which, may I ask you to give an explanation of how this
> works internally? (Just curious...)
It probably means that on the inside just the pointer to the original
variable is copied. On ordinary assignments, perl copies the C structure
into a new variable. Here it just copies the memory address.
> What I mean is that we all know that Perl5 has basically two
> fundamentally orthogonal variable systems. And we know we can access
> directly a package's symbol table, but AFAIK we can't do the same with
> lexical variables, even though there "MUST" be one, and given the
> orthogonality hinted to above, it is somewhat surprising that such an
> aliasing can even be done. Oh, and of course on lexical scope exit it
> will "downgrade" to a simple copy, won't it? (Just tried, and AFAICT
> it is indeed so).
No, an alias remains an alias. Perl increments the reference-count of
the variable to be aliased:
ethan@ethan:~$ perl -MDevel::Peek
{ my $c = 42;
Dump($c);
*alias = \$c;
Dump($alias);
}
Dump($alias);
__END__
SV = IV(0x8163094) at 0x814cc6c
REFCNT = 1
FLAGS = (PADBUSY,PADMY,IOK,pIOK)
IV = 42
SV = IV(0x8163094) at 0x814cc6c
REFCNT = 2
FLAGS = (PADBUSY,PADMY,IOK,pIOK)
IV = 42
SV = IV(0x8163094) at 0x814cc6c
REFCNT = 1
FLAGS = (PADBUSY,PADMY,IOK,pIOK)
IV = 42
So after doing the aliasing, the reference-count has gone up to 2. On
scope exist, it is decreased again by one. Since the reference-count
hasn't yet gone to zero, the memory previously associated with 'my $c'
remains intact and can still be accessed through the alias. Also note
how the memory addresses involved are all identical. My suspicion is
that aliases were just a side-effect that turned out to be very useful.
It is the C-semantics of copying a pointer ported to perl.
Aliases are a lot like hard links on Unices. You can delete the file
that was hard-linked to but the hard link itself remains valid and
behaves if it was the original file. Which it is anyway, from the point
of view of the filesystem.
As for lexical variables, technically they are not very different from
package variables. They have the PADMY flag set which means they are
subject to refcounting. Other than that, they only differ from package
variables in that they are not kept in a symbol-table but in so called
pads. Each block has its own pad and they are inaccessible from
pure-Perl (unless someone writes an XS extension that makes them
accessible).
Tassilo
--
$_=q#",}])!JAPH!qq(tsuJ[{@"tnirp}3..0}_$;//::niam/s~=)]3[))_$-3(rellac(=_$({
pam{rekcahbus})(rekcah{lrePbus})(lreP{rehtonabus})!JAPH!qq(rehtona{tsuJbus#;
$_=reverse,s+(?<=sub).+q#q!'"qq.\t$&."'!#+sexisexiixesixeseg;y~\n~~dddd;eval
------------------------------
Date: 16 Dec 2004 10:43:13 +0100
From: Arndt Jonasson <do-not-use@invalid.net>
Subject: Re: (quickie) (Aliasing) Possible to get this to work in strict?
Message-Id: <yzd6532lgsu.fsf@invalid.net>
"Tassilo v. Parseval" <tassilo.von.parseval@rwth-aachen.de> writes:
> As for lexical variables, technically they are not very different from
> package variables. They have the PADMY flag set which means they are
> subject to refcounting. Other than that, they only differ from package
> variables in that they are not kept in a symbol-table but in so called
> pads. Each block has its own pad and they are inaccessible from
> pure-Perl (unless someone writes an XS extension that makes them
> accessible).
Maybe I misunderstand the above, but it seems to say that in the code
snippet below, the lexical variable $x should not be seen by the
'eval' inside the block. But the output in the block is "r = 7" (while
$y is 5, showing that the outer $x is unchanged), so 'eval' does see
the lexical variable. What am I missing?
$e = '$x';
$x = 5;
*y = $x;
{
my $x = 7;
$r = eval $e;
print "r = $r\n";
print "outer r = $y\n";
}
$r = eval $e;
print "r = $r\n";
------------------------------
Date: Thu, 16 Dec 2004 11:28:14 +0100
From: "Tassilo v. Parseval" <tassilo.von.parseval@rwth-aachen.de>
Subject: Re: (quickie) (Aliasing) Possible to get this to work in strict?
Message-Id: <slrncs2opu.175.tassilo.von.parseval@localhost.localdomain>
Also sprach Arndt Jonasson:
> "Tassilo v. Parseval" <tassilo.von.parseval@rwth-aachen.de> writes:
>> As for lexical variables, technically they are not very different from
>> package variables. They have the PADMY flag set which means they are
>> subject to refcounting. Other than that, they only differ from package
>> variables in that they are not kept in a symbol-table but in so called
>> pads. Each block has its own pad and they are inaccessible from
>> pure-Perl (unless someone writes an XS extension that makes them
>> accessible).
>
> Maybe I misunderstand the above, but it seems to say that in the code
> snippet below, the lexical variable $x should not be seen by the
> 'eval' inside the block. But the output in the block is "r = 7" (while
> $y is 5, showing that the outer $x is unchanged), so 'eval' does see
> the lexical variable. What am I missing?
Why should 'eval' not see lexical variables? It does see them. It sees
the lexical $x here because inner lexicals override variables by that
name in an outer block.
> $e = '$x';
> $x = 5;
> *y = $x;
Did you mean to write
*y = \$x;
here?
> {
> my $x = 7;
> $r = eval $e;
> print "r = $r\n";
> print "outer r = $y\n";
> }
> $r = eval $e;
> print "r = $r\n";
With the change suggested above, I get:
r = 7
outer r = 5
r = 5
Is this not what you would expect?
Tassilo
--
$_=q#",}])!JAPH!qq(tsuJ[{@"tnirp}3..0}_$;//::niam/s~=)]3[))_$-3(rellac(=_$({
pam{rekcahbus})(rekcah{lrePbus})(lreP{rehtonabus})!JAPH!qq(rehtona{tsuJbus#;
$_=reverse,s+(?<=sub).+q#q!'"qq.\t$&."'!#+sexisexiixesixeseg;y~\n~~dddd;eval
------------------------------
Date: 16 Dec 2004 11:36:10 +0100
From: Arndt Jonasson <do-not-use@invalid.net>
Subject: Re: (quickie) (Aliasing) Possible to get this to work in strict?
Message-Id: <yzd1xdqlecl.fsf@invalid.net>
"Tassilo v. Parseval" <tassilo.von.parseval@rwth-aachen.de> writes:
> Also sprach Arndt Jonasson:
>
> > "Tassilo v. Parseval" <tassilo.von.parseval@rwth-aachen.de> writes:
> >> As for lexical variables, technically they are not very different from
> >> package variables. They have the PADMY flag set which means they are
> >> subject to refcounting. Other than that, they only differ from package
> >> variables in that they are not kept in a symbol-table but in so called
> >> pads. Each block has its own pad and they are inaccessible from
> >> pure-Perl (unless someone writes an XS extension that makes them
> >> accessible).
> >
> > Maybe I misunderstand the above, but it seems to say that in the code
> > snippet below, the lexical variable $x should not be seen by the
> > 'eval' inside the block. But the output in the block is "r = 7" (while
> > $y is 5, showing that the outer $x is unchanged), so 'eval' does see
> > the lexical variable. What am I missing?
>
> Why should 'eval' not see lexical variables? It does see them. It sees
> the lexical $x here because inner lexicals override variables by that
> name in an outer block.
>
> > $e = '$x';
> > $x = 5;
> > *y = $x;
>
> Did you mean to write
>
> *y = \$x;
>
> here?
Oops, yes. Or *y=*x. I made the last change in the test program but
not in the article.
> With the change suggested above, I get:
>
> r = 7
> outer r = 5
> r = 5
>
> Is this not what you would expect?
It is, but "inaccessible from pure-Perl" made me think it might not
work. Does "inaccessible" simply mean that there is no package hash
table one can traverse to find the variable?
------------------------------
Date: 16 Dec 2004 10:42:57 GMT
From: anno4000@lublin.zrz.tu-berlin.de (Anno Siegel)
Subject: Re: (quickie) (Aliasing) Possible to get this to work in strict?
Message-Id: <cprorh$a3e$1@mamenchi.zrz.TU-Berlin.DE>
Tassilo v. Parseval <tassilo.von.parseval@rwth-aachen.de> wrote in comp.lang.perl.misc:
> Aliases are a lot like hard links on Unices. You can delete the file
> that was hard-linked to but the hard link itself remains valid and
> behaves if it was the original file. ...
The analogy extends to the link count of a file, which has the same
function and reason-of-being as the refcount of a Perl variable.
Anno
------------------------------
Date: Thu, 16 Dec 2004 12:45:44 GMT
From: peter@PSDT.com (Peter Scott)
Subject: Re: (quickie) (Aliasing) Possible to get this to work in strict?
Message-Id: <Yrfwd.524058$nl.389022@pd7tw3no>
In article <yzd1xdqlecl.fsf@invalid.net>,
Arndt Jonasson <do-not-use@invalid.net> writes:
>"Tassilo v. Parseval" <tassilo.von.parseval@rwth-aachen.de> writes:
>> Also sprach Arndt Jonasson:
>>
>> > "Tassilo v. Parseval" <tassilo.von.parseval@rwth-aachen.de> writes:
>> >> As for lexical variables, technically they are not very different from
>> >> package variables. They have the PADMY flag set which means they are
>> >> subject to refcounting. Other than that, they only differ from package
>> >> variables in that they are not kept in a symbol-table but in so called
>> >> pads. Each block has its own pad and they are inaccessible from
>> >> pure-Perl (unless someone writes an XS extension that makes them
>> >> accessible).
[snip]
> "inaccessible from pure-Perl" made me think it might not
>work. Does "inaccessible" simply mean that there is no package hash
>table one can traverse to find the variable?
Yes, but: http://search.cpan.org/~robin/PadWalker-0.10/PadWalker.pm
--
Peter Scott
http://www.perldebugged.com/
*** NEW *** http://www.perlmedic.com/
------------------------------
Date: Thu, 16 Dec 2004 02:31:12 -0700
From: "Robin" <webmaster @ infusedlight.net>
Subject: Re: Can't get past 'use strict' :(
Message-Id: <WY6dne9XT_dMyFzcRVn-gg@comcast.com>
"Karlo Lozovina" <_karlo_@_mosor.net_> wrote in message
news:Xns95C0B356F954Bkarlomosornet@130.133.1.4...
> Here I go again: the following code:
>
> while(my $line = <DIRLIST>) {
> my $dir_list[$i] = $line;
> chomp($dir_list[$i]);
> $i++;
> }
>
> produces this error:
>
> syntax error at ./backup.pl line 24, near "$dir_list["
>
> when i use "use strict". I read Programming Perl's chapter on strict,
> but I can't get this thing working :(.
>
> --
> _______ Karlo Lozovina - Mosor
> | | |.-----.-----.
> | || _ | _ | Na osami blizu mora, dok se sunce zemlji smije
> |__|_|__||_____|_____| Balun gledat, za njin letit...
actually, your code looks pretty tight.
-Robin
------------------------------
Date: Thu, 16 Dec 2004 11:44:06 GMT
From: "Peter Wyzl" <wyzelli@yahoo.com>
Subject: Re: Can't get past 'use strict' :(
Message-Id: <ayewd.74895$K7.45851@news-server.bigpond.net.au>
"Karlo Lozovina" <_karlo_@_mosor.net_> wrote in message
news:Xns95C0B356F954Bkarlomosornet@130.133.1.4...
: Here I go again: the following code:
:
: while(my $line = <DIRLIST>) {
: my $dir_list[$i] = $line;
: chomp($dir_list[$i]);
: $i++;
: }
It won't compile even without 'use strict'. You can't declare a slice of an
array like that, you need to declare the whole array and then assign to the
slices. However, a better, or more Perlish way to do that would be to take
advantage of some of the majic offered by the <> operator in array context
and read the whole file into the array in one step.
my @dir_list = <DIRLIST>;
You can then chomp() the array without having to chomp every element as you
read it...
chomp @dir_list;
If you find after doing the above that you still need to have $i (if you are
using it for something other than knowing which array element to assign the
line to and chomp, then assigning an array in scalar contect is the number
of elements in the array.
my $i = @dir_list;
Which reduces the above to:->
my @dir_list = <DIRLIST>;
chomp @dir_list;
my $i = @dir_list; # may not be needed..
--
Wyzelli
------------------------------
Date: 16 Dec 2004 12:26:29 GMT
From: Karlo Lozovina <_karlo_@_mosor.net_>
Subject: Re: Can't get past 'use strict' :(
Message-Id: <Xns95C188DAB277Ekarlomosornet@130.133.1.4>
"Peter Wyzl" <wyzelli@yahoo.com> wrote in
news:ayewd.74895$K7.45851@news-server.bigpond.net.au:
> It won't compile even without 'use strict'. You can't declare a
> slice of an array like that, you need to declare the whole array
> and then assign to the slices.
I know - looking at that code day after, and I seem like an idiot
:).
> However, a better, or more Perlish way to do that would be to take
> advantage of some of the majic offered by the <> operator in array
> context and read the whole file into the array in one step.
> my @dir_list = <DIRLIST>;
Wow! Didn't know Perl can do that. It rocks, thanks.
> You can then chomp() the array without having to chomp every
> element as you read it...
> chomp @dir_list;
Didn't now I can do that, either :). Guess I'll have to finish
reading Programming Perl before beginning another Perl project.
Anyway, thanks a lot Peter, you've been most helpfull.
--
Karlo Lozovina - Mosor
------------------------------
Date: Thu, 16 Dec 2004 06:52:16 -0600
From: Tad McClellan <tadmc@augustmail.com>
Subject: Re: Can't get past 'use strict' :(
Message-Id: <slrncs3180.32t.tadmc@magna.augustmail.com>
Robin <webmaster@infusedlight.net> wrote:
> "Karlo Lozovina" <_karlo_@_mosor.net_> wrote in message
> news:Xns95C0B356F954Bkarlomosornet@130.133.1.4...
>> while(my $line = <DIRLIST>) {
>> my $dir_list[$i] = $line;
>> chomp($dir_list[$i]);
>> $i++;
>> }
[ snip quoted .sig, you aren't supposed to do that you know ]
> actually, your code looks pretty tight.
Nonsense alert!!
Doing explicit indexing when explicit indexing is not required
by the algorithm is "error prone" rather than "tight".
chomp(@dir_list = <DIRLIST>);
Does the same thing, and seems much closer to what a reasonable
person might call "tight".
--
Tad McClellan SGML consulting
tadmc@augustmail.com Perl programming
Fort Worth, Texas
------------------------------
Date: Thu, 16 Dec 2004 12:41:02 GMT
From: "Peter Wyzl" <wyzelli@yahoo.com>
Subject: Re: Extracting part of strings?
Message-Id: <ynfwd.74963$K7.12855@news-server.bigpond.net.au>
"Karlo Lozovina" <_karlo_@_mosor.net_> wrote in message
news:Xns95C0A3E02A508karlomosornet@130.133.1.4...
: Hi folks! Perl newbie in need of assistance :). I've got array
: @dir_list, with bunch of entries like "/home/something/else" and I
: want to extract the last part, that is "else".
:
: I did it like this:
:
: for($i = 0 ; $i <= $#dir_list ; $i++) {
This loops through all elements of the array @dir_list
: @tmp_fields = split /\//, "$dir_list[$i]";
and performs an operation on them, the results being assigned to a temp
array
: $dirnames[$i] = $tmp_fields[$#tmp_fields];
from which we build a third array consisting of the last element of the temp
array
: }
:
: Since I'm an extreme Perl newbie (started learning it this afternoon
::>), I'm just wondering is there a better, or more standard way of
: doing stuff like this?
The File::Basename module provides some functions for handling file paths,
but is also trivial to deal with in a regex. A single regex is not portable
(between win32, unix, mac etc) but for most small script purposes this is
not important. It should be borne in mind however.
Several ways to deal with this come to mind...
for (@dir_list){ # loop through array
if (m/.*\/(.*)/){ # capture part after last /
push @dirnames, $1: # push into array
}
}
This can be written as:
for (@dir_list){ # loop construct
push @dirnnames, $1 if m|.*/(.*)|; # test and assign
}
We test if the regex match succeeded before assigning the capture ($1)
because if not we might assign something we don't want.
The two regexes above, whilst the look different are actually the same. The
second one uses different delimiters for the match operator in order to
avoid backwacking the slash which can lead to 'leaning toothpick syndrome'
wherein it becomes hard to read what is part of the regex and what is an
escape. The regex works because it is greedy. The first .* grabs
everything, and then the regex backtracks until the / can match with the .*
following matching everything else. The parens capture everything after the
matched / into the first default variable $1.
Here is one using map:
@dirnames = map {m{.*/(.*)}} @dir_list;
Note the different delimiters for the match operator, this time m{} instead
of m|| or m//. The outer braces form the BLOCK for the map function. BTW,
using // is the only time you can drop the m// and write it like
/.*\/(.*)/;
instead of
m/.*\/(.*)/;
HTH
--
Wyzelli
------------------------------
Date: 16 Dec 2004 12:07:41 GMT
From: anno4000@lublin.zrz.tu-berlin.de (Anno Siegel)
Subject: Re: Follow-up:go to a link, save the page and email it out?
Message-Id: <cprtqd$d2k$1@mamenchi.zrz.TU-Berlin.DE>
dale <zhangd@tycoelectronics.com> wrote in comp.lang.perl.misc:
I'm not following the thread, but if you want to post a followup,
don't declare it in the subject but post a real followup. (See
the documentation of you news reader for how to do that.)
Anno
[snipped]
------------------------------
Date: Thu, 16 Dec 2004 04:28:32 -0800
From: "naniwadekar" <nani3skip45@hotmail.com>
Subject: Installing DBI module on Solaris 8
Message-Id: <32dd43F3m7bluU1@individual.net>
I want to install DBI module on my Sun Sparc server
which runs Solaris8. I upgraded to Perl 5.8.5 by
downloading perl_s-5.8.5-sol8-sparc-local.gz from
sunfreeware.com; then I installed libiconv package.
Now I want to install gcc before compiling DBI
and DBD::Oracle modules. Which gcc package I should install? The bigger one
or the smaller one? On sunfreeware site, two choices are available.
gcc-3.4.2-sol8-sparc-local.gz
or gcc_small-3.4.2-sol8-sparc-local.gz
The GNU C compiler and related programs - installs in /usr/local. This
package includes the GNU C, C++, g77, Java, and OBJC compiler suites and
support files. The gcc_small package has ONLY the C and C++ compilers and is
a much smaller download. These gcc packages require the installation of
libiconv.
Should I install gcc or is gcc_small enough?
Thanks in advance.
------------------------------
Date: 16 Dec 2004 11:15:44 GMT
From: anno4000@lublin.zrz.tu-berlin.de (Anno Siegel)
Subject: Re: Open file, print into file, what variable is used?
Message-Id: <cprqp0$a3e$2@mamenchi.zrz.TU-Berlin.DE>
Billy <bustanut2020@yahoo.com> wrote in comp.lang.perl.misc:
> Real quick question...
So you don't know the answer, but you know what is involved in answering
it?
"Quick question" must mean that it didn't take you long to ask it. I
have read it three times, and I'm still not sure what it is you want
to know.
[incomprehensible question snipped]
Anno
------------------------------
Date: Thu, 16 Dec 2004 07:35:36 -0500
From: Sherm Pendley <spamtrap@dot-app.org>
Subject: Re: Regarding ISA and Inheritance
Message-Id: <ebydnSCe1JCFHFzcRVn-sQ@adelphia.com>
palemmahesh@yahoo.co.in wrote:
> Iam new to perl and I have some doubt regarding ISA of inheritance.
>
> Suppose /tmp/base/base.pm and /tmp/derived/derived.pm files are present
> in my file system.
>
> I want to inherit base.pm into the derived module.
>
> how should I write ISA in derived.pm.
There's no way to tell, from the information you've given. Packages are
used in inheritance, not file paths.
sherm--
--
Cocoa programming in Perl: http://camelbones.sourceforge.net
Hire me! My resume: http://www.dot-app.org
------------------------------
Date: Thu, 16 Dec 2004 08:10:27 -0500
From: Sherm Pendley <spamtrap@dot-app.org>
Subject: Re: Regarding ISA and Inheritance
Message-Id: <ON-dnZCHLYXZFFzcRVn-jw@adelphia.com>
Mahesh wrote:
> and I want to inherit base.pm into derived.pm perl module.
>
> just Iam saying that both the perl modules are in different
> directories.
It doesn't matter what directories they're in. @ISA contains class
names, not file names. You haven't said what classes (i.e. packages) are
declared in those files, so it's impossible to say what their @ISAs
should look like.
In the simplest case, where the classes are named "base" and "derived",
@derived::ISA might be declared in "derived.pm" like this:
package derived;
our @ISA = qw(base);
Have a look at:
perldoc perlboot
perldoc perltoot
perldoc perltooc
perldoc perlbot
Also, have you read the posting guidelines that appear here about twice
a week?
sherm--
--
Cocoa programming in Perl: http://camelbones.sourceforge.net
Hire me! My resume: http://www.dot-app.org
------------------------------
Date: 15 Dec 2004 22:48:48 -0800
From: "learner" <tzhai2002@yahoo.com>
Subject: replace multiple lines in a file with Perl
Message-Id: <1103179728.261464.6400@z14g2000cwz.googlegroups.com>
Hi, all,
I 've searched high and low, just couldn't find a single decent
example to replace multiple lines from a file.
So I did this:
#!/usr/local/bin/perl
undef $/;
$original = "
// This is an old text
// that is across several
// lines in this file. This file
// also contains the copy right
// symbol \251.";
$replacement = "
// I just want to replace
// the old lines in one fell
// swoop!";
$sHOME = "./changed/";
foreach $file (@ARGV) {
if (! open(INPUT,"<$file") ) {
print "Can't open input file $file\n";
next;
}
$data=<INPUT>;
close INPUT;
if ($data =~ s/$original/$replacement/g) {
$newfile = $sHOME.$file;
print $newfile."\n";
if (! open(OUTPUT,">$newfile") ) {
die "Can't open output file $newfile\n";
}
print OUTPUT $data;
close OUTPUT;
print "$file changed\n";
}
else { print "$file not changed\n"; }
}
exit(0);
Then I would do:
perl thisperlfile.pl name.of.file.with.original.txt
The input file contains only one copy of the original text, but it
created more than 50 replacement txt in the final file. I am really
puzzled. Could someone help please? Thanks.
------------------------------
Date: Thu, 16 Dec 2004 11:37:20 +0100
From: Robert Sedlacek <phaylon@dunkelheit.at>
Subject: Re: replace multiple lines in a file with Perl
Message-Id: <pan.2004.12.16.10.37.20.816541@dunkelheit.at>
learner wrote:
> s/$original/$replacement/g
Sorry, too early to read it completely, but you may find help in the docs
for regular expressions. Look out for the modifiers (here you just have g).
Maybe you should also look at qr//.
g,
Robert
--
http://www.dunkelheit.at/
I want, therefore I can.
------------------------------
Date: 16 Dec 2004 12:22:48 +0100
From: Arndt Jonasson <do-not-use@invalid.net>
Subject: Re: replace multiple lines in a file with Perl
Message-Id: <yzdwtvijxmf.fsf@invalid.net>
"learner" <tzhai2002@yahoo.com> writes:
> I 've searched high and low, just couldn't find a single decent
> example to replace multiple lines from a file.
>
> [...]
>
> The input file contains only one copy of the original text, but it
> created more than 50 replacement txt in the final file. I am really
> puzzled. Could someone help please? Thanks.
I ran your program, but it seemed to do what it should with my simple
input file. Exactly what does your input look like?
------------------------------
Date: Thu, 16 Dec 2004 06:59:30 -0600
From: Tad McClellan <tadmc@augustmail.com>
Subject: Re: replace multiple lines in a file with Perl
Message-Id: <slrncs31li.32t.tadmc@magna.augustmail.com>
learner <tzhai2002@yahoo.com> wrote:
> Could someone help please? Thanks.
Yes, if you provide a short and complete program *that we can run*
that illustrates your problem.
--
Tad McClellan SGML consulting
tadmc@augustmail.com Perl programming
Fort Worth, Texas
------------------------------
Date: Thu, 16 Dec 2004 19:48:46 +1300
From: "Tintin" <tintin@invalid.invalid>
Subject: Re: Session management for cgi, ldap, oracle?
Message-Id: <32cpbdF3ln6gqU1@individual.net>
"Christopher Nehren" <apeiron+usenet@coitusmentis.info> wrote in message
news:slrncs0qdk.29gt.apeiron+usenet@prophecy.dyndns.org...
>> I would prefer Perl 5.6 or higher.
>
> Apparently you missed the large thread about Solaris and Perl. It was
> mentioned numerous times that the 5.005 version of Perl shipped with
> Solaris is required for compatability with the Perl scripts that ship
> with Solaris.
So what's the problem.
Just install a later version of Perl in a different location. If you
install the package from sunfreeware.com, it goes into /usr/local, so it
won't effect the default Solaris Perl version.
------------------------------
Date: 16 Dec 2004 02:38:15 -0800
From: "dede" <abrey@gmx.net>
Subject: Re: use external files as "HERE-docs" (and avoid the undefined substitions)
Message-Id: <1103193495.395215.183500@z14g2000cwz.googlegroups.com>
Thanks for your remark, Paul
actually, yes , i assumed that there is always a whitespace delimiting
the end of the string, since I wanted to be as close to the "normal"
way of identifying variables as possible.
We might change to ..... s/(\${.+})/ defined eval ...... to force the
curly brackets as "tidy" syntax in the external file (that i use to
build HTMLs - see other reply).
This would protect us against "accidents" like wrong evals of
$something and enhance (slightly) the performance due to the not-made
eval's.
what do you think?
dede
------------------------------
Date: 16 Dec 2004 03:03:55 -0800
From: "dede" <abrey@gmx.net>
Subject: Re: use external files as "HERE-docs" (and avoid the undefined substitions)
Message-Id: <1103195035.162812.281040@z14g2000cwz.googlegroups.com>
Salut Tad,
thank you 4 your helpful remarks.
a) Require ?
Uh, i was not clear on this one. I actually "scan" the external files
like SQL-scripts or HTML-templates to get these "dynamic" - a bit like
using JSP-variables or PHP-vars. Soon I'd like to publish a
Pod-HTML-site generator that uses this technique.
b) Unnecessary modifiers !
Yeah - you are right. A nasty side effect of doing copy&paste, or
"willy-nilly" programming as you might put it. Acknowledged.
c) Errors?
Hmmm ... again .. 1st think then post - shame on me.
The error is actually called
"Use of uninitialized value in substitution iterator at
config/Scanner.pm line 501" (freshly generated today with Perl 5.8.4.
Activestate)
Merci again,
dede
------------------------------
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 7524
***************************************