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??
hello raywood and welcome to the monastery and to the wonderful world of Perl!

well learning by doing is a great thing but before you must learn by reading.

Read solid and idiomatic example similar to your need is the first step to end with a fluent Perl.

I always suggest the read of Perl Cookbook; it is a book a little bit aged but contains perfectly valid perl solution to common problems with clear explainations. If you want to mitigate the age of the book take you free copy of Modern Perl book: as suggested is a great book that discert about Perl following it's principles and concepts.

You took an example by a poweful monk: i'm sure it is from BrowserUk because of the almost-signature perl -slw and it is wise to choose good exmamples but it is even wiser to understand deeply what the code really do (for instance -s perl switch can be useful while testing a script but can introduce very strange behaviors.. are you aware of them? no? do not use it!).

I say this because it is better have your own simple code and submit it here for revision, code that you understand fully, that code by someone else if you only understand it for an half.

Now answers:

1) RAM is always involved and generally it is no good to waste it: that said I guess ancient WordStar (Oh WordStar I remember it: was the first word processor I used it on the 286 of my father) file will never eat your 16Gb RAM. Anyway when your slurp entirely a file it goes entirely in the RAM used by your perl process (with the little overhead of the variable that contains it). So it genarally wiser to process a file line by line than slurping it.

Try it yourself observing the following two programs in the process manager

this slurp the file

use strict; use warnings; my $file = $ARGV[0]; open my $fh, '<', $file or die "cannot open $file"; # pause some second to go to inspect the process manager sleep 5; # the diamond op <> slurp the whole file when in list context (the arr +ay to the left force list context) my @array_of whole_file = <$fh>; sleep 5;

while this iterates over it

use strict; use warnings; my $file = $ARGV[0]; open my $fh, '<', $file or die "cannot open $file"; # pause some second to go to inspect the process manager sleep 5; # the diamond op is now in scalar context while (my $line = <$fh>){ # do noting 1; } sleep 5;

Try these programs passing to them a 1Mb, 100Mb, 1Gb text file files and observe what happens

2) to adapt your program to process different files the simplest solution is to pass them as arguments in the command line: they will fill the @ARGV array. See perlvar for it.

So you can have something like:

foreach my $file (@ARGV){ # check it exists.. die "$file does not exists!" unless -e $file; print "start processing $file\n"; # send the file to your own sub for processing my_custom_process_sub($file); ...

3) Every Perl release is backward compatible so pick the more modern with a even minor version ie: 5.24

HtH

L*

There are no rules, there are no thumbs..
Reinvent the wheel, then learn The Wheel; may be one day you reinvent one of THE WHEELS.

In reply to Re: Learning Perl by Doing.. Learn by reading too by Discipulus
in thread Learning Perl by Doing by raywood

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 sharing their wisdom with the Monastery: (2)
As of 2024-04-26 00:37 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found