in reply to Re^2: regex behaves differently in split vs substitute?
in thread regex behaves differently in split vs substitute?

Eeeeeeewwwwww :P

#!/usr/bin/perl -- use strict; use warnings; my $dita = <<'__DITA__'; mono-basic-2.10 mono-2.10.2-r1 mono-2.10.5 __DITA__ open my $data => '<', \$dita or die $!; while( my $line = <$data> ){ if( my ($version) = $line =~ /^[a-z-]+(\d.*)\s*$/ ){ print ">>$version<<\n" } } __END__ >>2.10<< >>2.10.2-r1<< >>2.10.5<<

Replies are listed 'Best First'.
Re^4: regex behaves differently in split vs substitute?
by Marshall (Canon) on Oct 08, 2011 at 13:54 UTC
    That is exactly the same. It makes no difference if the "if" is written before the match or on the same line as the print. If you like your formulation better, then do it.

    The main idea of my reply to leslie was to avoid $1. The rationale is simple: $version is easier to understand than $1. This "if" before or at the end of the print is a side show, tangential to the point - makes no difference.

    In general try to avoid $1 because almost surely there is some better name! Even if I am reading my own code one year later, I don't want to read the regex to see that I'm capturing digits...I'll see that I've got $version from the regex and my eyeballs keep moving in the code.

      That is exactly the same... tangential to the point.

      You see Eeeeeeewwwwww :P is synonym for style tangent :)

      Yes, named variables make more sense, if you're going to use them for something

      If all you're going to do is print the match, you might as well use $1

        Yes, named variables make more sense, if you're going to use them for something

        But that's the point: most code that gets written is not simple example code, but code that is "going to [be used] for something", hence the example of using named variables.