Beefy Boxes and Bandwidth Generously Provided by pair Networks
There's more than one way to do things
 
PerlMonks  

comment on

( [id://3333]=superdoc: print w/replies, xml ) Need Help??
Dear Monks,

Below some code, where i've (out of habit) used a couple of UN*X utilities to ease the job; Now i may have to deploy this code on MSWin PCs (without these utils, -- and i don't want to bundle freeware versions of the utils with the code).

So i've written some Perl alternatives for the "inlined" ls, sed and cat calls, but these are certainly not the only - and probably not the best - way to skin these cats. Any suggestions for improvement ?

#___________________________________________________________ #1A: Read files sorted ascending on timestamp @files = `ls -t -1 -r $dir`; # strip ./../ & process .msg/.txt #1B: Same using plain vanilla Perl ... foreach my $g qw(msg txt) { push @files, glob("$dir\\*.$g"); } my %files = map { $_, -M} @files; @files = reverse (sort { $files{$a} <=> $files{$b} } keys %files); #___________________________________________________________ #2A: Strip $author} line system "sed s/{author}.*//g <$file >${file}1..txt"; ($TRACE>=2) and system "cat ${file}1.txt"; #2B: Same using plain vanilla Perl ... open (FH, "<${file}.txt") or die "Can't open ${file}.txt: $!\n"; open (FH1, ">${file}1.txt") or die "Can't create ${file}1.txt: $!\n"; while (<FH>) { next if (/{author}/); print FH1; ($TRACE>=2) and print; } close (FH) or die "Can't close ${file}.txt: $!\n"; close (FH1) or die "Can't close ${file}1.txt: $!\n"; #___________________________________________________________ #3A: ($TRACE>=2) and system "cat VSort.txt"; #3B: Same using plain vanilla Perl ... if ($TRACE>=2) { open (FH, "<$file") or die "Can't open $file: $!\n"; while (<FH>) { print; } close(FH) or die "Can't close $file: $!\n"; }


Best regards,
Allan Dystrup

============================================================
As the eternal tranquility of Truth reveals itself to us, this very place is the Land of Lotuses
-- Hakuin Ekaku Zenji

In reply to UN*X ls, sed, cat... in Perl by ady

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



  • Are you posting in the right place? Check out Where do I post X? to know for sure.
  • Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
    <code> <a> <b> <big> <blockquote> <br /> <dd> <dl> <dt> <em> <font> <h1> <h2> <h3> <h4> <h5> <h6> <hr /> <i> <li> <nbsp> <ol> <p> <small> <strike> <strong> <sub> <sup> <table> <td> <th> <tr> <tt> <u> <ul>
  • Snippets of code should be wrapped in <code> tags not <pre> tags. In fact, <pre> tags should generally be avoided. If they must be used, extreme care should be taken to ensure that their contents do not have long lines (<70 chars), in order to prevent horizontal scrolling (and possible janitor intervention).
  • Want more info? How to link or How to display code and escape characters are good places to start.
Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others studying the Monastery: (7)
As of 2024-03-28 19:47 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found