in reply to Possible Improvement to Shell.pm

Does this seem like a good idea? If not, why not?

As far as I can tell it doesn't deal with name clashes. What happens if you have a command foo_bar and one foo-bar?

And what's up with all the other meta characters that are allowed in the shell, but not in perl? (I don't know the shell very well, but I could imagine that '+' might be one of those.

Replies are listed 'Best First'.
Re^2: Possible Improvement to Shell.pm
by plobsing (Friar) on Jul 08, 2008 at 16:46 UTC
    As far as name clashes go: you're explicitly asking for one command, so thats probably the one you want to use. I would also tend to question the sanity of a system that had programs with such similar names yet differing functionality.

    I hadn't thought of '+', because it hadn't come up for me yet. Thanks.

      What happens if you have two commands that get 'perlified' to the same name?

      use Shell 'foo.bar', 'foo-bar';

      Perhaps the import should die in that case. To accomodate this need, perhaps you could allow an explicit mapping?

      use Shell # Command # sub name ['foo.bar' => 'foo_dot_bar' ], [ 'foo-bar' => 'foo_dash_bar'], [ $^O =~ /Windows/ ? 'copy' : 'cp' => 'cp' ];


      TGI says moo

        Thats a better solution to the problem (mine causes c++ to become 'c__', which is just too cryptic).

        If explicit remapping were available, should the automagic remapping of ugly characters to underscore be kept? I'm leaning towards no.

        Also, why choose an alist over a hash? Just a style thing? (Update: nevermind, I see now that it allows you to map a shell name to multiple perl names elegantly)

        Update 2: Here's the patch:
        --- Shell.pm.old 2008-07-08 12:09:00.000000000 -0700 +++ Shell.pm 2008-07-08 19:28:17.000000000 -0700 @@ -21,8 +21,14 @@ @EXPORT = 'AUTOLOAD'; } foreach my $sym (@EXPORT) { + my ($shellsym, $perlsym); + if (ref $sym and ref $sym eq 'ARRAY') { + ($shellsym, $perlsym) = @$sym; + } else { + $shellsym = $perlsym = $sym; + } no strict 'refs'; - *{"${callpack}::$sym"} = \&{"Shell::$sym"}; + *{"${callpack}::$perlsym"} = \&{"Shell::$shellsym"}; } }
      As far as name clashes go: you're explicitly asking for one command, so thats probably the one you want to use

      Very good. Obviously I don't know enough about Shell.pm, so disregard my comment ;-)

      I would also tend to question the sanity of a system that had programs with such similar names yet differing functionality.

      Me too, but as a module author you're in no position to judge your user's sanity, or the sanity of their systems.

      I hadn't thought of '+', because it hadn't come up for me yet. Thanks.

      If you want to do it properly, either look into the source code of a POSIX compatible shell or in the POSIX standard itself to get a list of all characters that can be used in commands without the need for escaping. On thinking a bit more I remember that there's /usr/bin[ on my system (for stuff like if [ .. ], so there could well be many other allowed characters.

      Maybe just substitute anything but [a-zA-Z0-9_] with underscores?