home *** CD-ROM | disk | FTP | other *** search
/ NetNews Usenet Archive 1992 #16 / NN_1992_16.iso / spool / comp / lang / perl / 4863 < prev    next >
Encoding:
Text File  |  1992-07-21  |  2.0 KB  |  62 lines

  1. Newsgroups: comp.lang.perl
  2. Path: sparky!uunet!gatech!psuvax1!flee
  3. From: flee@cs.psu.edu (Felix Lee)
  4. Subject: Perl meets shell
  5. Message-ID: <Brs2rD.Fz1@cs.psu.edu>
  6. Sender: news@cs.psu.edu (Usenet)
  7. Nntp-Posting-Host: dictionopolis.cs.psu.edu
  8. Date: Wed, 22 Jul 1992 07:45:05 GMT
  9. Lines: 51
  10.  
  11. I've been slowly collecting a pile of Perl routines that I want to use
  12. in both shell scripts and Perl scripts.  But it's rather awkward to
  13. arrange for this to happen.
  14.  
  15. Presumably, in Perl, you would do something like
  16.     require 'fqdn.pl';
  17.     $y = &fqdn($x);
  18.  
  19. And in shell you would do
  20.     y=`fqdn $x`
  21. or
  22.     cat list | fqdn
  23.  
  24. Supporting the shell usage requires writing a wrapper that applies the
  25. routine to @ARGV or <STDIN>.  The awkward part is this same wrapper
  26. has to be written for every little routine in the library.
  27.  
  28. Instead, what I have right now are two Perl programs, "perl-apply" and
  29. "perl-map", that can be used like so:
  30.     y=`perl-apply fqdn $x`
  31.     cat list | perl-map fqdn
  32.     perl-map fqdn `cat list`
  33.  
  34. "perl-apply proc args" locates proc, loads it, calls proc with all the
  35. args, and prints the result.
  36.  
  37. "perl-map proc args" locates proc, loads it, calls proc on each arg
  38. (or on each line of STDIN), and prints each result on a separate line.
  39.  
  40. This still isn't ideal.  The shell usage is somewhat messy.
  41.  
  42. It would be nice if the same file could be used for both purposes.
  43. This requires a way for the file to know whether it's being run as a
  44. script, or loaded with "require" (or "do").
  45.  
  46. Here's one approach.  If you put something like
  47.     #!/usr/bin/wrapper
  48. at the top of the file, this will get ignored.  Unfortunately, the
  49. wrapper has to be a binary executable.
  50.  
  51. Perhaps the wrapper could be Perl itself, with a new flag.
  52. Something like:
  53.     #!/usr/bin/perl -Emain
  54. The "-Emain" means that the procedure "main" should be called after
  55. the file is loaded, after all the immediate statements are executed,
  56. before exiting.
  57.  
  58. Does this seem reasonable?  It still seems messy.
  59.  
  60. (Why doesn't Unix fit together smoothly on all levels? sigh.)
  61. --
  62.