www.webdeveloper.com
Results 1 to 5 of 5

Thread: I have inheritied a script and dont know what this line does :)

  1. #1
    Join Date
    Dec 2005
    Posts
    108

    I have inheritied a script and dont know what this line does :)

    Hi,

    I have a perl script which is used to calculate the amount of code churn in perforce (a type of code change contol system);

    The system uses the command line to output commands and then grep the results to get the appropiate data.

    There is one line however which really confuses me Its this:

    Code:
    $numlines = `p4 print $filename#$revChglistTable{$filename} | wc -l`;
    chomp($numlines);
    I am not sure what the | wc -1 part and the chomp() does. the

    Code:
    p4 print $filename#$revChglistTable{$filename}
    Part is the command output, so you dont really need to worry about that. The question I need answered is:

    |
    Code:
     wc -1
    Is this a perl thing? What does it do? Whenever I run the script I get the following debug message in command prompt:

    Code:
    'wc' is not recognized as an internal or external command,
    operable program or batch file.

    BTW I am using active perl on windows

    Thanks!

  2. #2
    Join Date
    Aug 2004
    Location
    Ankh-Morpork
    Posts
    19,176
    "wc" is a UNIX/Linux command that gets info on a file (the "-l" says you want a line count), so probably isn't going to work on a Win platform unless you have some sort of UNIX command emulator installed.
    "Please give us a simple answer, so that we don't have to think, because if we think, we might find answers that don't fit the way we want the world to be."
    ~ Terry Pratchett in Nation

    eBookworm.us

  3. #3
    Join Date
    Aug 2004
    Location
    Ankh-Morpork
    Posts
    19,176
    Here's an alternative way to get the line count:
    Code:
    open(FILE, "< $filename#$revChglistTable{$filename}") or die "can't open $filename: $!";
    for ($numlines=0; <FILE>; $numlines++) { }
    # you shouldn't need a chomp() in this case
    Above derived from http://www.oreilly.com/catalog/cookb...pter/ch08.html
    "Please give us a simple answer, so that we don't have to think, because if we think, we might find answers that don't fit the way we want the world to be."
    ~ Terry Pratchett in Nation

    eBookworm.us

  4. #4
    Join Date
    Dec 2005
    Posts
    108
    Great, thanks alot, Im glad I invetigated it further because it turns out the tool was producing incorrect data because this wasnt working on windows.

    Basically, I want to execture the external command:

    Code:
    'p4 print $filename#$revChglistTable{$filename}';
    in perl and read back the number of lines it produces. For example if I typed in the command

    Code:
    p4 print //depot/foo#2
    the file contents will be output on the command line. I'm not sure how to execture external commands like this in perl, or indeed how to read the contents back into the script (so I can count the number of lines etc)

    Thanks!
    Last edited by idn; 02-13-2006 at 02:17 PM.

  5. #5
    Join Date
    Feb 2006
    Posts
    198
    executing an external command in perl is quite straight forward. say for example you wanted a directory listing (windows or unix):

    $directory_listing = `dir /home/me`;

    The backticks indicate that the text be executed as an external command and the output be stored in the variable $directory_listing

    Diarmuid Ryan
    Web Design Ireland

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center



Recent Articles