From: yjnnhauhht on
Hi,

The following works without strict ref (withh perl 5.10 not 5.8) :
my $i=1;
my $string="teststring";
$string=~s/(test)/${$i}/;
print "$string\n";

But fails to compile with error message "Can't use string ("1") as a
SCALAR ref while "strict refs"" with strict ref.

Is there a way to write it differently so that it's accepted with
strict ref?

Regards,
Yjnnhauhht.
From: Peter Makholm on
yjnnhauhht <yjnnhauhht(a)mailinator.com> writes:

> But fails to compile with error message "Can't use string ("1") as a
> SCALAR ref while "strict refs"" with strict ref.
>
> Is there a way to write it differently so that it's accepted with
> strict ref?

No, but you can disable strict refs in a block:

#!/usr/bin/perl

use strict;
use warnings;

use 5.10.0;

my $i = 1;
my $string = "teststring";

{
no strict 'refs';

$string =~ s/(test)/\U${$i}/i;
}

say $string;

$string =~ s/(test)/\L${$i}/i;

say $string;
__END__

//Makholm
From: Tad McClellan on
yjnnhauhht <yjnnhauhht(a)mailinator.com> wrote:
> Hi,
>
> The following works without strict ref (withh perl 5.10 not 5.8) :
> my $i=1;
> my $string="teststring";
> $string=~s/(test)/${$i}/;
> print "$string\n";
>
> But fails to compile with error message "Can't use string ("1") as a
> SCALAR ref while "strict refs"" with strict ref.
>
> Is there a way to write it differently so that it's accepted with
> strict ref?


Sure:

$string=~s/(test)/test/;

I smell an XY-problem.

What is it that you are actually trying to accomplish?


--
Tad McClellan
email: perl -le "print scalar reverse qq/moc.liamg\100cm.j.dat/"
The above message is a Usenet post.
I don't recall having given anyone permission to use it on a Web site.
From: Big and Blue on
On 07/01/10 17:29, Tad McClellan wrote:

>
> $string=~s/(test)/test/;
>
> I smell an XY-problem.
>
> What is it that you are actually trying to accomplish?

Is it, by chance:

my $i=1;
my $string="teststring";
$string=~s/(?<test>test)/$+{test}/;
print "$string\n";

?

--
Just because I've written it doesn't mean that
either you or I have to believe it.
From: sln on
On Thu, 1 Jul 2010 08:02:54 -0700 (PDT), yjnnhauhht <yjnnhauhht(a)mailinator.com> wrote:

>Hi,
>
>The following works without strict ref (withh perl 5.10 not 5.8) :
>my $i=1;
>my $string="teststring";
>$string=~s/(test)/${$i}/;
>print "$string\n";
>
>But fails to compile with error message "Can't use string ("1") as a
>SCALAR ref while "strict refs"" with strict ref.
>
>Is there a way to write it differently so that it's accepted with
>strict ref?
>

${$ .. is dereferencing notation so $i must be a reference.
If you change it to $i = \2, it would then be a reference and you wouldn't get
that message.
Then $string would contain "1string" after the substitution.

But, I get the feeling you want to have a variable capture buffer variable.
In that case the replacement side should be done seperately as an actual variable
assignment "string".

Or you can combine it all in a double eval regex as something like this:
(expanded with print for detail)

$i = 2;
$string =~ s/(test)(string)/print '${'.$i."}\n"; '${'.$i.'}'/ee;
# or, shortened
# $string =~ s/(test)(string)/'$'.$i/ee;


-sln