From: yjnnhauhht on 1 Jul 2010 11:02 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 1 Jul 2010 11:11 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 1 Jul 2010 12:29 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 1 Jul 2010 18:23 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 1 Jul 2010 19:47 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
|
Next
|
Last
Pages: 1 2 3 Prev: FAQ 9.18 How do I decode a MIME/BASE64 string? Next: Problem with IE automation in Perl |