Find the answer to your Linux question:
Results 1 to 5 of 5
have another query I want to call a function/subroutibe test_fucn from test.cgi for that I am doing following #!/usr/bin/perl -w use strict; require "./test.cgi"; for my $reslt (@values) { if ...
  1. #1
    Linux Newbie
    Join Date
    Jan 2008
    Posts
    114

    How to use a subroutine in mutiple perl scripts

    have another query

    I want to call a function/subroutibe test_fucn from test.cgi for that I am doing following

    #!/usr/bin/perl -w
    use strict;

    require "./test.cgi";

    for my $reslt (@values) {
    if ( $rslt eq "red" ) { next; }
    print "Value find";
    &test_fucn;

    }


    But problem - It is giving me whole printable output from test.cgi which is not a part of test_fucn.

    any suggestions, so that I can use only subroutine ?
    Switched to Scripting

  2. #2
    Linux Engineer wje_lf's Avatar
    Join Date
    Sep 2007
    Location
    Mariposa
    Posts
    1,192
    You have two choices, and I don't think you will like either of them. :)

    You can modify test.cgi, so that it contains nothing but functions, nothing that actually calls those functions.

    You can copy the function you want to elsewhere, either in your main Perl script, or to some other file which you require instead of test.cgi.
    --
    Bill

    Old age and treachery will overcome youth and skill.

  3. #3
    Linux Newbie
    Join Date
    Jan 2008
    Posts
    114
    Ya, i already know this but the major.

    This is not possible in actual that function is very big and calling many other(in same script) and I am creating a script which is I am going to put in cron job to run on regular basis.
    Switched to Scripting

  4. #4
    Linux Engineer wje_lf's Avatar
    Join Date
    Sep 2007
    Location
    Mariposa
    Posts
    1,192
    Ok, let's call your new script, the one that contains the require statement, "fred".

    Create a script which is just like test.cgi, except it contains basically only function definitions. Make sure you put all the function definitions there, and nothing else but the
    Code:
    #!/whatever/perl
    at the beginning. We'll call this new script "barney".

    Then, inside fred, require barney instead of test.cgi.

    Then remove those function definitions from test.cgi. (Make sure you lay a copy of it aside, just in case!) Then, in test.cgi, require barney.

    That way you have only one copy of the functions to maintain in case you wish to change (or fix) any of them.

    This work for you?
    --
    Bill

    Old age and treachery will overcome youth and skill.

  5. #5
    Linux Newbie
    Join Date
    Jan 2008
    Posts
    114
    I used some other way

    Instead of calling that function I called the whole script and passed parameter and it works for me..

    Thanks
    Switched to Scripting

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
...