LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   Python/Perl Command Equivalents (https://www.linuxquestions.org/questions/programming-9/python-perl-command-equivalents-898357/)

Sergei Steshenko 08-21-2011 11:53 AM

Quote:

Originally Posted by Woodsman (Post 4449367)
... Bottom line is if IT personnel do not support Perl or Python, or refuse to support, then portability is a moot issue. ...

You probably don't care about what IT people support - Perl can be used under both Linux and Windows without being installed centrally. For Windows have a look at http://strawberryperl.com -> http://strawberryperl.com/download/5...0-portable.zip .

I.e. under Windows you'll need a .bat script that would bootstrap everything as needed.

Proud 08-21-2011 11:59 AM

FWIW Windows Script Host can do Javascript/JScript out of the box as well as VBScript (plus scripting engines for perl, python, etc). And there's Windows PowerShell with a v2 for newer OSs. Also C#/.Net stuff can be crossplatform with Mono iirc.
Something written in perl/python/Java should be able to be almost write once, run anywhere. At least just between Windows and linux on x86 machines.

As a syntax I say that python example with common words is far easier to read than the perl use of characters. Understanding of either does completely depend on knowledge of the language.
Breaking things between language version 2 and 3 doesn't mean they don't pay a lot of attention to regression testing.
I've never quite got the hard-on some people have for closures/anonymous functions/lambdas in imperative languages. Nor do I see a problem with return statements. Seems there's been decades and many successful languages that have things the way you don't prefer, so can we all be so wrong? :)
Yes CPAN is well known as a valuable resource that any language would be blessed to have.
Apparently there's no written spec or standard for perl prior to 6, which isn't ready yet. ;)
As for ~250MB data structures in python, I'd say it's possibly not really the best tool for that job, and that there's the C API/bindings of the mainstream implementation CPython. Also, EVE Online's python/stackless python.
I don't think anyone's going to disagree that no language is perfect, some aim for more practical features, others to stick to more abstract or theoretical ideals of computation expression. Always good to check you're up to date on the state of different tools, sorry for any derailment OP.

Let us know if/when you get those firm answers about language options and user interaction and if you still need help.

Edit: Sergei, is there really an issue about a feature being in the language itself vs the standard library?
And the OP may very well care what IT say even if they can be technically sidestepped, for support and legal reasons.

Sergei Steshenko 08-21-2011 12:22 PM

Quote:

Originally Posted by Proud (Post 4449424)
...
I've never quite got the hard-on some people have for closures/anonymous functions/lambdas in imperative languages. ...

Anonymity means less broken naming conventions and names contention. As I wrote, the consuming scope, i.e. the end user decides on names, so developers of "modules" (not Perl modules, just reusable pieces of code) don't care about each others naming decisions.

Closures is a fundamental expression of stateful (opposed to stateless) code. All those fancy OO data class members are just variables at outer scopes of closures.

I came to all this not at school, but through practice - luckily, met a couple of younger guys who suggested to try, and after trying I realized I really liked them - they made life easier. Also, not only my life. When, say, 20 people on project need to write configuration files, and I work as an integrator, my own interest is to make sure that by construction there are no contentions.

MTK358 08-21-2011 12:52 PM

Quote:

Originally Posted by Sergei Steshenko (Post 4449415)
Oh, really ?! How about:

http://docs.python.org/library/array.html :

In Perl arrays are built-in, come with language, not with a module.

I think that they're called "lists" in Python. And AFAIK they are part of the language, they even have their own syntax:

Code:

mylist = ["foo", "bar"]
Quote:

Originally Posted by Proud (Post 4449424)
I've never quite got the hard-on some people have for closures/anonymous functions/lambdas in imperative languages. Seems there's been decades and many successful languages that have things the way you don't prefer, so can we all be so wrong? :)

A lot of problems can be made a lot simpler with closures and first-class functions. I really wish that more common languages would support them. And I really wonder why they don't, I can't believe that it's that difficult. I even implemented them in my own working interpreter, and it wasn't really complicated.

Woodsman 08-21-2011 01:29 PM

Quote:

You probably don't care about what IT people support
I care in as much that ignoring IT policies could lead to termination. :)

Quote:

Perl can be used under both Linux and Windows without being installed centrally.
If you mean a Perl script can be compiled into a stand-alone app, then that is good to know. I notice Strawberry Perl is available as a portable app. I also notice something called Perl2Exe. Yet even in those cases, installing software in an enterprise network could lead to termination. I don't want this thread to be about office politics, but cooperating with IT personnel is presumed in these discussions. :)

Bear in mind too that if money is involved then the IT folks become a party in the solution because such software needs evaluation and approval. Popular scripting languages are free/libre software and potentially avoid much of that red tape.

With that said, generally I prefer the principle of "seeking forgiveness rather than asking permission." :) One method to bypass locked systems is a USB flash drive. I have suggested that option if the IT people refuse to let us install Cygwin --- we can run the scripts from a USB stick.

