Q-1) Is this approach fine or is there any better way to do it?
It's fine so long as your dataset is regular and well-formed. If it starts to become less so there are other options: use a different format (XML, JSON, YAML, etc.), use a module (say Text::xSV or Text::CSV_XS), pre-process it, etc.
Q-2) Will it be better to embed the data in input.txt in the code itself rather than keeping it in a separate file?
Almost invariably no. Do you use a version control system? Hopefully the answer is "yes" in which case you would be committing deltas every time you change the dataset if it were embedded in the source. Equally, anyone else using your code would have to fire up an editor and know enough perl to be able to change the data without messing up the script syntax. Far, far better to have the data separated from the code.
The only time it is a good idea to embed data in code is for testing (including SSCCE). Your test data may not change (usually should not change) as if it did your test results might not be conclusive or reproducible.
All IMHO, of course.
In reply to Re: What is better: Static input data in separate file or embedding static input data in code.
by hippo
in thread What is better: Static input data in separate file or embedding static input data in code.
by Perl300
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |