Beefy Boxes and Bandwidth Generously Provided by pair Networks
Clear questions and runnable code
get the best and fastest answer
 
PerlMonks  

Re: calling a perl file from a perl script

by jonadab (Parson)
on Apr 06, 2007 at 19:45 UTC ( [id://608736]=note: print w/replies, xml ) Need Help??


in reply to calling a perl file from a perl script

I am pretty new to perl and this might be a stupid question

Actually, if anything, the question is probably harder to answer than it needs to be, because of the confusing way in which you stated it. Nonetheless, I'll try...

how can i separate my code in pieces and run them all from one file

There are several ways to go about this, depending on what it is you want to accomplish. do '/path/to/filename.pl' for instance is not entirely the same as require '/path/to/filename.pl'. (One difference is that the latter won't run the contents of the file twice if you have the same require statement in two different places.) For a beginner, require may be close to what you want, but it's hard to be sure without knowing more about your situation. Be aware that the required file should finish with a true value. There is also use, of course, not to mention various more obscure possibilities (such as open FOO, $filename and eval scalar <FOO>).

-- 
We're working on a six-year set of freely redistributable Vacation Bible School materials.

Replies are listed 'Best First'.
Re^2: calling a perl file from a perl script
by hodashirzad (Novice) on Apr 06, 2007 at 19:51 UTC
    well i am creating a crawler that gets product prices and compares them with each other I thought it would be good if I have separate file for each part of my code like to get a link list of the categories one code and for getting a link list of the products one and so on.
      I thought it would be good if I have separate file for each part of my code like to get a link list of the categories one code and for getting a link list of the products one and so on

      That almost sounds like modules. I think I'd recommend creating your functions to do those things you describe in one or more .pm files ("modules"), placing them in one of the site-perl directories listed in @INC, and then loading them with use from any program that wants to use them.

      Modules you're only going to use locally don't have to be held up to the same exacting standards you would do if you were going to publish them to the CPAN for the world to use. A module that's only going to be used by just one app is not really significantly harder to create than any other Perl script. You don't have to mess with package namespaces, for instance, especially for a small application: you can just define your functions in the normal way and let them live in the main namespace. You don't need any more POD for a module of this kind than you would for any other code. I do recommend the use strict pragma, but I'd recommend that even for files you were going to require.

      -- 
      We're working on a six-year set of freely redistributable Vacation Bible School materials.
        Thanks a lot for your help I will look into creating modules and start from there.
      Ok, here is one of the more obscure methods of doing that I think. One that I would have used long ago if I had know how to. You can store your code in files as anonymous subs. You might take a look at Perl POE for examples. http://poe.perl.org On the POE site there is an example job server you can pass code_refs to, which would work with this. Then you can load your code from where ever into a hash so you can use a dispatch table. Found this bit on some node on PM here recently.
      my %dispatch_table = ( some_name_one => \&do_something, some_name_two +=> \&do_something_else ); ($dispatch_table{"$some_name"} || sub { print "no command found\n" })- +>($my_args,@into_sub);
      Here is an example of a plugin loader I'm working on for a IRC Bot.
      # the code is loaded into $code from a DB in my case # and it loops around the sub below to load all plugins. # In your file you would have an anonymous sub routine # like { my $times=shift; for (1...$times) { print "Hello, World!\n"; +} } # check reference type my $eval_code = sub { $code }; my $chk_ref=ref $eval_code; if ($chk_ref !~ /CODE/) { print "[load_plugins] Error loading plugin $name: Not CODE!\n"; } else { # test code $eval_code = eval "sub { $code }"; } if ($@) { chomp($@); print "[load_plugins] Error loading plugin $name: $@\n"; } else { # store code in a hash $code{$name} = ( { id => $id, name => $name, code => $eval_code, }, ); print "[load_plugins] Plugin $name loaded successfully.\n"; }

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others rifling through the Monastery: (3)
As of 2024-04-24 20:53 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found