LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices

Reply
 
Search this Thread
Old 08-31-2010, 11:43 PM   #16
Feynman
Member
 
Registered: Aug 2010
Distribution: Gentoo
Posts: 62

Original Poster
Rep: Reputation: 15

Ok, for starters, this is the same project I have been working on since I started posting on this website. I want to be able to read a template, replace specified strings with values defined as the arguments of some subroutine, and then print the output to a new file. Then I want to pass the new file to an other program that generates an output file. Then I want to parse the output file and extract useful numbers and tables. Then I want to have the ability of sending those to another template file or some other subroutine, or simply print them out on the screen.

I decided it would be almost essential to have all the files that result from one template file (the input and output files) in one directory. So, my first thought was, "all my friends use these classes and objects, perhaps I could define a class that has the path to the template file, all the strings to look for, and all the corresponding values they should be replaced with as objects." That seemed like a good solution until I needed to make a unique directory for the files. I thought about using PID, but what if my code made a bunch of such class members (i.e. processed several template files and looked at data found in several of the corresponding output files)? Then, in theory, the PID could be recycled and used for more than one of these template processing procedures. But whether that is true or not (this was just a concern I had), I realized the perfect solution was to create a directory named after the object that contained the template file. I posted on stackoverflow about this, and they all said this was nearly impossible in perl and my approach was probably wrong. One of them suggested a hash of objects. I had learned about hashes by now (as I used them to store strings to look for = values to replace them with) and I had used hashes of hashes of hashes already (this is actually slightly more complicated than I explained, but this is getting long enough). I had actually considered this and after that suggestion I decided this must be the best approach.

And that brings me here...
 
Old 08-31-2010, 11:50 PM   #17
Feynman
Member
 
Registered: Aug 2010
Distribution: Gentoo
Posts: 62

Original Poster
Rep: Reputation: 15
You said my statement did not indicate the need for a package and a file would do. I did not know that. Can I use subroutines stored in another file without making it a package? If so--and you think this is better--I see no reason why not.
 
Old 08-31-2010, 11:53 PM   #18
Sergei Steshenko
Senior Member
 
Registered: May 2005
Posts: 4,481

Rep: Reputation: 453Reputation: 453Reputation: 453Reputation: 453Reputation: 453
Quote:
Originally Posted by Feynman View Post
You said my statement did not indicate the need for a package and a file would do. I did not know that. Can I use subroutines stored in another file without making it a package? If so--and you think this is better--I see no reason why not.
Yes, you can.

Try to understand my example in http://www.linuxquestions.org/questi...0/#post4084259 post - the point is that a reference can be exported by one file and imported by another.

After that we'll talk how to extend the example to multiple subroutines. Or you decide you actually like the idea to store one subroutine per file.
 
Old 08-31-2010, 11:56 PM   #19
Feynman
Member
 
Registered: Aug 2010
Distribution: Gentoo
Posts: 62

Original Poster
Rep: Reputation: 15
Ok thank you. I will get back to you tomorrow. It is getting late.
 
Old 08-31-2010, 11:59 PM   #20
Sergei Steshenko
Senior Member
 
Registered: May 2005
Posts: 4,481

Rep: Reputation: 453Reputation: 453Reputation: 453Reputation: 453Reputation: 453
Quote:
Originally Posted by Feynman View Post
Ok thank you. I will get back to you tomorrow. It is getting late.
OK; also think that a filesystem directory is a natural container of residing in it files, and the directory name may reflect commonality of the files functionality. It's WRT to storing one subroutine per file.
 
Old 09-01-2010, 11:24 AM   #21
Feynman
Member
 
Registered: Aug 2010
Distribution: Gentoo
Posts: 62

Original Poster
Rep: Reputation: 15
Ok, I did your example and I see your point. It still seems to me like a waste of a perl file to just store one subroutine there--not to mention extra writing for each line of code when it is referred to.

In short, I still think the best approach is to have one file with all the subroutines needed make input files from templates. There really are not that many--they just require some thought in coding. This way I have them all stored in one place--and I do not have to keep saying "require." This would make working with a few dozen templates much easier. Granted, I may not get to this point, but in theory working with over a dozen template files at once would seem quite reasonable for doing thorough investigations of large molecules (again, I am using this program to run input files containing information about molecules on quantum chemistry software that calculates properties of said molecules.)

This way, whenever I am working on a new job with molecules, all I have to do is say "use inputgenerator" and I have all the subroutines necessary to make my input files. I will later get to the point where I say "use generalparsers" and I will have all the subroutines I need to parse the output files.
 
Old 09-01-2010, 01:39 PM   #22
Sergei Steshenko
Senior Member
 
Registered: May 2005
Posts: 4,481

Rep: Reputation: 453Reputation: 453Reputation: 453Reputation: 453Reputation: 453
Quote:
Originally Posted by Feynman View Post
Ok, I did your example and I see your point. It still seems to me like a waste of a perl file to just store one subroutine there--not to mention extra writing for each line of code when it is referred to.

In short, I still think the best approach is to have one file with all the subroutines needed make input files from templates. There really are not that many--they just require some thought in coding. This way I have them all stored in one place--and I do not have to keep saying "require." This would make working with a few dozen templates much easier. Granted, I may not get to this point, but in theory working with over a dozen template files at once would seem quite reasonable for doing thorough investigations of large molecules (again, I am using this program to run input files containing information about molecules on quantum chemistry software that calculates properties of said molecules.)

This way, whenever I am working on a new job with molecules, all I have to do is say "use inputgenerator" and I have all the subroutines necessary to make my input files. I will later get to the point where I say "use generalparsers" and I will have all the subroutines I need to parse the output files.
You do not have to keep saying "require" - Perl has 'opendir', 'readdir', and the latter is normally called in a loop. So 'require' will appear once in the loop body.

