Beefy Boxes and Bandwidth Generously Provided by pair Networks
Pathologically Eclectic Rubbish Lister
 
PerlMonks  

Re: It's the little things...

by liverpole (Monsignor)
on Mar 02, 2010 at 19:35 UTC ( #826240=note: print w/ replies, xml ) Need Help??


in reply to It's the little things...

    What was your moment where a little thing just popped up and bit you on the toe?

Too many to even scratch the surface :), but one comes to mind...

When I first started with Perl, I would get the base part of a filename with:

my $iam = `basename $0`; chomp $iam;

Which is silly, of course.  Why go to the shell when you've got Perl?

For a long while I then used something I thought much more clever:

(my $iam = $0) =~ s,.*/,,;
Even as I type it now, my fingers still remember it from so much usage.  It worked great, but only in Linux.

I finally started learning more about the available core modules, and now I know the way to go is:

use File::Basename; my $iam = basename $0;

As an aside to your meditation, if you're in Windows, you should be able to type just "filename" (without the .pl extension).  If it doesn't work, you may have to edit your "Advanced system settings" (under System in the Control Panel), and add ";.PL" to the end of the PATHEXT variable in your "System Variables" list.


s''(q.S:$/9=(T1';s;(..)(..);$..=substr+crypt($1,$2),2,3;eg;print$..$/


Comment on Re: It's the little things...
Select or Download Code
Re^2: It's the little things...
by JavaFan (Canon) on Mar 02, 2010 at 23:50 UTC
    Which is silly, of course. Why go to the shell when you've got Perl?
    Perl is the ultimate glue language. You've got Perl. That means, using the shell is easy. It's designed to be easy to use different utilities. Not using the shell for the sake of not using the shell is very unPerlish.

    Now, in this particular case, I probably wouldn't use use the shell either, but there are many cases I don't hesitate to it.

          "Not using the shell for the sake of not using the shell is very unPerlish"

      <Sigh> -- Not exactly what I meant, but because you may have misunderstood my intended message, let me rephrase it:  Why go to the shell when you can do it just as easily in Perl?

      Of course I use the shell when it makes sense.  Without hesitation.  But it's not a good idea (for a number of reasons) to get in the habit of blindly running to the shell without considering the alternatives.  One reason the comes to mind is portability.  Another is speed.

      And yes, I'm aware that in a little program that uses the shell for nothing other than stripping the basename from its own path there is unlikely to be noticeably slower than its alternative.  But what if your script is so useful that some other program now calls it thousands or millions of times?  And some other program calls that one thousands or millions of times?  Eventually you run the risk of encountering leaky abstractions.

      I'll confess, there are plenty of times I've done:

      % cat somefile | grep somestring

      But I still try to be aware that it's overkill, just like instinctively using the shell (when you don't necessarily have to) can be.


      s''(q.S:$/9=(T1';s;(..)(..);$..=substr+crypt($1,$2),2,3;eg;print$..$/
Re^2: It's the little things...
by Lady_Aleena (Chaplain) on Mar 03, 2010 at 00:52 UTC
    As an aside to your meditation, if you're in Windows, you should be able to type just "filename" (without the .pl extension). If it doesn't work, you may have to edit your "Advanced system settings" (under System in the Control Panel), and add ";.PL" to the end of the PATHEXT variable in your "System Variables" list.

    Even though I am not too put out by having to type the filename with the extension, I took your advice but need to reboot. Thanks!

    Gives liverpole cookies!

    Have a nice day!
    Lady Aleena
Re^2: It's the little things...
by ambrus (Abbot) on Mar 03, 2010 at 12:44 UTC

    You can't prove it on me, because I never gave that code to anyone and deleted it ages ago, but when I started to write programs with perl, I had a bot that popened to netcat to perform a http request. I've no idea how that must have worked because obviously it has to feed data both to the input of netcat and read its output, so I assume it was something like $resp = `echo 'GET /$parms HTTP/1.1\nHost: example.com\n' | nc example.com 80`. (Note however that there was no many-argument pipe open at that time in perl 5.5005 or 5.6.) That it extracted data from the response html with bad regexen doesn't count as a big sin compared to the above I guess.

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others rifling through the Monastery: (11)
As of 2014-07-29 12:15 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    My favorite superfluous repetitious redundant duplicative phrase is:









    Results (217 votes), past polls