Drieux wrote at Fri, 26 Jul 2002 16:53:40 +0200: > I think the issue is, > > would you really want to maintain that code? > >> ex: >> if( my($var) = $string =~ /(\d+)/){ >> # do stuff here >> } >> } >> And does the scope of $var in the example scope to the closing if } or )? > > so whether or not one could do the trick > > I would go with something more boring > > if( $string =~ /(\d+)/){ > # we matched, save our $1 > my $tmp_number = $1; > ..... > } > }
Could be that it is a religious topic, but IMHO the style if ( my ($var) = $string =~ /(\d+)/) { ... } is at least as easy to maintain than if ($string =~ /(\d+)) { my $var = $1; } Reasons are: - Giving the variables good names, explains very good what the regexp has to match - it's nearly a comment. And it's always good to write self explaining programs - I try to play as less as reasonable with the global variables $1, $2, ... One example is when the if-statement becomes more complex, Imagine you want also to say something like if ($string =~ /(\d+)/ and $description =~ /(\w+)/) { # now $1 contains something very different } Writing like if (my ($var) = $string =~ /(\d+)/ and $description =~ /(\w+)/) { # doesnt have this problem # it's a little bit more sure } - Always when looking at the regexp, it's easy to see that the capturing plays an important role. That is an important fact, when changing the regexp a little bit - as the (?: ... ) syntax is a little bit less readable than the simple ( ... ) syntax, but sometimes very important - we save 3 characters to type ( "$1;" ) Of course, all I said, doesn't play a role if the line becomes too long, but that should be avoided in general, IMHO. I don't want to offense you, all I'd like to say is that this programming style is not so bad that we should explain beginners not to use it. Perhaps we should advice them to program in that way, but I think the real answer is TMTWTDI Greetings, Janek -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]