ra93013 has asked for the wisdom of the Perl Monks concerning the following question:

I'm trying to passing in a regular express and keep getting

Use of uninitialized value $_ in pattern match (m//) at ./INVALID_FILE +.pl
Here's the option I use to pass in the expression
--pattern '[G|M][0-1][0-9]_(GSP_|VIB_)[0-9]{12}\.log'

$args{dir} = $DEFAULT_DIRECTORY unless defined( $args{dir} ); $args{log} = $DEFAULT_LOG_FILE unless defined( $args{log} ); $args{pattern} = $DEFAULT_PATTERN unless defined( $args{pattern} ); $args{sendto} = $DEFAULT_SEND_TO unless defined( $args{sendto} ); $args{help} = 0 unless defined( $args{help} ); die "$usage\n" if $args{help}; #create new Log::LogLite object my $log = new Log::LogLite( $args{log}, $ERROR_LOG_LEVEL ); print "Pattern Submitted: $args{pattern}\n"; my $pattern_re = m/(?:$args{pattern})/o; #my $pattern_re = $args{pattern}; print "Pattern: $pattern_re\n"; # Make sure this directory exists if ( -d $STARTING_DIRECTORY . $args{dir} ) { my @files = File::Find::Rule ->not_name($pattern_re) ->in($STARTING_DIRECTORY . $args{dir});

Replies are listed 'Best First'.
Re: Uninitialized Value $regex
by toolic (Bishop) on Jun 04, 2015 at 20:23 UTC
Re: Uninitialized Value $regex
by kcott (Archbishop) on Jun 04, 2015 at 20:34 UTC

    G'day ra93013,

    Your problem is probably this line:

    my $pattern_re = m/(?:$args{pattern})/o;

    That's attempting to perform a match (m//) on $_ which isn't set ("uninitialized value $_"). See perlre.

    What you probably want is qr//, not m//. See "Regexp Quote-Like Operators".

    -- Ken

Re: Uninitialized Value $regex
by Athanasius (Archbishop) on Jun 05, 2015 at 01:56 UTC

    Hello ra93013,

    Just a side note — this:

    $args{dir} = $DEFAULT_DIRECTORY unless defined( $args{dir} ); $args{log} = $DEFAULT_LOG_FILE unless defined( $args{log} ); $args{pattern} = $DEFAULT_PATTERN unless defined( $args{pattern} ); $args{sendto} = $DEFAULT_SEND_TO unless defined( $args{sendto} ); $args{help} = 0 unless defined( $args{help} );

    can be written more succinctly as:

    $args{dir} //= $DEFAULT_DIRECTORY; $args{log} //= $DEFAULT_LOG_FILE; $args{pattern} //= $DEFAULT_PATTERN; $args{sendto} //= $DEFAULT_SEND_TO; $args{help} //= 0;

    See perlop#Logical-Defined-Or.

    Hope that helps,

    Athanasius <°(((><contra mundum Iustus alius egestas vitae, eros Piratica,

      can be written more succinctly ... See perlop#Logical-Defined-Or.

      Yes, but not in perl versions older than 5.10 (released Dec 2007) / 5.9 (released Jul 2007), see perl5100delta /perl590delta. Older perl versions will likely see this as a syntax error. Unfortunately, this little detail is not documented in perlop. For someone new to Perl, perlop must read like defined-or was always present in Perl.

      Alexander

      --
      Today I will gladly share my knowledge and experience, for there are no sweeter words than "I told you so". ;-)
        ... not in perl versions older than 5.10 ...

        In any version of Perl, the command-line  perldoc perlop (and other docs) can be a very welcome local feature sanity check. It's saved me wasted motion more than once.

        The on-line perlop (and on-line documentation in general) is tied to the most recent Perl version and is perhaps a bit overenthusiastic in presenting new features, but even this can be dialed back to previous versions (back to 5.8, IIRC).


        Give a man a fish:  <%-(-(-(-<