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

HI i am new to perl.. please help me iam getting error at @INFFiles = `dir /b $filespec`; foreach loop is not executing and iam not understanding this part $VersionNumber =~ s/^\d+\.\d+\.\d+\.\d+//g; and $VersionNumber =~s/\x0d//g; please Guide me what i need to learn to understand this code.

sub FileVersion($$$) { my ($path, $fileName, VersionID) = @_; if ("" eq $fileName) { $filespec = "$path\\*.inf"; } else { $filespec = "$path\\$fileName"; } @INFFiles = `dir /b $filespec`; foreach $infFile (@INFFiles) { chomp $infFile; $infFile = "$path\\$infFile"; $encoding = GetFileEncoding($infFile); open(INF, "<:encoding($encoding)", "$infFile") || writelog(log +die,"Could not open $path\\$infFile"); @Contents = <INF>; @match = grep(/DriverVer=/, @Contents); close INF; } my ($prefix, $VersionNumber) = split(',', $match[0]); $VersionNumber =~ s/[^\d+\.\d+\.\d+\.\d+]//g; $VersionNumber =~ s/\x0d//g; chomp $VersionNumber; writelog(debug,"pr_UpdateFileVersion $MyVersionID, $VersionNumber" +); ExecStoredProcedure("pr_UpdateFileVersion", "$MyVersionID,'$Versio +nNumber'"); return $VersionNumber; }

Replies are listed 'Best First'.
Re: Please help me
by jwkrahn (Abbot) on Feb 12, 2010 at 05:27 UTC
    iam getting error at @INFFiles = `dir /b $filespec`;

    Use opendir, readdir and closedir or glob instead.

    iam not understanding this part $VersionNumber =~ s/[^\d+\.\d+\.\d+\.\d+]//g;

    [^\d+\.\d+\.\d+\.\d+] is a character class that matches any single character that is not the character '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', '+' or '.' which could more simply be written as [^\d+.].    The substitution operator replaces each character that matches with nothing globally with the result that $VersionNumber will only contain the twelve characters '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', '+' and '.'.

Re: Please help me
by FalseVinylShrub (Chaplain) on Feb 12, 2010 at 06:17 UTC

    Hi

    To expand a little on what jwkrahn said:

    Your foreach loop probably is executing, but because  @INFFiles = `dir /b $filespec`; doesn't do what you think, it's only executing once. Backticks (`...`) return a string with the output of the command. If you wanted the filenames in a list, you'd have to split up the string yourself.Edit: wrong diagnosis. But I would still use a built-in way such as glob or readdir, rather than invoke an external command.

    Perl already has ways to get a list of filenames. Perhaps the simplest replacement would be:

    @INFFiles = glob($filespec);

    If you use that, you won't need to chomp or prepend $path in the foreach loop.

    Regarding $VersionNumber =~ s/^\d+\.\d+\.\d+\.\d+//g;, I'm not sure if this is a mistake or not, since it will remove everything except digits (0-9), '+' and '.':

    $ perl -ple 's/[^\d+\.\d+\.\d+\.\d+]//g' This has no numbers, dot or plus This has 1 dot. 1. This has 1234567890 + . 1234567890+.

    It can be simplified to:

    $ perl -ple 's/[^.\d+]//g' This has no numbers, dot or plus This has 1 dot. 1. This has 1234567890 + . 1234567890+.

    Since it is being used to clean up a version number, I think it is probably meant to be s/[^0-9.]//g - remove everything except digits and dot. Since you've already stripped everything out of version number, the next two lines (strip \x0d and chomp) seem unnecessary.

    Finally, use strict; and use warnings; at the top of your script ( if you don't already ). And don't use function prototypes ( the $$$ in sub FileVersion($$$) ) - they don't do what you want.

    FalseVinylShrub

    Disclaimer: Please review and test code, and use at your own risk... If I answer a question, I would like to hear if and how you solved your problem.

      No he doesn't have to split the result of `command`. It gets split into lines in list context. The problem is that the newlines stay. So you do have to chomp() the elements.

      Jenda
      Enoch was right!
      Enjoy the last years of Rome.

        Hi

        Oops. Thank you.. Can't believe I didn't know that. Amended post.

        However, now confused as s/he does chomp() the elements, it seems to me. So I can't see what the problem is.

        FalseVinylShrub

        Disclaimer: Please review and test code, and use at your own risk... If I answer a question, I would like to hear if and how you solved your problem.

      Thank you very much for your Clear Explanation.First it gave me error Because Filespec path is wrong.but now $versionNumber is not coming correctly means it is taking another value. For example it is coming as 10.12.12.10 instead of 6.2.2.4.what may be the problem .$version Number is Taking another un related value. some times correct $versionNumber is coming Some times Not.so Initilization of $versionNUmber is Good idea?

        Hi

        No worries...

        Afraid I can't help with your new problem, because (a) I am ill and my brain isn't working (b) it's not clear enough. Try to post a cut-down sample that shows the problem, with sample input and output. See the "How to post a good question" link already posted. You may want to start a new thread, so more people see it.

        FalseVinylShrub

        Disclaimer: Please review and test code, and use at your own risk... If I answer a question, I would like to hear if and how you solved your problem.

Re: Please help me
by TGI (Parson) on Feb 12, 2010 at 15:24 UTC
    Please read How To Ask A Question (Simple English) (RFC).

    Also, it looks like you don't have strict enabled. You should. It will save you hours and hours of work.

    One of the easiest ways to debug a chunk of code is to insert some print or warn statements. Try modifying your code as follows:

    @INFFiles = `dir /b $filespec`; use Data::Dumper; warn "GETTING FILES " warn Dumper \@INFiles; foreach $infFile (@INFFiles) { warn "PROCESSING $infFile";

    You will be able to see what values are being used for @INFiles.


    TGI says moo

      HI

      Thank you for your help. next time onwards i will follow the rules.

        It's always hard when you are working with new tools and reaching out to new people for help. It looks like you got the answer you needed, and some other helpful advice as well.

        Still, writing clear questions with good titles makes it easier for people to help you. The easier it is for others to offer help, the more help you can expect to receive. Sometimes the effort to clarify ones thoughts and compose a good post is enough to let you answer your own question.

        Both questions and answers are necessary parts of Perlmonks. Thank you for your contribution so far. Keep asking, and sooner than you think, you will be able to start answering questions too.

        Welcome to Perlmonks and Perl in general.


        TGI says moo