Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl Monk, Perl Meditation
 
PerlMonks  

Korn Shell Wrappers for Perl Module Functions?

by sierrathedog04 (Hermit)
on Jul 10, 2001 at 20:14 UTC ( #95345=perlquestion: print w/replies, xml ) Need Help??

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

As a Perl monk I like to program in Perl. If a function must be reusable than I simply place it in a .pm

Recently however a user asked me to to write a Korn shell function to do something that I would rather do in Perl. The problem is that I am not allowed simply to pass parameters through the Korn shell to the Perl function, e.g., I cannot simply write a Korn shell script that says: `myperlmodulewrapper.pl myshellparam`or something like that, since myshellparam will be visible to those who do a `ps` from the shell.

If I have to I will take out my O'Reilly Learning the Korn Shell and write this function purely as a Korn shell script. But I would rather come up with a simple Korn shell function that in turn calls a Perl module or Perl script, and does so without passing parameters through the shell to Perl.

Is what I would like to do possible?

Replies are listed 'Best First'.
Re: Korn Shell Wrappers for Perl Module Functions?
by lhoward (Vicar) on Jul 10, 2001 at 20:37 UTC
    How about passing the arguments as environmental variables?

    doit.ksh

    #!/bin/ksh export FOO=123 export BAR=234 ./doit.pl
    doit.pl
    #!/usr/bin/perl -w use strict; #do something with $ENV{FOO} and $ENV{BAR}
Re: Korn Shell Wrappers for Perl Module Functions?
by Malkavian (Friar) on Jul 10, 2001 at 20:33 UTC
    One way or another, the script you call will be visible to a ps command.
    If, however, you're not worried about the script itself showing up, but the argument list you send to it, then the solution is to use the environment space in the shell.
    The first part of the shell script sets the environment arguments, then you call the wrapper script, which then pulls those values out of ENV space.
    You'll then see the scriptname showing up on a ps with no args
    It's kludgy and nasty, but it'll do the job.
    This may not be what you really wanted, but it's a thought.. :)

    Cheers,

    Malk
Re: Korn Shell Wrappers for Perl Module Functions?
by Anonymous Monk on Jul 10, 2001 at 20:45 UTC
    How about
    echo Var1=$var1 Var2=$var2|perlscript
    ? You can see the environmentspace with ps too.
      Perhaps what the ps is showing is the args to echo, not the args to the perl script?

        For most shells, 'echo' is a built-in and so doesn't require a different process and so what would show up in 'ps' output would be 'ksh' and the arguments passed to it. So the arguments to 'echo' shouldn't show up in the 'ps' output. This is the method I would use.

                - tye (but my friends call me "Tye")
Re: Korn Shell Wrappers for Perl Module Functions?
by runrig (Abbot) on Jul 10, 2001 at 20:36 UTC
    If they want a Korn shell function to execute from a shell script, then it may be more efficient to just do it as a Korn shell function. It'll be one less process to spawn. Then again, depending on the function maybe it would be worthwhile to write the Perl script.

    You could pass arguments by environment variables, of course, but command line args are usually better especially if you want to optionally run this script from the command line. Or you could take command line args if they're there, but default to the %ENV hash if there are no command line args.

    Besides, 'ps -f' on my system anyway, would show that it IS a perl script running no matter what argument passing you used. (Or are you just worried about the parameters showing?)

      Chances are though to do anything much more complicated than addition the korn shell script will have to fork off many processes for things like sed, tr, etc.

      --
      perl -pe "s/\b;([st])/'\1/mg"

Re: Korn Shell Wrappers for Perl Module Functions?
by sierrathedog04 (Hermit) on Jul 10, 2001 at 21:05 UTC
    Update: Yes, I am worried about the parameters showing up when someone runs 'ps'. It is a matter of complete indifference to me if users can merely tell that I am running Perl behind the scenes. The suggestion that I use environmental variables sounds promising. My understanding is as follows:
    • My understanding is that if a Korn shell sets an environmental variable and then calls a Perl script the environmental variable will be visible to the Perl script.
    • The same is true in reverse. If a Perl script sets an environmental variable and then exits then the Korn script that called the Perl script can also see that environmental variable.
    • Also, if a Korn shell script sets an environmental variable then only someone logged in as that user can see the variable.
    • Finally, concurrent users will not confuse the shell. Each environmental variable will exist only within the process that called it.

    As they say at the Paris Perlmongers meetings, 'N'est-ce que pas?" Is what I have said about environmental variables in Korn and Perl true?

    Recursive Update: Clearly there is more involved here than simply swapping environmental variables to and from Korn and Perl. Therefore, the best solution given the user requirement to provide a Korn shell function is simply to do the whole shmear in Korn.

    By the way, it occurred to me that the Korn shell would have had more success as a scripting language if the developer had named it something imaginative like 'Tortilla'.

      Not true. Environmental variables are passed from shell to perl, not back (Correctly from parent to child). Environmental variables could be seen from all users per ps, but are not shown by default. Two points out of four. See here!
        Environment vars will only be seen if you pipe them to the script, as you've done in your link.
        However, if you set them on a line by line basis in a shell script they won't be visible.

        Malk.

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://95345]
Approved by root
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others chanting in the Monastery: (7)
As of 2020-05-30 05:52 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    If programming languages were movie genres, Perl would be:















    Results (171 votes). Check out past polls.

    Notices?