Beefy Boxes and Bandwidth Generously Provided by pair Networks
Keep It Simple, Stupid
 
PerlMonks  

calling perl subroutine from shell

by alandev (Scribe)
on Nov 06, 2006 at 08:43 UTC ( [id://582397] : perlquestion . print w/replies, xml ) Need Help??

alandev has asked for the wisdom of the Perl Monks concerning the following question:

hi monks

i want to call a subroutine(clear_scr()) in a package say a.pm from a shell script.
(i.e)
i want to directly call a subroutine in a package from a shell script.
is it possible to do it that way ?
thanks

Replies are listed 'Best First'.
Re: calling perl subroutine from shell
by Corion (Patriarch) on Nov 06, 2006 at 08:51 UTC

    This is not possible. The shell doesn't understand Perl. The only way you can do something like this is by invoking Perl as a oneliner for example:

    perl -MMy::Package -e 'My::Package::clear_scr()'

    (I've used My/Package.pm instead of a.pm)

    If you need to pass parameters to your routine, I recommend that you pass them in via @ARGV:

    perl -MMy::Package -e 'My::Package::clear_scr(@ARGV)' wipe

    For more inspiration, see how the Perl installation package uses ExtUtils::Command, which exports such interesting functions as rm_f, mkdir and so on...

Re: calling perl subroutine from shell
by GrandFather (Saint) on Nov 06, 2006 at 08:50 UTC

    If you execute perl -help you will notice "-[mM][-]module  execute `use/no module...' before executing program" among the command line options so:

    perl -Ma -e 'clear_scr()'

    should do what you want assuming a.pm exports clear_scr.

    Update: removed bogus .pm!


    DWIM is Perl's answer to Gödel
Re: calling perl subroutine from shell
by Tomte (Priest) on Nov 06, 2006 at 08:52 UTC

    what you asked for is:

    perl -Ma -e 'a::clear_scr();'
    This starts a new perl process, loads the module a.pm, given it#s location is in @INC, and calls the subroutine clear_scr.

    I have a gut feeling that this is not want you really want, though - if I'm right, you'll have to clarify your intentions.

    regards,
    tomte


    An intellectual is someone whose mind watches itself.
    -- Albert Camus

Re: calling perl subroutine from shell
by dorko (Prior) on Nov 06, 2006 at 13:27 UTC
Re: calling perl subroutine from shell
by pajout (Curate) on Nov 06, 2006 at 13:22 UTC
    Just two details:
    1. You can use
    #!/usr/bin/perl
    as the first line in yourscript.pl, which becomes "executable" - it means that you can run it from command line in the same manner as shell script (don't forget chmod 755, for instance).

    2. Don't use names of your modules starting with lowercase. It can cause collision with perl internals and unexpected behavior as the effect.

Re: calling perl subroutine from shell
by jort (Initiate) on Nov 06, 2006 at 22:29 UTC
    perl -e 'use something; &aroutine(@ARGV); &blah(); $anythingyouwant="h +ere";' *.c
    In this particular case, it sounds like you want:
    perl -e 'use a; &clear_scr()'
      Calling perl subroutine from shell script and pass @ARGV's

      Perl script : a.pl

      #!/usr/bin/perl -w

      use Exporter;
      @ISA = qw(Exporter);
      @EXPORT=qw(a(@ARGV));
      sub a{
      my($a,$b)=@_;

      print $a."\n";
      print $b."\n";
      }
      1;

      Shell script : b.sh
      #/bin/bash -e
      arg1=1;
      arg2=2;
      perl -e "require qw(a.pl) ; a($arg1, $arg2);"

      run ./b.sh

      output :
      1
      2