Beefy Boxes and Bandwidth Generously Provided by pair Networks
Welcome to the Monastery
 
PerlMonks  

for loop ?

by stevensw (Acolyte)
on Aug 10, 2011 at 19:27 UTC ( #919722=perlquestion: print w/ replies, xml ) Need Help??
stevensw has asked for the wisdom of the Perl Monks concerning the following question:

Hello, I am absolutely perplexed by the following subroutine:

sub getPlatform { my $os = `uname`; chomp($os); for ($os) { /HP-UX/ and do { return('HP'); }; /Linux/ and do { my $hardware = `uname -m`; chomp($hardware); for ($hardware) { /ppc/ and return('PPC'); /ppc64/ and return('PPC'); /i686/ and return('INTEL'); /x86_64/ and return('INTEL'); errorMsg("Could not determine platform type for '$hard +ware'"); } }; } }

I think I know what the overall function of the subroutine is, it returns the platform. But we programmers are taught that 'for' is universally a looping construct, whereas here its intended use appears to resemble that of a switch statement, but I am not sure. Can a monk enlighten this newbie? Thanks

Comment on for loop ?
Download Code
Re: for loop ?
by philipbailey (Chaplain) on Aug 10, 2011 at 19:43 UTC

    Of course, for is indeed a looping construct, but in this case the for loops each have only one iteration. The programmer is using the "for" to set the default variable $_ to the value of $os or $hardware in each case, then performing a regular expression match (/.../), implicitly on $_, against various values. Older Perls (5.8 and prior) do not have a switch statement (well, given/when statement), so various other approaches have been used in the past to emulate one. I would probably not use this particular approach myself, but it seems to work well enough.

Re: for loop ?
by cdarke (Prior) on Aug 10, 2011 at 19:46 UTC
    This use of 'for' is unusual, but early perl's did not really have a switch/case statement. Nowadays we would use given and when.

    The 'for' loop loops over each item in its list (the variables inside parentheses). In each for loop there is only one item in the list, so they would only go through one iteration.
      We did, however, have if ... elsif ... else in the "bad old days", which would be a better approach than abusing a looping construct. Using for like this just makes the code harder to read than it should be.
        So far as I know we still have if ... elsif ... else. Personally I would not even think of using a for loop in this way, which means I learnt something. I'm all for trying different code tricks, some are dire and should be destroyed at birth, but some bring enlightenment.
        I'm not sure that the code is any harder to read than some of the attempts at switch/case using LABELS (as in the Perl Cookbook). I agree with your sentiment though, and grateful that we have been blessed with given/when.
Re: for loop ?
by ikegami (Pope) on Aug 10, 2011 at 19:47 UTC

    for is being used as a topicaliser, aliasing $_ to $os.

    It's still technically a (foreach) loop, but the body will unconditionally be executed once.

Re: for loop ?
by DrHyde (Prior) on Aug 11, 2011 at 10:18 UTC

    Ignoring the confusing for, I'd also point out that spawning uname is completely unnecessary. Everything that code does can be done by useing Config.pm and interrogating %Config.

    Maybe I should add architecture stuff to Devel::CheckOS.

      That's not entirely true. You can compile Perl for Linux/ia32 and run it on Linux/amd64. %Config won't tell you that. You can compile Perl for HP/PA-RISC and run it on HP/ia64 (though that's not what the OP is doing). %Config won't tell you that, either.

      Yes, been there, done that. Though I'd still prefer POSIX::uname over `uname`.

Re: for loop ?
by kcott (Abbot) on Aug 11, 2011 at 10:46 UTC

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://919722]
Approved by Perlbotics
Front-paged by toolic
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others chanting in the Monastery: (8)
As of 2014-12-23 01:43 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    Is guessing a good strategy for surviving in the IT business?





    Results (133 votes), past polls