Another option is to install to a shared network directory that belongs to the group. Generally IT people let users in such groups do as they please inside those directories. I have suggested that option too.

Another option is to run the Cygwin environment and scripts from user's home directories, although that consumes more network drive space than a common installation.

All of these work-arounds require scripts to know path locations for any such unique installation. As long as any such work-around does not introduce viruses, spyware, or anything else that raises the ire of the IT people, then likely there are no repercussions and everybody lives happily ever after. :)

Quote:

FWIW Windows Script Host can do Javascript/JScript out of the box as well as VBScript (plus scripting engines for perl, python, etc). And there's Windows PowerShell with a v2 for newer OSs. Also C#/.Net stuff can be crossplatform with Mono iirc.
I am aware that Windows Script Host supports JavaScript, but if I am cornered into supporting a native Windows method then I'll have to stick with VB Script. I have no experience with JavaScript. Although I'm rusty after several years I have sufficient experience with the similar Visual Basic for Applications (VBA). I don't know whether PowerShell is an option. As I mentioned previously I have yet to receive a reply about which scripting options these folks support. From what I have read thus far, porting my shell script to PowerShell will be much work. Like Perl and Python, I have to learn PowerShell syntax from scratch, whereas VB Script will be more or less familiar.

Yet even then, my script uses external tools such as awk and sed. I would have to simulate those external calls as internal functions or find a way to run the GNUWin32 equivalent of those tools from within any script. So once again I'm back to installing software unless I can perform everything internally in the ported script. If installing software is approved, then I'll just stick with the Cygwin software.

With respect to this thread, I basically wanted to know how much is involved to port a shell script to a different scripting language on a different operating system. Doable, but lots of work. :) With respect to my other thread, I have a solution in place (Cygwin), but now wait for approval.

Sergei Steshenko 08-21-2011 01:52 PM

Quote:

Originally Posted by Woodsman (Post 4449470)
I care in as much that ignoring IT policies could lead to termination. :)


If you mean a Perl script can be compiled into a stand-alone app, then that is good to know. I notice Strawberry Perl is available as a portable app. I also notice something called Perl2Exe. Yet even in those cases, installing software in an enterprise network could lead to termination. I don't want this thread to be about office politics, but cooperating with IT personnel is presumed in these discussions. :) ...

No, I don't mean that "a Perl script can be compiled into a stand-alone app". I mean that portable Perl does not require installation in central places, but that it can be put into any directory an run from there, and one doesn't even need to have Administrator privileges to do that.

Sergei Steshenko 08-21-2011 01:54 PM

Quote:

Originally Posted by Woodsman (Post 4449470)
...
Yet even then, my script uses external tools such as awk and sed. ...

Whatever 'awk' and 'sed' do can be done by Perl built-in functions without spawning child processes.

Proud 08-21-2011 02:24 PM

Yes I thought we'd covered that almost everything you're using common utilities for can be expressed within each programming language. And probably not much work, generally you just have a little more declaration of variables and flow control, a little more verbosity about which features/functions you're using.

I'm not getting why something in e.g. Java would care how one has named one's methods and variables within a useful library any more than they would if it were in perl. Pass the arguments as however you've named them, be oblivious to how the method's implemented, assign the result as you wish. The only thing seems to be the name of the method, and that's down to having a sane creator of the library you're using. As for naming contention, namespaces. If you really want to rename the method, subclass, wrapper method with the name you want just passing the arguments to the other method, done?
In a similar vein, Java seems fine with interfaces and anonymous classes whenever anonymous functions are needed.
There must be some class of algorithm that I'm just not used to, that works best with passing function references around, rather than just object references to implementing class instances.

I still can only think that Sergei's talk of arrays in python is the lack of control around consecutive memory locations, to perform in-place inserts/sorts in the most manual and efficient manner. Beyond that, afaik python gives you several sequential mutable data types that you can place empty or null values in, but just can't micromanage the memory for behind the scenes. Well, unless you subclass them iirc.

I don't know anywhere that's got VB of any flavour as an Invest tech, thought I'm sure there's some. I'd strong suggest you at least look at C# if you're going with anything using the .Net framework & libraries.
I have little experience of it, but Cygwin sounds a rather hefty solution to your problem, though as you say you can leave your script as is, calling those common separate utilities.

Sergei Steshenko 08-21-2011 03:09 PM

Quote:

Originally Posted by Proud (Post 4449496)
...
I'm not getting why something in e.g. Java would care how one has named one's methods and variables within a useful library any more than they would if it were in perl.
...

Translate this:


Code:

use List::Util 'shuffle';

use strict;

my $_number_of_retries = 10;
my $_network_timeout = 60;

