Beefy Boxes and Bandwidth Generously Provided by pair Networks
The stupid question is the question not asked

help with perl understanding

by barryscott (Acolyte)
on Jan 08, 2008 at 09:19 UTC ( #661036=perlquestion: print w/replies, xml ) Need Help??

barryscott has asked for the wisdom of the Perl Monks concerning the following question:

Good morning Monks. I'm struggling to extract functions from c code,
int test_function (int a, char b) { /* some code here*/ }
and was very kindly helped out by the forum with the following code snippet, which (with my limited but fast growing knowledge of perl) looks like the kind of thing I'm after...
my ($prev, $type, $func, $args, $body); while (<>) { if (/^\{/) { # maybe check for structs/classes here if necessary ($type, $func, $args) = split(' ', $prev, 3); $body = ''; } elsif (/^\}/) { # ... process the function here ... $func = undef; } else { if (defined($func)) { $body .= $_; } else { $prev = $_; } } }
but I have some questions to understand it fully. Firstly - is $prev a special variable? How does perl know to use the line previous? Is something clever happening on the regex that I can't identify? Next, do I require a file to be looked at line by line, or "slurped" in? Can someone very quickly let me know, and the advantages of each? Help is *much* appreciated, I'm trying to design some tools for analysing my source code and learning perl at the same time. ---EDIT--- I've had some coffee, steamed open my eyes and sat down with a manual. I can now see how it works - sorry to bother ;oD This language really is amazing you know, so versatile, but if you don't fully understand some subtle features it can be very difficult to read. I'm an embedded c programmer by trade, so this high level work amazes me.

Replies are listed 'Best First'.
Re: help with perl understanding
by svenXY (Deacon) on Jan 08, 2008 at 09:40 UTC
    just look at the c code you provided:
    - the first line will jump to the else and to the else and therefore set $prev with the current line
    - the next line (the "{") will jump into the first if and then process $prev (which was "filled" one loop-run before)
    - and so on - you will get the point of it ;-)
    - print() statements within your code help a lot to learn what is going on...
Re: help with perl understanding
by sundialsvc4 (Abbot) on Jan 08, 2008 at 16:37 UTC

    (Whups... forgot to log-in.)

    One thing that might really throw you for a loop is, when you define a sub, “where are the parameters?!” Perl has the very peculiar strategy of placing all of the arguments to a function into an array (strangely-named “@_”) which is available to the function, and you'll see people grabbing parameters off of it with things like shift (which pops the first element from an array).

    However, I must say that I do admire a language that makes it a trivial task to reverse two variables that are out-of-order:   (This is a list assignment...)

    ($a, $b) = ($b, $a) if ($b > $a);
    ... and it is very nice to work in a system that has a very well-thought-out notion of “lists” and “hashes,” even though the language bumbles through some of these concepts with (in my opinion) rather sickening syntax. (Hold your nose when you get to “contexts.”)

    And let's face it:   Messrs. Kernighan and Plauger put an assortment of “warts” into their language too, and by now we sort-of don't see them as warts anymore because we like what the language lets us do. The same's true of Perl. (It's a “teletype-era language” too, and sometimes it shows.) You'll scratch your head a lot, stick your tongue out at it a few times, roll your eyes and mutter under your breath, and in a matter of a few days or maybe a week or so, begin to get the groove of what all the fuss is about. Quite a few more “warts” here to be overlooked, but love is blind. ;-)

      ..... you'll see people grabbing parameters off of it with things like shift (which pops the first element from an array).

      Sorry to pedantic, what with you disliking so many Perl-isms, but pop pops, and shift shifts. ^_^

      Just hoping to clear up any future mishaps!

      -=( Graq )=-

Re: help with perl understanding
by Anonymous Monk on Jan 08, 2008 at 16:28 UTC

    First of all, the Perl code-fragment you have provided is not related to “a C function.” That sort of thing is provided by a sub.

    The Perl fragment that you have provided first declares five private variables, then enters a loop that reads a line of data from the standard-input. What might be throwing you off here is that there seem to be no actual references to the file, nor to the record that has been read from it, nor to the reading! Perl is big on “shorthand,” and it's very much a purpose-built language; its purpose is text-processing.

    So the while-loop is reading from a file, and each of the if statements contain “regular expressions” (string patterns), which implicitly (because they do not use the operator =~) refer to the latest line that has been read from the input. (This line is contained in the very strangely named variable, $_. Don't ask me why Perl uses such odd names; maybe Larry Wall can't type good. I'm also not a fan of its insistence upon “funny characters” like $, @, % but once again it wasn't my idea...)

    Some of the constructions in that Perl fragment are peculiar; in fact, the entire flow-of-control seems strange to me.

    Anyhow, “Perl is what Perl is,” and even though the syntax of the language may throw you off for a few moments, your existing familiarity with embedded-C will soon come roaring back. Probably the most peculiar aspect of this language from that perspective is that, whereas “C” does almost-nothing for you, Perl does quite a lot. Garbage-collection and automatic memory management, for example; built-in hashes; a fairly-clever “reference” system for safe indirect addressing. Don't look at the Perl-5 source code unless you have a strong stomach... although you with your experience may well find it informative.

Log In?

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

How do I use this? | Other CB clients
Other Users?
Others lurking in the Monastery: (6)
As of 2021-06-12 12:25 GMT
Find Nodes?
    Voting Booth?
    What does the "s" stand for in "perls"? (Whence perls)

    Results (51 votes). Check out past polls.