Beefy Boxes and Bandwidth Generously Provided by pair Networks
Keep It Simple, Stupid
 
PerlMonks  

Re: Using system (); with Strawberry Perl

by eyepopslikeamosquito (Archbishop)
on Nov 25, 2021 at 00:14 UTC ( [id://11139093]=note: print w/replies, xml ) Need Help??


in reply to Using system (); with Strawberry Perl

Sorry, but calling grep and mv and cat in this style from a driving Perl script via system (without even checking for errors) is just too weird for me. :) I've seen people use a shell script driver to call Perl scripts, but not the reverse.

Anyways, if this came up in a code review at work, you would be told to write the whole thing in Perl. See Unix shell versus Perl for why.

Replies are listed 'Best First'.
Re^2: Using system (); with Strawberry Perl
by LanX (Saint) on Nov 25, 2021 at 00:25 UTC
    Actually that's similar to how I "migrated" from Bash to Perl back then ...

    Cruel code! ;-)

    Cheers Rolf
    (addicted to the Perl Programming Language :)
    Wikisyntax for the Monastery

Re^2: Using system (); with Strawberry Perl
by Fletch (Bishop) on Nov 25, 2021 at 01:58 UTC

    There was one "perl script" at $job several years back which was basically a bash script with every line wrapped in backticks . . . *shudder*

    The cake is a lie.
    The cake is a lie.
    The cake is a lie.

      It's awful, but it does work :-)
Re^2: Using system (); with Strawberry Perl
by cavac (Parson) on Nov 30, 2021 at 10:01 UTC

    For me, a mixture of Bash and Perl sometimes makes sense when writing a quick proof of concept. If the main part of the code works out, i can always go to CPAN, find a module that fits my need, read the docs and then implement it.

    But for a first try, it's often quicker to just calling wget than it is to install and use LWP::UserAgent. And for a PoC, it really doesn't matter if bash calls wget and then the perl script, or if the perl script calls wget when needed. It's just throw-away code anyway.

    Case in point: Perl+PostgreSQL+GeoIP = Awesome! was my proof of concept, which then turned into GeoIP revisited. The proof of concept was ugly and was using external (command line) tools. But it quickly showed me what worked and what didn't. And it was "good enough" to post on PM to get some feedback from which i learned that i was using a deprecated GeoIP database version. Which then lead to a "Pure Perl" version that was nicer, but also more time intensive to implement.

    perl -e 'use Crypt::Digest::SHA256 qw[sha256_hex]; print substr(sha256_hex("the Answer To Life, The Universe And Everything"), 6, 2), "\n";'

      There is a vast gulf between your proof of concept bash script and the OP's code! I see your bash script, for example, calling:

      rm GeoIPCountryCSV.zip
      I do not see your Perl script calling:
      system ("rm GeoIPCountryCSV.zip");

      Look at the OP's code again. Have you ever seen anything like it? I haven't, which is why I asked for the backstory behind it. Sadly, it looks like we'll never know because the OP seems to have vanished (presumably forever) from the Perl Monks universe. This is a tragedy because learning the backstory would make the description of the why behind this code much more interesting when it's deservedly viewed by a wider audience.

      BTW, given your interest in using a proof of concept, you might be interested in a three-part series I wrote a few years ago: Building the Right Thing (Part I): Pretotyping

        Uhm, i grepped through parts of my codebase and there is this little gem:

        system "aplay /home/cavac/src/pagecamel_dsky/masteralarm.wav &";

        Turns out, playing sound on a Raspberry Pi 2(!) is hard, especially from Perl.

        And yes, i have seen stuff like OP's code before. Many times. Many, many, times. Unfortunately. At least here on PM this is something that classes under "needs rewrite". I also develop Arduino stuff, and there a source code in that kind of quality would probably class as "made by an expert"...

        perl -e 'use Crypt::Digest::SHA256 qw[sha256_hex]; print substr(sha256_hex("the Answer To Life, The Universe And Everything"), 6, 2), "\n";'

      While I understand your sentiment and have done similar myself there is an inherent need to realise that doing such is pure hackery. In this particular case hadrons is shelling out to perform things like rename, unlink and so on which is arguably more work (because of shell escapes, etc) than just running the Perl built-ins. It is also highly relevant that it is this practice of shelling out alone which has given hadrons such problems that they have needed to come here seeking advice in the first place.

      But for a first try, it's often quicker to just calling wget than it is to install and use LWP::UserAgent.

      Can't say I agree. If LWP isn't already installed you may as well do so now because you are going to need it sooner or later. It's also pretty simple to use. However, if even that is too much trouble, then consider HTTP::Tiny which is in core so you already have it.


      🦛

        LWP/wget might not be the best example, i agree. (Although, unless something has changed in the last year or so, the installed LWP doesn't work by default because the ::https module is missing).

        perl -e 'use Crypt::Digest::SHA256 qw[sha256_hex]; print substr(sha256_hex("the Answer To Life, The Universe And Everything"), 6, 2), "\n";'
Re^2: Using system (); with Strawberry Perl
by hadrons (Novice) on Nov 25, 2021 at 17:58 UTC
    I'm aware its poor programming, but the results work

      Except that they don't - otherwise you wouldn't be here asking for help to fix it. ;-)


      🦛

        Well, sort of :-) ... it's an issue of the platform. If I could stick to cygwin it would be fine, but I needed to move the script to a Windows based on. But, I'm not going to deny I'm at a disadvantage defending this coding either.

        Well...it worked until it didn't...
        I *think* that's what was meant :P

      I admire your honesty. Do you have any interest in writing solid code?

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://11139093]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others sharing their wisdom with the Monastery: (1)
As of 2024-04-25 00:25 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found