Beefy Boxes and Bandwidth Generously Provided by pair Networks
Welcome to the Monastery
 
PerlMonks  

Re: What is better: Static input data in separate file or embedding static input data in code.

by Discipulus (Canon)
on Nov 02, 2017 at 21:05 UTC ( [id://1202643]=note: print w/replies, xml ) Need Help??


in reply to What is better: Static input data in separate file or embedding static input data in code.

Hello Perl300,

personally i (ab)use a lot the __DATA__ token. It's useful because can be used as other (even if bareword) filehandles.

It's free because magically appears to be valid as it is encountered in the file.

I use it when data is small or when I need to test some data exceptions so I put there worst cases mixed to some sane data and eventually some invalid row.

When I'm happy I just add a line opening an external file and I put the new (now lexical!) filehandle into the loop.

Also I hate Excel. More than the program in itself I hate the fact many people look at you bad if you do not declare to be an Excel wizard. So when some Excel task is given to me I export to comma separeted plain file and I put that blob under __DATA__ token. Then I excel..

In such, rare cases, i stick with the data into the file because both have no sense without the other.

But generally yes seems a good principle to have data separted from logic.

For default i use sometimes:$ARGV[0]//='input.txt' or $file=$ARGV[0]//='input.txt' for programs where only one argument is admitted. If more I use always Getopt::Long

For small hacks i (ab)use oneliners perl -lane ... does a lot of useful work for you in nothing. For example it auto chomp lines, thing you forgot to do in your above example ;=)

PS another useful thing for data are heredocs: sometimes I use one or more of them to test some XML parser logic extracted from a huge unreadable file.

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.
  • Comment on Re: What is better: Static input data in separate file or embedding static input data in code.
  • Select or Download Code

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://1202643]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others taking refuge in the Monastery: (2)
As of 2024-04-25 05:24 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found