in reply to Re: calling a perl file from a perl script
in thread calling a perl file from a perl script

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.
  • Comment on Re^2: calling a perl file from a perl script

Replies are listed 'Best First'.
Re: calling a perl file from a perl script
by jonadab (Parson) on Apr 06, 2007 at 20:03 UTC
    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.
Re^3: calling a perl file from a perl script
by swares (Monk) on Apr 06, 2007 at 21:18 UTC
    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"; }