Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl Monk, Perl Meditation
 
PerlMonks  

it is amazing sometimes how little you know...

by Buckaroo Buddha (Scribe)
on Jul 05, 2000 at 17:43 UTC ( #21121=perlmeditation: print w/replies, xml ) Need Help??


i'm passing time here, it's first thing in the morning
i was looking for info on loops (the perl-built in way
to do something different on the first pass only ...
obviously, it isn't important, i could do it manually...
but i like to learn this 'little' stuff

since there is no specific section for 'doing something
differently on the first or last iteration of a loop'
i decided to read the perl pocket ref from page 1 and figure
that i'd stumble across it eventually...

i'd gotten as far as page seven when the world as i knew
it changed dramatically...

i read (for the first time) about the difference between
      '$x' and "$x"

as someone who is (mostly) self-taught in perl i am
always astounded to find these things out
i remember once asking what the difference beween the
single and double quotes were and being told (or at least <italic)hearing</italic>)
'nothing, you can even use que-que-bang (qq!) for the same thing.'

well as it turns out, '$x' = '$x' and "$x" = "how existential"
you see they say that single quotes mean a literal string.

i'm sure many of you know about litteral strings as second
nature but the real truth is, none of us really know how
little we know. Sometimes, when you're lucky enough to
capture a glimpse of that, the possibilities become staggering.

  • Comment on it is amazing sometimes how little you know...

Replies are listed 'Best First'.
RE: it is amazing sometimes how little you know...
by maverick (Curate) on Jul 05, 2000 at 21:27 UTC
    Perl really lends itself to these almost black magic sort of tricks. One of my favorites is =~ returning the back refrences in array context.
    @array = ($string =~ /(\d+)/);
    or taking an element of and array that would have been returned by some function.
    $size = (stat("/etc/passwd"))[7];
    It's the little things that you tend not to do in other languages...
    /\/\averick
      The =~ returning the matches in an array context is probably also one of my favorites. Note that you can also combine it with the g modifier, like this:
      @array = ($string =~ /(\d+)/g);
      to get every number on the string. In fact, if you have a simple regex like this you don't need the grouping parenthesis, you can simply use:
      @array = ($string =~ /\d+/g);
      One of my favorite examples of this is the following code to keep the $VERSION variable in sync with the RCS/CVS revision number (taken from the perlmod man page):
      $VERSION = do { my @r = (q$Revision$ =~ /\d+/g); sprintf "%d."."%02d" x $#r, @r };
      It took me a while to fully understand this code the first time I saw it :-)

      --ZZamboni

      <Grin> Yeah, many of the key perl functions return different data depending on context. Localtime(), for example, returns an array of time stats unless used in scalar context, in which case it returns a nicely formated time stamp for human consumption.
      I also like the convenient perlisms like:
      print LOGFILE "\$input = $input" if DEBUGING; # Instead of: if( DEBUGING ) { print LOGFILE '$input = ', $input }
RE: it is amazing sometimes how little you know...
by barndoor (Pilgrim) on Jul 05, 2000 at 20:34 UTC
    I've only been using perl for a few months and am still in the 'refer to O'Reilly every 1/2 hour' phase. Already when I look at my early scripts, I can see how much I've learn't in that time. Even so, every day I learn another 20 things that make me go Wow! Like yourself I usually come across these snippets of wisdom by accident when looking for something else or trying to solve an unrelated problem. However, each day my scripts get cleaner, neater and a little closer to what can be regarded virtuous perl.
      I know that feeling. And beleive me it gets worse as you grow older in/with perl. The other day I caught myself blushing while reading my own code! I had to alter stuff that I had done a couple of years ago, and downloaded a script that had been working for that period of time.

      This particular system was using flatfiles to generate a product list, and keep track of orders posted via web. Something in the lines of a pre-historic shopping cart, if you will. Well, to get the bottom of it, I found things in the script like: (please note the lack of my and local usage)
      @tmp = <FILE>; foreach $line (@tmp) { chop $line; ($foo,$bar) = split(/\t/,$line); $HASH{$foo} = $bar; } foreach $key (keys %HASH) { print "<some html>$key - $HASH{$key}</some html>\n"; }
      ... and other pearls of the sort! (pun intended) I'm sure at the time I thought I had a good reason to hash and then loop the hash, but now, looking at the file, I fail to find it. If I had to re-write the entire script nowadays, I'd do it in a very different fashion. Maybe something in the lines of:
      while (<FILE>) { m/(^.*)\t(.*$)/; print "<some html>$1 - $2</some html>\n"; }
      Come to think of it, even the HTML is so bad that I would do it a different way! I dunno, at this point I'm just rambling. I don't think that this feeling is ever going to get any better. Maybe in two years I'll look back at the stuff I'm doing today and go EEEEK! And maybe four years from now I'll look back on the stuff that I'll do in two years and go EEEEK! as well? Who knows...

      #!/home/bbq/bin/perl
      # Trust no1!

      Does anyone ever get past the "refer to O'Reilly every 1/2 hour" stage? Of course, as you progress it will be _different_ O'Reilly books that you'll be refering to.

      --
      <http://www.dave.org.uk>

      European Perl Conference - Sept 22/24 2000, ICA, London
      <http://www.yapc.org/Europe/>
RE: it is amazing sometimes how little you know...
by Buckaroo Buddha (Scribe) on Jul 05, 2000 at 17:46 UTC

    another neat thing is

    $ARRAY[-1]
    is the same as

    @ARRAY[(scalar @ARRAY) -1]
    (actual code from one of my early scripts ;)

RE: it is amazing sometimes how little you know...
by redmist (Deacon) on Jul 07, 2000 at 22:20 UTC
    You are absoluteley right Buckaroo. The other day I did something as simple as attempt to beat the government conspiracy to find the highest prime. All I wanted to do is write a script to find the highest prime and print it. It's SOOO easy to do in my head, but as a Perl script, it threw me for a loop (hehe).

    This experienced just re-enforced the importance of thinking about the algorithm for me. I remember one Meditation that said, "Think about the algorithm, not the language." Although one also has to think about the language, I think that thinking about algorithms is very important (duh)...more important than most people think.

    redmist

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others drinking their drinks and smoking their pipes about the Monastery: (2)
As of 2019-08-26 06:50 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found

    Notices?