one liners or not

T

tweetiebirds

Hi,

This may be a dumb question and excuse me if it is:

In a Perl program, let's say I need to replace a string in a text file
with another.

What is best?

1. Open the file , get a file handle and then
open(HANDLE, "flname");
while (<HANDLE>) {
if /pattern {
s/pattern/newpattern/g;
}
close(HANDLE)
}


or

2.

system ("perl -pi -e 's/pattern/newpattern/g' flname");

Is it good to call PERL from PERL?


or

3.

$repl = `perl -pi -e 's/pattern/newpattern/g' flname`;



thanks !
 
A

A. Sinan Unur

(e-mail address removed) wrote in @f14g2000cwb.googlegroups.com:
This may be a dumb question
http://www.catb.org/~esr/faqs/smart-questions.html

and excuse me if it is:

Please don't post if it is.
In a Perl program,
++++++++++++++++++++

This statement is key.
let's say I need to replace a string in a text file
with another.

What is best?

1. Open the file , get a file handle and then
open(HANDLE, "flname");
while (<HANDLE>) {
if /pattern {
s/pattern/newpattern/g;
}
close(HANDLE)
}

This is no good, as it is not Perl. Thus, your program would not work.

In addition, whatever language this is in, it probably has a facility to
check if open failed. You should *always* check for that possibility.

Further, assuming the language you wrote in is somewhat similar to Perl
in how it handles things, it looks like you only modify the contents of
the file in memory, and do *not* modify the original file.
2.

system ("perl -pi -e 's/pattern/newpattern/g' flname");

Is it good to call PERL from PERL?

There is no PERL. It looks like you are calling perl from a Perl program.

Again, you do not check if the system call actually succeeded. Given that
it will fail, that is not a good idea.

In general, I can't see a point to this in a well-designed, reusable
program. Although, I might use the correct form of that in a throwaway
script.
$repl = `perl -pi -e 's/pattern/newpattern/g' flname`;

What kind of output do you expect from that command, even if you had
correctly specified the command line options to perl?

Just to reiterate:

http://www.catb.org/~esr/faqs/smart-questions.html


Sinan
 
X

xhoster

Hi,

This may be a dumb question and excuse me if it is:

Your excused! :)

In a Perl program, let's say I need to replace a string in a text file
with another.

What is best?

That depends on what you want to do.
1. Open the file , get a file handle and then
open(HANDLE, "flname");
while (<HANDLE>) {
if /pattern {

if (/pattern/) {
# but you don't need this anyway. if /pattern/ doesn't match, there
# is no harm in calling the substitution, because it won't do anything,
# so just omit the if statement.
s/pattern/newpattern/g;
}
close(HANDLE)
}

This doesn't do anything meaninful. It makes the substituion, and then
throws away the answer. Doesn't write it back to the file or anything.
Also, if the pattern can span lines (or whatever $/ is set to) then it
won't work right (but neither will the other two examples)
or

2.

system ("perl -pi -e 's/pattern/newpattern/g' flname");

Now this changes the original file. Is that what you want to do?
Is it good to call PERL from PERL?

No, but it is fine to call perl from Perl. I do it all the time in ad-hoc
code. But I would shy away from it for "production" code.
or

3.

$repl = `perl -pi -e 's/pattern/newpattern/g' flname`;

Why would you want to do this? What do you plan on doing with $repl?

Xho
 
T

tweetiebirds

Yes I want to change to original file.
So on 1. I understand that it doesn't modify the file.
I am not planning of doing anything with $repl, I was only wondering
which one was more efficient (faster). (system("") or $repl = '')
Why won't it work right if the input record seperator is set?

thanks.
 
X

xhoster

(e-mail address removed) wrote:

You should repond to the person you are responding to (so the "references"
remain properly threaded) and quote a salient portion of what you are
responding to.
Yes I want to change to original file.
So on 1. I understand that it doesn't modify the file.

So then obviously it can't be the "best" as it doesn't do what
you wanted. Why include it?
I am not planning of doing anything with $repl, I was only wondering
which one was more efficient (faster). (system("") or $repl = '')

The difference between them is trivial. If you are really worried
about it, which there is no reason to be, then you should measure the
difference on your computer, on your filesystem, and with your actual
test files. I bet system will be a hair faster.
Why won't it work right if the input record seperator is set?

It won't work right if the pattern spans the record (as defined by
$/) boundary. For example, if pattern is "hello\nthere", and the
file contains that string, it won't match because first "hello\n" is
tested, and then "there" is tested, but both together are not tested.

