in reply to Win32 File Creation Weirdness, ActivePerl 5.8.8

Your expression:

my ($id) = /(\d+)$/ || /^(\d+)/;

will not work correctly because the first regular expression is evaluated in scalar context and so will return 1 when it matches and not the id number.

This will do what you want:

my $id = ( /(\d+)$/ )[ 0 ] || ( /^(\d+)/ )[ 0 ];

Or this:

my $id = /(\d+)$/ ? $1 : /^(\d+)/ ? $1 : ();

Replies are listed 'Best First'.
Re^2: Win32 File Creation Weirdness, ActivePerl 5.8.8
by missingthepoint (Friar) on Jul 07, 2009 at 09:59 UTC

    jwkrahn, I fixed the bug like so after taking a break - I'm rather sleep deprived :|

    my ($id) = /(\d+)$/; unless (defined $id) { ($id) = /^(\d+)/ }

    Windows never ceases to astonish me. Thanks all for your help :)


    The zeroeth step in writing a module is to make sure that there isn't already a decent one in CPAN. (-- Pod::Simple::Subclassing)
      Windows never ceases to astonish me.
      You think that's astonishing? Brace yourself.

      The Windows CLI wants redirection operator characters (e.g.,  > < |) in a double-quoted string to be escaped with a  ^ (hat) character if they are preceded by an odd number of backslash-escaped double-quote characters in the string! (Don't even ask what happens if the backslash escapes are themselves backslash-escaped.)

      Consider (this just illustrates redirection-character escaping; the scalar/list context problem with the regex is not addressed):

      C:\@Work\Perl\junque>dir Volume in drive C has no label. Volume Serial Number is EC11-ED1D Directory of C:\@Work\Perl\junque 07/07/2009 11:42 AM <DIR> . 07/07/2009 11:42 AM <DIR> .. 07/07/2009 11:41 AM <DIR> 16 07/07/2009 11:41 AM <DIR> 17 foo bar 07/07/2009 11:42 AM <DIR> 18- baz quux 0 File(s) 0 bytes 5 Dir(s) 10,227,699,712 bytes free C:\@Work\Perl\junque>perl -e "for (<*>) { my ($id) = /(\d+)$/ || /^(\d+)/; print qq{$_ => \"id=^>$id\" => x \n}; } " 16 => "id=>1" => x 17 foo bar => "id=>17" => x 18- baz quux => "id=>18" => x C:\@Work\Perl\junque>perl -e "for (<*>) { my ($id) = /(\d+)$/ || /^(\d+)/; print qq{$_ => \"id=>$id\" => x \n}; } " The system cannot find the path specified. C:\@Work\Perl\junque>perl -e "print qq{ (\") \n}; for (^<*^>) { my ($id) = /(\d+)$/ ^|^| /^^(\d+)/; print qq{$_ =^> \"id=>$id\" =^> x \n}; } " (") 16 => "id=>1" => x 17 foo bar => "id=>17" => x 18- baz quux => "id=>18" => x x => "id=>" => x
      Enjoy!

        I am speechless. Needing those chars escaped if preceded by an odd number of \"?? That requirement's straight out of Monty Python!


        The zeroeth step in writing a module is to make sure that there isn't already a decent one in CPAN. (-- Pod::Simple::Subclassing)