On Tuesday 13 Apr 2010 12:29:37 Akhthar Parvez K wrote:
> Got that, thanks Shlomi! However, can this be done by referencing $1
> directly with strict refs on? I definitely feel it can be done and would
> be nice to know how!

It cannot be done directly using ${$idx} as you've shown but as a wise man 
once said "You can always hack something":

1. You can using string eval : << eval '$1' >> should yield the value of the 
"$1" variable and you can go further than that.

2. You can try using @- and @+ from perldoc perlvar along with the substr() 
function . In this case, returning matches would be much more convenient.

3. There may be some dark magic way of using the "main::" hash and 
dereferencing the value of "$1" there. Please don't do that.

All of these ways are not as straightforward as just returning matches, so 
please don't do them. $1, $2, $3, etc. have been intended for convenience (and 
were inspired by some similar features of previous UNIX technologies), and 
should not be regarded as "The one good way of doing things" because they are 
not. When possible, seek better ways.

Regards,

        Shlomi Fish

> 
> Regards,
> Akhthar Parvez K
> http://Tips.SysAdminGUIDE.COM
> UNIX is basically a simple operating system, but you have to be a genius to
> understand the simplicity - Dennie Richie
> 
> On Tuesday 13 Apr 2010, Shlomi Fish wrote:
> > There are several ways to overcome this problem. I suggest that instead
> > of referencing $1, $2 and $3 directly you place them in an array:
> > 
> > {{{
> > my @matches = ($string =~ m{....}ms);
> > }}}
> > 
> > And then access the captures in @matches using their indices -
> > $matches[$idx-1]. Just note that $matches[0] would be $1, $matches[1]
> > would be $2. If that doesn't work for you due to some reason, I may be
> > able to come up with other suggestions.
> > 
> > Regards,
> > 
> >     Shlomi Fish

-- 
-----------------------------------------------------------------
Shlomi Fish       http://www.shlomifish.org/
What Makes Software Apps High Quality -  http://shlom.in/sw-quality

Deletionists delete Wikipedia articles that they consider lame.
Chuck Norris deletes deletionists whom he considers lame.

Please reply to list if it's a mailing list post - http://shlom.in/reply .

-- 
To unsubscribe, e-mail: beginners-unsubscr...@perl.org
For additional commands, e-mail: beginners-h...@perl.org
http://learn.perl.org/


Reply via email to