sub
  {
  my(
    $info_marker,
    $system_wrapper_sub,
    $filename_only,
    $base_urls_array_ref
    ) = @_;

  my @base_urls = shuffle(@{$base_urls_array_ref});

  ################################
  foreach my $base_url(@base_urls)
  ################################
    {
    my $full_url = "$base_url/$filename_only";

    if(
      $system_wrapper_sub->
        (
        $info_marker,
        "\\wget -nv -c -t $_number_of_retries -T $_network_timeout $full_url"
        ) == 0
      )
      {
      if(-z $filename_only)
        {
        unlink($filename_only);
        next;
        }

      if(-e $filename_only)
        {
        return 0; # success
        }
      }
    } # foreach my $base_url(@base_urls)

  1; # failure
  }; # sub

into Java - the code returns an anonymous code reference and does not pollute global namespace. The code is used like this (assuming the code resides in 'function.prl' file) :

Code:

my $f = require '/full/path/to/function.prl';
# function call through $f follows

Pay attention that there are two variables outside the function body: '$_number_of_retries', '$_network_timeout', but still they can not have a contention with other variables with the same name - because of Perl scoping rules.

This is a piece of code I'm using in my tool building stuff from sources.

Proud 08-21-2011 03:30 PM

What global namespace is there to pollute? Stuff in Java is always in a class, constants and methods are imported and references to them get qualified as required if there's contention.

Your 2 variables outside the function would probably be object variables instantiated for each independant use of this method that you'd need, or just static variables for the class the methods are defined in. I don't see the problem your syntax is solving, perhaps Java doesn't have it to start with, being OO the way it is and compiled to bytecode.

Again, define this class as implementing whatever interface your code with the $f equiv needs, and you can pass it an object instance that has the one or more methods, with you naming the reference as you wish. Not seeing the big win here. No you can't pass completely arbitrary objects around (well without deeming them simply of type Object and later casting them back to classes/interfaces you know of), but that's stopping you mess something up, instead you define the interface aka method signatures the passed object must implement, and beyond that people are free to implement those as they wish, using whatever variable and private method naming schemes, and your caller need know or care nothing about them, while the compiler checks at compile time that you've not passed the wrong reference.

If you want to get more into arbitrary method calls, Java has reflection, you can list an object's method names/signatures as text and then build a call to them dynamically afaik. Couples in with web services and other remote method invocation iirc.

I'm no expert, I already said no language is perfect, but I don't see how perl's solving a problem that Java's stumped by here. And strong typing and compiler-time checks are good.

Sergei Steshenko 08-21-2011 04:03 PM

Quote:

Originally Posted by Proud (Post 4449538)
What global namespace is there to pollute? Stuff in Java is always in a class ...

Gotcha. Class names are global. I.e. using class names causes a headache of having their names unique.

And if you are going to tell me there are namespaces on top of classes, I'll tell you that namespaces are global, i.e. using namespaces causes a headache of having their names unique.

Languages without anonymity are defective by design.

MTK358 08-21-2011 04:32 PM

@Proud

This is slightly unrelated, but still relavent:

One thing I don't like about many OOP languages without first class functions if that they force OOP down your throat when first-class functions would make it so much easier.

For example, in an OOP language, if you wanted to make a customizable sorting function, you would have to define a class (that pollutes the global namespace) that has a comparison method, create an instance of it, and pass it to your sorting function.

In a language with first-class functions, you just pass an anonymous function, like this:

Code:

my_sorting_function(func (a, b) <do comparison here> endfunc, my_array)
Isn't it far simpler and more elegant?

EDIT: If the language supports closures, another advantage is that the sorting function can use variables in the scope in which it was created. Try doing that in Java.

Sergei Steshenko 08-21-2011 04:44 PM

Quote:

Originally Posted by Proud (Post 4449538)
... No you can't pass completely arbitrary objects around (well without deeming them simply of type Object and later casting them back to classes/interfaces you know of), but that's stopping you mess something up, instead you define the interface aka method signatures the passed object must implement, and beyond that people are free to implement those as they wish, using whatever variable and private method naming schemes, and your caller need know or care nothing about them, while the compiler checks at compile time that you've not passed the wrong reference.
...

This has nothing to do with anonymity. OCaml, for example, is a strictly typed languages - much stricter than Java, OCaml doesn't even have RTTI if any kind, but it still has anonymous functions and lexical scoping. And no 'return' statement - everything is an expression.

---------- Post added 08-22-11 at 12:45 AM ----------

Oh, and Java-7 finally has closures (IIRC).

And Scala is functional - there should be a way of modernizing Java world.

Woodsman 08-21-2011 05:22 PM

Quote:

I mean that portable Perl does not require installation in central places, but that it can be put into any directory an run from there, and one doesn't even need to have Administrator privileges to do that.
Okay. That matches my comments about installing to a USB stick, or user home or group network directory.

Quote:

Whatever 'awk' and 'sed' do can be done by Perl built-in functions without spawning child processes.
Yes, I think we pretty much have established that. I was just being verbose. :)

kbp 08-21-2011 06:03 PM

OP: apologies, I didn't mean to get your thread hijacked by a language war


All times are GMT -5. The time now is 07:11 PM.