Xho
 
A

A. Sinan Unur

(e-mail address removed) wrote in @g14g2000cwa.googlegroups.com:
thanks for the comments,

What comments? I don't see any comments!

Please quote some context when you reply.
it didn't answer my question though.

So?

Sinan
 
M

MichiganBob

There is no PERL. It looks like you are calling perl from a Perl program.

Why is it that in a language that boasts TMTOWTDI, there is only one way
to refer to it?

-MichiganBob
 
A

A. Sinan Unur

...

Why is it that in a language that boasts TMTOWTDI, there is only one
way to refer to it?

That's cute.

Now, my comment was in response to the OP's code:

Could you please tell me where the OP is calling PERL (remembering that
perl and PERL are distinct names in a lot of file systems)?

Sinan
 
T

Tad McClellan

MichiganBob said:
...

Why is it that in a language that boasts TMTOWTDI, there is only one way
to refer to it?


Because it is useful (in avoiding ambiguity).

Perl is used to refer to the language proper.

perl is used to refer to the _implementation_ of the
language (the interpreter).

PERL is not used (by those in the know), because it is not an acronym.


Correct use of the case of [Pp]erl's name serves as a
kind of "secret handshake".

All you need to do is use "PERL" case, and someone will initiate you
into the Perl Club. :)
 
I

ioneabu

Hi,

This may be a dumb question and excuse me if it is:

In a Perl program, let's say I need to replace a string in a text file
with another.

What is best?

1. Open the file , get a file handle and then
open(HANDLE, "flname");
while (<HANDLE>) {
if /pattern {
s/pattern/newpattern/g;
}
close(HANDLE)
}


or

2.

system ("perl -pi -e 's/pattern/newpattern/g' flname");

Is it good to call PERL from PERL?


or

3.

$repl = `perl -pi -e 's/pattern/newpattern/g' flname`;



thanks !


I think that it's a good question and I don't know that it's been
answered adequately. The answer should be found in perldoc perlrun.
Here is some pertinent info from that file:

-p causes Perl to assume the following loop around your
program, which makes it iterate over filename argu-
ments somewhat like sed:

LINE:
while (<>) {
... # your program goes here
} continue {
print or die "-p destination: $!\n";
}

If a file named by an argument cannot be opened for
some reason, Perl warns you about it, and moves on to
the next file. Note that the lines are printed auto-
matically. An error occurring during printing is
treated as fatal. To suppress printing use the -n
switch. A -p overrides a -n switch.

"BEGIN" and "END" blocks may be used to capture con-
trol before or after the implicit loop, just as in
awk.


-i[extension]
specifies that files processed by the "<>" construct
are to be edited in-place. It does this by renaming
the input file, opening the output file by the origi-
nal name, and selecting that output file as the
default for print() statements. The extension, if
supplied, is used to modify the name of the old file
to make a backup copy, following these rules: [snip]

I tried writing an example that reproduced the above specifications and
I was not able to figure out an elegant way to recreate what -i does.
I would be curious to see how a Perl program would look that does
exactly what perl -pi -e 's/pattern/newpattern/g' files* does as
specified above.

Thanks!

w
 
J

John W. Krahn

This may be a dumb question and excuse me if it is:

In a Perl program, let's say I need to replace a string in a text file
with another.

What is best?

1. Open the file , get a file handle and then
open(HANDLE, "flname");
while (<HANDLE>) {
if /pattern {
s/pattern/newpattern/g;
}
close(HANDLE)
}

or

2.

system ("perl -pi -e 's/pattern/newpattern/g' flname");

Is it good to call PERL from PERL?

or

3.

$repl = `perl -pi -e 's/pattern/newpattern/g' flname`;

Or:


( $^I, @ARGV ) = ( '', 'flname' );
while ( <> ) {
s/pattern/newpattern/g;
print;
}



John
 
J

John W. Krahn

Hendrik said:
John W. Krahn uitte de volgende tekst op 14/05/2005 10:33:


This won't work on Windows though. Replace '' by any other string. (How
about '.bak'?)

In the above examples the OP is using "perl -pi -e 's/pattern/newpattern/g'
flname" and since single quotes do not work in Windows shell I naturally
assumed that it was not running on Windows. :)


John
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,170
Messages
2,570,925
Members
47,468
Latest member
Fannie44U3

Latest Threads

Top