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

Dear monks,

I've been using the code below to match some files in the path described in the code. However I'm getting a "Use of uninitialized value $_ in pattern match (m//)" error.

#!/usr/bin/perl/ use strict; use warnings; my $i; my $j; my $k; my $l; my $id = "AC"; my $moved; for ($i=2; $i<=3; $i++) { my $j = sprintf ("%04d", $i); for ($k=1; $k<=3; $k++) { my $l = sprintf ("%05d", $k); my $path; $path = "/home/results/initial/$id$j/F${l}"; chdir "$path"; /MV(\d{4})/; $moved = $1; print "$moved\n"; } }

I seem to be getting this error even though I have changed directory into the directory specified in the path - which is something I don't want to do, I want to give the absolute path each time, but I was just trying to see if it would solve my problem. I also ran the script using perl -wc to check syntax/warnings but it comes back as "syntax correct". Also, if I do print `ls $path/MV*`; it finds the files fine and prints them. What do you think could be the source of my problem?

Replies are listed 'Best First'.
Re: Use of uninitialized value $_ in pattern match (m//)?
by hippo (Archbishop) on Feb 29, 2016 at 15:19 UTC

    The problem is that you have not set $_ anywhere either explicitly or implicitly. From your description it looks like you want to match against a filename so perhaps it is glob that you really want rather than m//?

Re: Use of uninitialized value $_ in pattern match (m//)?
by Corion (Patriarch) on Feb 29, 2016 at 15:20 UTC

    What is your match trying to match against?

    Usually, you have a variable to the left of the match statement. If you don't have that, Perl will use the default topic, $_ to match.

    What is the value of $_ and where do you set it?

      Hi, thanks for your time! I am trying to match against filenames, from files that are in the path I specify. I modified my script to

      $match =~/MV(\d{4})/; $moved = $1; print "$moved \n";

      but although I initialise my $match outside of the loops, I still get the same error as described initially :(

        Try

        #!/usr/bin/perl/ use strict; use warnings; my $id = "AC"; my $root = '/home/results/initial/'; for my $i (2..3){ for my $k (1..3){ my $path = $root.sprintf "%s%04d/F%05d",$id,$i,$k; print "$path\n"; for (glob "$path/MV*"){ print "$1\n" if /MV(\d{4})/; } } }
        poj

        What is the value in $match?

        What do you expect the value in $match to be?

        As hippo already mentioned, you seem to be highly confused about how to read entries in a directory. See glob, or opendir and readdir.

        ... I still get the same error as described initially ...

        From the OP: '... I'm getting a "Use of uninitialized value $_ in pattern match (m//)" error.'
        If you're really still getting this exact "error" (a warning, actually), then you still have a
            /MV(\d{4})/;
        statement lurking somewhere in your code that is still trying to implicitly match against the  $_ variable which you have still not initialized. What line number is associated with the warning message? Look at (or near) that line for the offending statement.


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