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

Hello
Thanks for your help on my last question: it has really helped me could you explain me why the glob function does not seem to work in the following case ? Thanks

The entry files of my code are files whose name are M4_0_***.txt M4_1_***.txt M4_2_***.txt M4_3_***.txt until M4_50_***.txt Here is the programm:
use strict; my $target="$ARGV[0]"; my @M4; @M4 = glob('M4_${target}_*'); print STDOUT "the files in M4 are @M4";

the result is : "the files in M4 are M4_$target". It does not seem how to replace the variable by its value. Could you check where did I go wrong ? Thanks

Replies are listed 'Best First'.
Re: function glob('*_${var}')
by Corion (Patriarch) on Jul 13, 2007 at 08:21 UTC

    Single quotes do not interpolate variables. See perlop about the quoting operators. You will likely want to use one the following solutions:

    @M4 = glob("M4_$target"); # no need for ${...} @M4 = glob(sprintf 'M4_%s', $target); @M4 = glob('M4_' . $target); # use simple concatenation

      Thanks a lot Corion.

      You are very nice.
Re: function glob('*_${var}')
by Anno (Deacon) on Jul 13, 2007 at 08:28 UTC
    Your code:

    my $target="ARGV[0]"; my @M4; @M4 = glob('M4_${target}'); print STDOUT "the files in M4 are @M4";
    Single quotes ('...') don't interpolate variables, double quotes ("...") do. You need double quotes in the third line of your code. Also, it is possible (and preferred) to declare a variable when it is first used. Replace the second and third line in your code with
    my @M4 = glob "M4_$target";

    Anno


      Thanks a lot Anno It works !!!

      (I have just made a little mistake in the programm As virtualsue said , it is $ARGV[0] instead of ARGV[0])
Re: function glob('*_${var}')
by Anonymous Monk on Jul 14, 2007 at 03:22 UTC
    read perlintro