Beefy Boxes and Bandwidth Generously Provided by pair Networks
Don't ask to ask, just ask
 
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

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://582397]
Approved by GrandFather
Front-paged by derby
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others goofing around in the Monastery: (2)
As of 2024-04-25 19:08 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found