<Help> How to use routines from another perl script

Discussion in 'Perl Misc' started by baiyanhuang@gmail.com, Oct 6, 2008.

  1. Guest

    Hi, All,

    I am just a novice to perl, I want to reuse a routine wrote in one
    perl script to all other perl scripts, just like c, c++ do, but I
    don't know how to "include" another perl script into current perl
    script to utilize the routines. would anyone give some tips on it?

    Thanks so much.

    Baiyan
     
    , Oct 6, 2008
    #1
    1. Advertising

  2. wrote:
    >I am just a novice to perl, I want to reuse a routine wrote in one
    >perl script to all other perl scripts, just like c, c++ do, but I
    >don't know how to "include" another perl script into current perl
    >script to utilize the routines. would anyone give some tips on it?


    Typically you would create a module and import its functions into your
    main program using 'use'.

    There is also 'do' which is kind of the poor man's 'use'.

    jue
     
    Jürgen Exner, Oct 6, 2008
    #2
    1. Advertising

  3. wrote:
    > Hi, All,
    >
    > I am just a novice to perl, I want to reuse a routine wrote in one
    > perl script to all other perl scripts, just like c, c++ do, but I
    > don't know how to "include" another perl script into current perl
    > script to utilize the routines. would anyone give some tips on it?
    >
    > Thanks so much.
    >
    > Baiyan


    A simplest way is to write routines into separate files

    --- example (/var/myroutines/ex1.pl) ---
    1;
    sub myfunc1 {
    my ($param1,$param2) = @_;
    my $to_return='';
    # some code here
    return $to_return;
    }
    --- example ---

    and "include" it in main program like this:

    #!/usr/bin/perl
    use strict;
    require "/var/myroutines/ex1.pl;
    my $val = myfunc1(1, 'abc');

    --
    Petr Vileta, Czech republic
    (My server rejects all messages from Yahoo and Hotmail.
    Send me your mail from another non-spammer site please.)
    Please reply to <petr AT practisoft DOT cz>
     
    Petr Vileta \(fidokomik\), Oct 7, 2008
    #3
  4. Tim Greer Guest

    Petr Vileta (fidokomik) wrote:

    > wrote:
    >> Hi, All,
    >>
    >> I am just a novice to perl, I want to reuse a routine wrote in one
    >> perl script to all other perl scripts, just like c, c++ do, but I
    >> don't know how to "include" another perl script into current perl
    >> script to utilize the routines. would anyone give some tips on it?
    >>
    >> Thanks so much.
    >>
    >> Baiyan

    >
    > A simplest way is to write routines into separate files
    >
    > --- example (/var/myroutines/ex1.pl) ---
    > 1;


    ^ Down there, maybe --> ?

    > sub myfunc1 {
    > my ($param1,$param2) = @_;
    > my $to_return='';
    > # some code here
    > return $to_return;
    > }


    1;

    > --- example ---
    >
    > and "include" it in main program like this:
    >
    > #!/usr/bin/perl


    use warnings;

    > use strict;
    > require "/var/myroutines/ex1.pl;


    || die, or die, or use eval.


    --
    Tim Greer, CEO/Founder/CTO, BurlyHost.com, Inc.
    Shared Hosting, Reseller Hosting, Dedicated & Semi-Dedicated servers
    and Custom Hosting. 24/7 support, 30 day guarantee, secure servers.
    Industry's most experienced staff! -- Web Hosting With Muscle!
     
    Tim Greer, Oct 7, 2008
    #4
  5. Tim Greer wrote:
    > Petr Vileta (fidokomik) wrote:
    >
    >> --- example (/var/myroutines/ex1.pl) ---
    >> 1;

    >
    > ^ Down there, maybe --> ?
    >


    Yes, of course ;-) But for some reason (I forgot why) I tend to write it to top
    of file.

    >> sub myfunc1 {
    >> my ($param1,$param2) = @_;
    >> my $to_return='';
    >> # some code here
    >> return $to_return;
    >> }

    >
    > 1;
    >
    >> --- example ---
    >>


    > use warnings;
    >


    use strict;
    no warnings;

    :)

    >> require "/var/myroutines/ex1.pl;

    >
    >>> die, or die, or use eval.


    Of course, "die" should be there, but this is a fast-written example only.

    --
    Petr Vileta, Czech republic
    (My server rejects all messages from Yahoo and Hotmail.
    Send me your mail from another non-spammer site please.)
    Please reply to <petr AT practisoft DOT cz>
     
    Petr Vileta \(fidokomik\), Oct 7, 2008
    #5
  6. Tim Greer Guest

    Petr Vileta (fidokomik) wrote:

    > Tim Greer wrote:
    >> Petr Vileta (fidokomik) wrote:
    >>
    >>> --- example (/var/myroutines/ex1.pl) ---
    >>> 1;

    >>
    >> ^ Down there, maybe --> ?
    >>

    >
    > Yes, of course ;-) But for some reason (I forgot why) I tend to write
    > it to top of file.
    >
    >>> sub myfunc1 {
    >>> my ($param1,$param2) = @_;
    >>> my $to_return='';
    >>> # some code here
    >>> return $to_return;
    >>> }

    >>
    >> 1;
    >>
    >>> --- example ---
    >>>

    >
    >> use warnings;
    >>

    >
    > use strict;
    > no warnings;
    >
    > :)
    >
    >>> require "/var/myroutines/ex1.pl;

    >>
    >>>> die, or die, or use eval.

    >
    > Of course, "die" should be there, but this is a fast-written example
    > only.
    >


    All humor aside, it's a good idea to give examples that are correct and,
    if you can, add some common error checks. I wrote a pretty lengthly
    blog article about this and why it's a good idea (it basically prevents
    follow up questions when your example doesn't work for the OP).
    --
    Tim Greer, CEO/Founder/CTO, BurlyHost.com, Inc.
    Shared Hosting, Reseller Hosting, Dedicated & Semi-Dedicated servers
    and Custom Hosting. 24/7 support, 30 day guarantee, secure servers.
    Industry's most experienced staff! -- Web Hosting With Muscle!
     
    Tim Greer, Oct 7, 2008
    #6
  7. Hans Mulder Guest

    Tim Greer wrote:
    > Petr Vileta (fidokomik) wrote:


    >> #!/usr/bin/perl

    >
    > use warnings;
    >
    >> use strict;
    >> require "/var/myroutines/ex1.pl;

    >
    > || die, or die, or use eval.


    Why use "die? "Require" already "die"s when something is wrong.

    Were you thinking of "do", perhaps?

    -- HansM
     
    Hans Mulder, Oct 7, 2008
    #7
  8. Tim Greer Guest

    Hans Mulder wrote:

    > Tim Greer wrote:
    >> Petr Vileta (fidokomik) wrote:

    >
    >>> #!/usr/bin/perl

    >>
    >> use warnings;
    >>
    >>> use strict;
    >>> require "/var/myroutines/ex1.pl;

    >>
    >> || die, or die, or use eval.

    >
    > Why use "die? "Require" already "die"s when something is wrong.
    >
    > Were you thinking of "do", perhaps?
    >


    I was actually making a point about the error (typo) he made in his
    example... as in "what did you want it to do here?" in an attempt at
    humor.

    Notice:

    require "/var/myroutines/ex1.pl;

    is missing the closing ". Sorry, that probably came out more sarcastic
    than humorous (it was meant in a lighthearted way), but I was serious
    about checking the calls.
    --
    Tim Greer, CEO/Founder/CTO, BurlyHost.com, Inc.
    Shared Hosting, Reseller Hosting, Dedicated & Semi-Dedicated servers
    and Custom Hosting. 24/7 support, 30 day guarantee, secure servers.
    Industry's most experienced staff! -- Web Hosting With Muscle!
     
    Tim Greer, Oct 8, 2008
    #8
  9. Tim Greer wrote:
    > Notice:
    >
    > require "/var/myroutines/ex1.pl;
    >

    Oh so ;-( I must change my glasses I think ;-)
    --
    Petr Vileta, Czech republic
    (My server rejects all messages from Yahoo and Hotmail.
    Send me your mail from another non-spammer site please.)
    Please reply to <petr AT practisoft DOT cz>
     
    Petr Vileta \(fidokomik\), Oct 9, 2008
    #9
  10. On 2008-10-07 23:01, Tim Greer <> wrote:
    > Hans Mulder wrote:
    >> Tim Greer wrote:
    >>> Petr Vileta (fidokomik) wrote:
    >>>> require "/var/myroutines/ex1.pl;
    >>>
    >>> || die, or die, or use eval.

    >>
    >> Why use "die? "Require" already "die"s when something is wrong.
    >>
    >> Were you thinking of "do", perhaps?
    >>

    >
    > I was actually making a point about the error (typo) he made in his
    > example... as in "what did you want it to do here?" in an attempt at
    > humor.
    >
    > Notice:
    >
    > require "/var/myroutines/ex1.pl;
    >
    > is missing the closing ".


    That, however, would be caught at compile time, so the presence or
    absence of "die" wouldn't make a difference.

    hp
     
    Peter J. Holzer, Oct 11, 2008
    #10
  11. Tim Greer Guest

    Peter J. Holzer wrote:

    > On 2008-10-07 23:01, Tim Greer <> wrote:
    >> Hans Mulder wrote:
    >>> Tim Greer wrote:
    >>>> Petr Vileta (fidokomik) wrote:
    >>>>> require "/var/myroutines/ex1.pl;
    >>>>
    >>>> || die, or die, or use eval.
    >>>
    >>> Why use "die? "Require" already "die"s when something is wrong.
    >>>
    >>> Were you thinking of "do", perhaps?
    >>>

    >>
    >> I was actually making a point about the error (typo) he made in his
    >> example... as in "what did you want it to do here?" in an attempt at
    >> humor.
    >>
    >> Notice:
    >>
    >> require "/var/myroutines/ex1.pl;
    >>
    >> is missing the closing ".

    >
    > That, however, would be caught at compile time, so the presence or
    > absence of "die" wouldn't make a difference.
    >
    > hp


    Yes it would. And indeed, die wouldn't make a difference. This is why
    I mentioned the attempt at humor. Pardon that it just caused
    confusion.
    --
    Tim Greer, CEO/Founder/CTO, BurlyHost.com, Inc.
    Shared Hosting, Reseller Hosting, Dedicated & Semi-Dedicated servers
    and Custom Hosting. 24/7 support, 30 day guarantee, secure servers.
    Industry's most experienced staff! -- Web Hosting With Muscle!
     
    Tim Greer, Oct 14, 2008
    #11
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Wet Basement
    Replies:
    1
    Views:
    2,593
    BobMonk
    Jul 15, 2003
  2. dpackwood
    Replies:
    3
    Views:
    1,869
  3. Petterson Mikael

    Execute another perl script from my perl script

    Petterson Mikael, Jan 5, 2005, in forum: Perl Misc
    Replies:
    3
    Views:
    155
    Paul Lalli
    Jan 5, 2005
  4. Replies:
    20
    Views:
    880
    Gunnar Hjalmarsson
    Jan 18, 2005
  5. vikrant
    Replies:
    4
    Views:
    296
Loading...

Share This Page