Another point of my code example is that the exported reference is anonymous. I.e. since it has no name, it by definition can't conflict with any other name - unlike traditional named subroutine.

The fundamental idea with anonymous references is that they get name (if any) in the places where they are used, not in the place where the object (in English sense) they point to is defined/described.
 
Old 09-01-2010, 04:38 PM   #23
Feynman
Member
 
Registered: Aug 2010
Distribution: Gentoo
Posts: 62

Original Poster
Rep: Reputation: 15
Here is what I typed in:

use strict;
use warnings;

require 'sub.pl';
'sub.pl'->('10speed705');

and an error came up. How do I only say "require" once?
 
Old 09-01-2010, 04:48 PM   #24
Sergei Steshenko
Senior Member
 
Registered: May 2005
Posts: 4,481

Rep: Reputation: 453Reputation: 453Reputation: 453Reputation: 453Reputation: 453
Quote:
Originally Posted by Feynman View Post
Here is what I typed in:

use strict;
use warnings;

require 'sub.pl';
'sub.pl'->('10speed705');

and an error came up. How do I only say "require" once?
How about first publishing all the sources, i.e. the source of 'sub.pl' ?

Is there a reason for changing my code ?
 
Old 09-01-2010, 05:01 PM   #25
Feynman
Member
 
Registered: Aug 2010
Distribution: Gentoo
Posts: 62

Original Poster
Rep: Reputation: 15
I was trying to use require only once. You said that was possible. I had also tried:
use strict;
use warnings;

(require 'sub.pl')->('10speed705');
('sub.pl')->('10speed705');

which did not work either. I managed to get my hash of hashes working. I have it as a package. I will change it to a file, but this "require" method seems like more typing than it is worth--and a lot more files (since each would have its own subroutine).

I am happy with the result as is--unless you think it would be better I use the "require" method.
 
Old 09-01-2010, 05:02 PM   #26
Feynman
Member
 
Registered: Aug 2010
Distribution: Gentoo
Posts: 62

Original Poster
Rep: Reputation: 15
What do you mean by pushing sources? I have seen "push" used for arrays, but how are you suggesting it be implemented for this task?
 
Old 09-01-2010, 05:07 PM   #27
Sergei Steshenko
Senior Member
 
Registered: May 2005
Posts: 4,481

Rep: Reputation: 453Reputation: 453Reputation: 453Reputation: 453Reputation: 453
Quote:
Originally Posted by Feynman View Post
I was trying to use require only once. You said that was possible. I had also tried:
use strict;
use warnings;

(require 'sub.pl')->('10speed705');
('sub.pl')->('10speed705');

which did not work either. I managed to get my hash of hashes working. I have it as a package. I will change it to a file, but this "require" method seems like more typing than it is worth--and a lot more files (since each would have its own subroutine).

I am happy with the result as is--unless you think it would be better I use the "require" method.
Did you try my code as is ?

Your

Code:
('sub.pl')->('10speed705');
makes no sense - because '->' is dereferencing and "('sub.pl')" is not a reference. The code I published does make sense - I copy-pasted the screen session, you can verify yourself that the code works. In my code I do dereference a reference. I again suggest first understanding every detail of my code.

Last edited by Sergei Steshenko; 09-01-2010 at 05:08 PM.
 
Old 09-01-2010, 05:08 PM   #28
Sergei Steshenko
Senior Member
 
Registered: May 2005
Posts: 4,481

Rep: Reputation: 453Reputation: 453Reputation: 453Reputation: 453Reputation: 453
Quote:
Originally Posted by Feynman View Post
What do you mean by pushing sources? ...
You wrote

Code:
require 'sub.pl'
, so I want to see the contents of 'sub.pl' file.
 
Old 09-01-2010, 05:35 PM   #29
Feynman
Member
 
Registered: Aug 2010
Distribution: Gentoo
Posts: 62

Original Poster
Rep: Reputation: 15
Here is sub.pl:

use strict;
use warnings;

sub {
my ($person) = @_;
warn "Hello $person";
};

and yes, I tried your code as is. It worked fine. I was trying to figure out how to add another line that did not use the "require" statement.

Last edited by Feynman; 09-01-2010 at 05:37 PM.
 
Old 09-01-2010, 05:50 PM   #30
Sergei Steshenko
Senior Member
 
Registered: May 2005
Posts: 4,481

Rep: Reputation: 453Reputation: 453Reputation: 453Reputation: 453Reputation: 453
Quote:
Originally Posted by Feynman View Post
Here is sub.pl:

use strict;
use warnings;

sub {
my ($person) = @_;
warn "Hello $person";
};

and yes, I tried your code as is. It worked fine. I was trying to figure out how to add another line that did not use the "require" statement.
First, a cosmetic note - .pl extension is for Perl scripts; .pm extension is for Perl modules, your 'sub.pl' as well as my 'sub.prl' is neither Perl script nor Perl module, and that's why I chose a different extension.

Now back to the core issues - when require'd in 'main' what kind of thing does your 'sub.pl' and my 'sub.prl' return ? Justify your answer, i.e. describe how 'require' works.
 
  


Reply

Tags
class, hash, object, package, perl


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Perl hash of hashes props666999 Programming 2 09-07-2006 05:43 AM
Using hash value as key for other hash in Perl scuzzman Programming 6 02-14-2006 06:08 PM
Perl and Hash automatic PB0711 Programming 3 09-23-2005 03:14 AM
Generating hash in perl ? MikeFoo1 Programming 3 05-21-2005 07:03 PM
Hash-sort like in PERL in C++ nyk Programming 4 06-11-2004 09:40 AM


All times are GMT -5. The time now is 03:43 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration