Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

How to use scalar value in substitution pattern?

4 views
Skip to first unread message

Masato Sogame

unread,
Dec 30, 2009, 2:40:56 AM12/30/09
to begi...@perl.org
Hello,
I want to replace text in file.
So, I wrote subroutine like this:

sub replace_txt {
my ($old,$new) = @_;
open (FILE,"<$ARGV[0]");
while(my $line = <FILE>) {
$line =~ s/$old/$new/g;
print $line;
}
close(FILE);
}

when I use this subroutine line this:
&replace_txt('$a', '$b');
but It doesn't work. $a in $ARGV[0] is still $a.

But, when I rewrite my subroutine like this:

sub replace_txt {
open (FILE,"<$ARGV[0]");
while(my $line = <FILE>) {
$line =~ s/\$a/\$b/g;
print $line;
}
close(FILE);
}

It work fine. but I want to pass old and new text to subroutine as argument.
How to do this?
I search about this but I can't find any help so please help me.


Shlomi Fish

unread,
Dec 30, 2009, 5:27:33 AM12/30/09
to begi...@perl.org, poket...@yahoo.co.jp
Hi Masato!

A few notes about your code.

On Wednesday 30 Dec 2009 09:40:56 Masato Sogame wrote:
> Hello,
> I want to replace text in file.
> So, I wrote subroutine like this:
>
> sub replace_txt {
> my ($old,$new) = @_;

Your code needs indentation.

> open (FILE,"<$ARGV[0]");

See:

http://www.perlfoundation.org/perl5/index.cgi?two_argument_open

This should be replaced with:

<<<<<<
open my $file, "<", $ARGV[0]
or die "Could not open '$file' - $!";
>>>>>>
I should note that it is bad form to access $ARGV[0] from the subroutine like
that. I would do:

<<<<<
# in top-level
my ($filename) = @ARGV;

replace_text($filename, $old_text, $new_text);
>>>>>

> while(my $line = <FILE>) {
> $line =~ s/$old/$new/g;

Unless $old is a mini-regular-expression you should use \Q and \E here:

http://perldoc.perl.org/functions/quotemeta.html

> print $line;

Do you want to print it to STDOUT?

> }
> close(FILE);
> }
>
> when I use this subroutine line this:
> &replace_txt('$a', '$b');

1. Don't use ampersand-subroutine calls:

http://www.perlfoundation.org/perl5/index.cgi?subroutines_called_with_the_ampersand

2. << '$a' >> is the literal string dollar-a. Maybe you want << "$a" >> or in
this case simply << $a >> because you don't need to stringify a single string
variable. Furthermore, you should not use $a and $b as lexical variables
because they have special meaning in
http://perldoc.perl.org/5.8.8/functions/sort.html and other functions.

> but It doesn't work. $a in $ARGV[0] is still $a.
>
> But, when I rewrite my subroutine like this:
>
> sub replace_txt {
> open (FILE,"<$ARGV[0]");
> while(my $line = <FILE>) {
> $line =~ s/\$a/\$b/g;
> print $line;
> }
> close(FILE);
> }
>
> It work fine. but I want to pass old and new text to subroutine as
> argument. How to do this?
> I search about this but I can't find any help so please help me.
>

I think the problem with your code is that you didn't use \Q and \E.

Regards,

Shlomi Fish

--
-----------------------------------------------------------------
Shlomi Fish http://www.shlomifish.org/
Freecell Solver - http://fc-solve.berlios.de/

Bzr is slower than Subversion in combination with Sourceforge.
( By: http://dazjorz.com/ )

Suresh Kumar

unread,
Dec 30, 2009, 3:45:51 AM12/30/09
to begi...@perl.org, poket...@yahoo.co.jp
HI

Pass the parameters in double quotes or simply $a, $b. Like below


&replace_txt("$a", "$b");

&replace_txt($a, $b);

Single quotes doesn't interpolate the variables.

Thanks,
Suresh

<============>


Hello,
I want to replace text in file.
So, I wrote subroutine like this:

sub replace_txt {
my ($old,$new) = @_;

open (FILE,"<$ARGV[0]");
while(my $line = <FILE>) {

$line =~ s/$old/$new/g;
print $line;
}
close(FILE);
}

when I use this subroutine line this:
&replace_txt('$a', '$b');

but It doesn't work. $a in $ARGV[0] is still $a.

But, when I rewrite my subroutine like this:

sub replace_txt {
open (FILE,"<$ARGV[0]");
while(my $line = <FILE>) {
$line =~ s/\$a/\$b/g;
print $line;
}
close(FILE);
}

It work fine. but I want to pass old and new text to subroutine as argument.
How to do this?
I search about this but I can't find any help so please help me.


=============

Suresh Kumar

unread,
Dec 30, 2009, 4:59:04 AM12/30/09
to poket...@yahoo.co.jp, begi...@perl.org
I got your problem.

Pass the parameters like below,


&replace_txt('\$a', '$b');

It will work fine.

In this line, $line =~ s/$old/$new/g;
$old should have \$a, in order to match a variable name.
If u pass '$a' alone - &replace_txt('$a', '$b');
$old will have $a and it will search for the value of $a and not actual $a.

Hope u will get this.

Thanks,
Suresh


On Wed, Dec 30, 2009 at 2:25 PM, Masato Sogame <poket...@yahoo.co.jp>wrote:

> Sorry, Suresh my English was bad.
> I want to replace string '$a' to '$b' in $ARGV[0]
> For example if I write text file like this:
>
> my $a = "Hello,World";
> print $a;
>
> And I pass this text file to my script then I want to output like this:
>
> my $b = "Hello,World";
> print $b;
>
> So I want to pass old text and new text as argument of my subroutine.
> (More simple just "I want to replace variable name in my text file.")


>
> HI
>>
>> Pass the parameters in double quotes or simply $a, $b. Like below
>> &replace_txt("$a", "$b");
>> &replace_txt($a, $b);
>>
>> Single quotes doesn't interpolate the variables.
>>
>> Thanks,
>> Suresh
>>
>

> --------------------------------------
> Get Disney character's mail address on Yahoo! Mail
> http://pr.mail.yahoo.co.jp/disney/
>

--
Thanks,
Suresh

Uri Guttman

unread,
Dec 30, 2009, 11:40:32 AM12/30/09
to suresh kumar, begi...@perl.org, poket...@yahoo.co.jp
>>>>> "sk" == suresh kumar <sur...@gmail.com> writes:

sk> Pass the parameters in double quotes or simply $a, $b. Like below
sk> &replace_txt("$a", "$b");

that is wrong. don't quote scalar vars unless you want to force
stringification.

also to the OP, don't use $a and $b except in sorting. they are special
variables and will not trigger errors under strict.

uri

--
Uri Guttman ------ u...@stemsystems.com -------- http://www.sysarch.com --
----- Perl Code Review , Architecture, Development, Training, Support ------
--------- Gourmet Hot Cocoa Mix ---- http://bestfriendscocoa.com ---------

Uri Guttman

unread,
Dec 30, 2009, 11:42:14 AM12/30/09
to suresh kumar, poket...@yahoo.co.jp, begi...@perl.org
>>>>> "sk" == suresh kumar <sur...@gmail.com> writes:

sk> I got your problem.
sk> Pass the parameters like below,
sk> &replace_txt('\$a', '$b');

sk> It will work fine.

did you test that? it has the same problem as the original code. your
other post had better answers. please refrain from answering here unless
you are sure of your answers. or at least follow up your own posts if
you realize they are wrong.

0 new messages