Sorry to be on the simple side, but I've just recently entered the monastery and not shortly before just picked up my first book on perl. I am in something of a healthy discussion with a colleague of mine also new to perl. A need came up for a script to take all files within a directory that have a name of an 10.x.x.x ip address and copy the file with a name of the corresponding hostname. I know that perl's mantra is 'theres more than one way to do it' but his method seems to not take advantage of perl's traits and makes a lot of system calls:
#!/usr/bin/perl -w system("ls -1 10.* > hostfilelist"); open (LIST, "hostfilelist"); push(@entrys, <LIST>); foreach my $entry (@entrys) { open (CURRENT, "$entry"); my @contents = <CURRENT>; @results = grep {/hostname/} @contents; #print $results[0]; @outputname = split (/ / , $results[0]); #print $outputname[1]; system("touch $outputname[1]"); open (OUTPUTFILE, ">$outputname[1]"); print OUTPUTFILE "@contents"; close (LIST); close (CURRENT); close (OUTPUTFILE); unlink ("hostfilelist"); }
my version of the same process is as follows:
#!/usr/bin/perl -w use strict; my @entrys; my $entry; my $host; #to specify the directory, just uncomment the next line and put in the #real path to the dir, then comment out, or erase the opendir line tha +t #references "." #opendir DIR, "/path/to/dir/with/configs" or die "Cant open dir $!\n"; opendir DIR, "." or die "Cant open dir $!\n"; @entrys = readdir DIR; #uncomment the next line if you are specifying a differenet directory #other than the pwd. #chdir "/path/to/dir/with/configs" or die "Cant change directory $!\n" +; foreach my $entry (@entrys) { next if ( $entry !~ /^10/ ); open CURRENT, $entry or die "Cant open file $entry $!\n"; while (<CURRENT>) { $host = $_; if ( $host =~ /^hostname/ ) { $host =~ s/^hostname\s(.*)/$1/; chomp $host; open OUTPUTFILE, ">$host" or die "Cant create file $!\n"; print OUTPUTFILE <CURRENT>; close OUTPUTFILE; } } close CURRENT; }
he argues that my script is longer and takes more time and memory. i see his as making several system calls and multiple arrays. granted both work and neither would rock the processor, but i am really looking for some kind of justification that i am on the right path towards improving what skills i have and thinking about more than just "did it work?".

humbly, -c


In reply to A question of efficiency by c

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.