Professional Web Applications Themes

How do i run shell command - PERL Beginners

-----Original Message----- From: com [mailto:com] Sent: Tuesday, August 31, 2004 12:10 AM To: org Subject: Re: How do i run shell command It works for me . Thanks Sudhakar Gajjala Chris Devers <com> on 08/30/2004 10:47:55 PM Please respond to org To: Sudhakar Gajjala/C/UTStarcomUTStarcom cc: org Subject: Re: How do i run shell command On Mon, 30 Aug 2004 com wrote:   pipe ( |  You realize, of course, that this can be done entirely in Perl ? Quoting from the excellent _Perl Cookbook_: [...] you can emulate wc by opening up and reading the file yourself: open(FILE, "< $file") ...

  1. #1

    Default RE: How do i run shell command




    -----Original Message-----
    From: com [mailto:com]
    Sent: Tuesday, August 31, 2004 12:10 AM
    To: org
    Subject: Re: How do i run shell command


    It works for me . Thanks

    Sudhakar Gajjala





    Chris Devers <com> on 08/30/2004 10:47:55 PM

    Please respond to org

    To: Sudhakar Gajjala/C/UTStarcomUTStarcom
    cc: org
    Subject: Re: How do i run shell command


    On Mon, 30 Aug 2004 com wrote:
     
    pipe
    ( | 

    You realize, of course, that this can be done entirely in Perl ?

    Quoting from the excellent _Perl Cookbook_:

    [...] you can emulate wc by opening up and reading the file
    yourself:

    open(FILE, "< $file") or die "can't open $file: $!";
    $count++ while <FILE>;
    # $count now holds the number of lines read

    Another way of writing this is:

    open(FILE, "< $file") or die "can't open $file: $!";
    for ($count=0; <FILE>; $count++) { }

    If you're not reading from any other files, you don't need the
    $count
    variable in this case. The special variable $. holds the number of
    lines read since a filehandle was last explicitly closed:

    1 while <FILE>;
    $count = $.;

    This reads all the records in the file and discards them.

    But if you really do need to do this via a system command -- you don't,
    but I'll play along -- then the command as you've given it is what is
    known as a Useless Use Of Cat.

    This command --

    cat file | wc -l

    -- is equivalent to this one --

    wc -l file

    -- but the latter invokes less overhead, and so should be a bit faster.

    Unless you really are conCATenating a chain of files together, most
    commands of the form "cat foo | cmd" can be rewritten as "cmd foo" or,
    maybe, "cmd < foo".



    --
    Chris Devers com
    http://devers.homeip.net:8080/blog/

    np: 'It's Not Easy Being Green (lo-fi midi version)'
    by Kermit
    from 'The Muppet Movie Soundtrack'




    Chris,

    You are exactly right, that is a useless use of cat, old habits die
    hard. And of course you are correct in that it can be done entirely in
    perl, the availability of the shell cmd wc makes us lazy, and we don't
    want to code what we can just call from the system.

    Chris Hood



    Christopher Guest

  2. #2

    Default RE: How do i run shell command



    Except laziness is a virtue, this is just insufficient code, not the
    "good" laziness. If you coded it up the way it *should* be done for
    portability, security, proper error handling, etc. it would in the end
    be longer.

    my array;
    tie array, 'Tie::File', $filename or die "Can't tie file: $!";
    my $length = array;

    Tough to beat....

    http://danconia.org
    Wiggins Guest

  3. #3

    Default RE: How do i run shell command

    From: Radhika Sambamurti <mailto:com> wrote:

    : thanks,
    : radhika
    :
    : : If you're not reading from any other files, you don't
    : : need the $count variable in this case. The special
    : : variable $. holds the number of lines read since a
    : : filehandle was last explicitly closed:
    : :
    : : 1 while <FILE>;
    : : $count = $.;
    : :
    : : This reads all the records in the file and discards them.
    : :
    : Hi,
    : was trying to reproduce the code [above].
    : I was wondering what the 1 is doing before the while. Is
    : it the exit status of the while, that is until eof is
    : reached and exit code = 1 ?


    'while' can be used as a statement modifier. When used
    that way, it places each successive value in the $_ variable
    and the line number of the file in the variable $. (And a
    number of other things.)

    If the statement doesn't do anything with $_ and doesn't
    produce any other effect, it becomes irrelevant. 1 and 0
    won't raise errors under strict and warnings.

    0;
    1;
    2; # <--- raises a constant in void context warning.


    HTH,

    Charles K. Clarkson
    --
    Mobile Homes Specialist
    254 968-8328








    Charles Guest

Similar Threads

  1. running shell command within php
    By Andreas Paasch in forum PHP Development
    Replies: 3
    Last Post: November 12th, 10:00 PM
  2. formatting output of `ls -l` shell command
    By lecichy in forum PHP Development
    Replies: 5
    Last Post: October 16th, 02:05 AM
  3. exec shell command from php
    By Gal in forum PHP Development
    Replies: 4
    Last Post: October 2nd, 12:54 AM
  4. Using 'Shell' command in code.
    By Alex Mirzayanov in forum ASP.NET Web Services
    Replies: 1
    Last Post: September 19th, 08:54 AM

Bookmarks

Posting Permissions

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

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139