Need a Perl Guru.

CptKrf

Member
Joined
Dec 6, 2022
Messages
45
Reaction score
41
Credits
624
GURU = Anyone who knows more than me. i.e. almost everyone.

I am making good progress in learning Perl, but am stuck on an item that I can't seem to find in the Mastering Perl books. (Or more likely, don't recognize.)

I am doing I/O projects on a PI 4, using the GPIO for all kinds of stuff. The access for the pins via Perl is with the Device-BCM2835 module from CPAN. It works fine.

In it are very verbose calls to do something (set a pin for input/output, read, write, etc. The following is one.

Device::BCM2835::gpio_fsel(&Device::BCM2835::RPI_V2_GPIO_P1_07, $inputcmd)

So far I have been making a separate hardcoded call for each of the pins that I want to set to input, but that is cumbersome and obviously not the proper way to do such. I want to call this routine with a list of pins, not just number 07 or 19 or whatever. In other words, something like this...

$pinno = RPI_V2_GPIO_P1_07;
Device::BCM2835::gpio_fsel(&Device::BCM2835::$pinno, $inputcmd);
Or even better...
$pinno = "07";
Device::BCM2835::gpio_fsel(&Device::BCM2835::RPI_V2_GPIO_P1_ . "$pinno", $inputcmd)

(I know the above is incorrect. I am just showing what I am trying to do.)

I have been through the topics on aliasing and pointers but I am still not seeing the forest for the trees.

Anybody?
 


I'm not good with Perl however; I'm curious and have a question after looking here:

Are you trying to invoke a hardcoded module with perl's -M option?

Is BCM2853 by chance a broadcom chip and your trying to make a call for all the pins in a list that pertains to the module?
 
Trying to help.....Is this close to what you need?

SPI access requires being root, whereas GPIO access can be done as any user, as the underlying bcm2835 library (which must be installed) can use the /dev/gpiomem interface.

 
Last edited:
I already use Device::BCM2835. It works fine and my code to access the GPIO pins has no problem with it. But, I apparently didn't make my wants clear.

It isn't that I need help in getting the driver to work, but that I don't quite understand the Perl calls that use it.

To change a pin to input from output something like the following is used...

Device::BCM2835::gpio_fsel(&Device::BCM2835::RPI_V2_GPIO_P1_07, $inputcmd)

It works fine, but is hardcoded to Pin 07. If I want to use pin 13 also, I have to include another hardcoded statement...

Device::BCM2835::gpio_fsel(&Device::BCM2835::RPI_V2_GPIO_P1_13, $inputcmd)

Obviously a clunky way to code, but I can't figure out how to include a variable in the statement so that a loop can access one code line with several pin numbers.

So, not a question on how to make it work, but how to make it work like proper Perl code.
 
I'm not familiar with Perl either, but most languages have a way of defining a function.
Can you create your own wrapped function that does what you want? One that accepts an array of values corresponding to pin numbers?
 
It's been a while since I did any serious Perl. I haven't got time to get myself fully back up to speed with Perl right now either. I'm also unfamiliar with the GPIO module. I know what GPIO is and what it does. I'm just unfamiliar with its Perl interface.

But off the top of my head - assuming you're going to use the same command for each GPIO pin ID, you could simply set up an array of pin ID's and then loop through each pin ID and call the gpio_fsel function, passing each pin reference and the command string:
Perl:
my $inputCmd = "Your  command string here" # assuming it is a string?!
my @pinIDs = ( &Device::BCM2835::RPI_V2_GPIO_P1_07, &Device::BCM2835::RPI_V2_GPIO_P1_13 ) # Add any other GPIO pin IDs/references here, comma separated!
foreach my $pinID (@pinIDs) {
    Device::BCM2835::gpio_fsel($pinID, $inputCmd)
}
Something like that.

Otherwise, if you're going to use different commands for different pins, you'd have to set up a data structure that holds a GPIO pin ID and a command and then create an array to contain multiple instances of that data structure. But I can't remember how to define structures in Perl offhand, so I'll skip that for now!
 
Problem solved. ChatGPT suddenly appeared from nowhere and gave several good answers, some including features of Perl that I didn't even know existed. My professional programming career is long behind me so the issue is only academic, but I would say that the future of coding is going to change rapidly. C++ is/was my baliwick, and so far Chatty hasn't given me anything I didn't already know about such, but for languages that I have only a moderate knowledge of, it makes the difference of a code snippit working or not.
 
Certainly, although plain C is usually the best for an ordinary driver. I have always advised students to start with C before moving on to C++. The structure is much simpler (pointers not withstanding) and for a beginner it will not get them lost in objects, inheritance, classes, polymorphism and such. Case in point. The usual C book (back in the days when we had books) was a half inch thick or less. A C++ manual is like a War and Peace tome with 2 inches of paper weight, at least.

That being said, from the view of both compiler and editor, C and C++ look exactly the same. You can write nothing but ordinary C procedures and call it myprog.c++ and neither will care. And (in my opinion) you have to learn C before C++ anyway.
 
Certainly, although plain C is usually the best for an ordinary driver. I have always advised students to start with C before moving on to C++. The structure is much simpler (pointers not withstanding) and for a beginner it will not get them lost in objects, inheritance, classes, polymorphism and such. Case in point. The usual C book (back in the days when we had books) was a half inch thick or less. A C++ manual is like a War and Peace tome with 2 inches of paper weight, at least.

That being said, from the view of both compiler and editor, C and C++ look exactly the same. You can write nothing but ordinary C procedures and call it myprog.c++ and neither will care. And (in my opinion) you have to learn C before C++ anyway.
Very good information and details. Thanks!
I hope I can find enough info. online about C and C++.
Enjoy the weekend!
 
Way back in the day, I kept a copy of the Zortech C++ compiler on hand on my MS DOS systems and used it almost exclusively for straight "C" programming.

For learning C, while you might not want it to be your only reference, you can't go wrong by having a copy of K&R "The C Programming Language, second edition" (aka "The New Testament").

Alas, I don't know of any similar book book for C++. I doubt there can be such a book as C++ doesn't lend itself to such concise description. Although I wonder if C++ might be more manageable to learn if one limited the initial study to those features that people actually use on a regular basis. Quick - someone tell me how wrong I am about this. :)
 
Stroustrup's The C++ Programming Language would be the equivalent. It's extremely well written. Like K&R, it's probably not best for the absolute beginner. It's quite a bit thicker than K&R, though.
 
The reason that I always have steered beginners to C rather than the C++ they have alway heard to be vastly superior, is to keep them from withdrawing from the class in total confusion and changing their degree plans to psychology.

In one of my many C++ books, plainly advertising itself as a "Learn C++ in 7/25/n+1 days" the following sentence appears by the SECOND page.

"Virtual members and abstract classes grant C++ polymorphic characteristics allowing for the
possibility for a member of the abstract base class to use the special pointer 'this' to access a virtual members, even though 'this' itself has no implementation for a particular function."

This is babble-speak written for no other reason than to sell a book. Totally useless for a beginner. Out of my whole library, only about two books properly start a student with "Hello World" in the proper manner and do not bring the advanced ++ features into play until far later.

Way back in my own DOS days, I also had the K&R white manual as my only reference. Not by choice, but because it was the ONLY book available. The 'net was far in the future, techbook stores did not exist outside of major universities and even those only had books on Fortran and Cobol.

The old White Book is mostly a description, not a tutorial. I can remember struggling in the pointer section for days on end - load editor and source (from slow floppy), write something, save it, exit editor, call compiler and linker, look at error report then start again. As I had started on pointer-less Fortran years before, It was a surprisingly long time before I finally got the concept of a variable that has the address of a variable.

Those were fun days.
 
The reason that I always have steered beginners to C rather than the C++ they have alway heard to be vastly superior, is to keep them from withdrawing from the class in total confusion and changing their degree plans to psychology.

In one of my many C++ books, plainly advertising itself as a "Learn C++ in 7/25/n+1 days" the following sentence appears by the SECOND page.

"Virtual members and abstract classes grant C++ polymorphic characteristics allowing for the
possibility for a member of the abstract base class to use the special pointer 'this' to access a virtual members, even though 'this' itself has no implementation for a particular function."

This is babble-speak written for no other reason than to sell a book. Totally useless for a beginner. Out of my whole library, only about two books properly start a student with "Hello World" in the proper manner and do not bring the advanced ++ features into play until far later.

Way back in my own DOS days, I also had the K&R white manual as my only reference. Not by choice, but because it was the ONLY book available. The 'net was far in the future, techbook stores did not exist outside of major universities and even those only had books on Fortran and Cobol.

The old White Book is mostly a description, not a tutorial. I can remember struggling in the pointer section for days on end - load editor and source (from slow floppy), write something, save it, exit editor, call compiler and linker, look at error report then start again. As I had started on pointer-less Fortran years before, It was a surprisingly long time before I finally got the concept of a variable that has the address of a variable.

Those were fun days.
I'm a beginner so suggestions are welcomed when you have the time.

I'm reading here in the effort to start somewhere.
 
Linus himself, seems to like C pretty good, but from all accounts he hates C++ and
doesn't want it in the Linux kernel at all.



 
I haven't followed the progress, but they're adding Rust as a 2nd language option for the Linux kernel.

 
I haven't followed the progress, but they're adding Rust as a 2nd language option for the Linux kernel.

Good article:-
 
Linus himself, seems to like C pretty good, but from all accounts he hates C++ and
doesn't want it in the Linux kernel at all.
I agree wholeheartedly with him. C is small, tight and has no fluff. C++, while it has everything that the original C has, also contains the kitchen sink, the backyard barbeque pit, and that warehouse storing new tires that don't fit anything. For everyday use on building programs, it is fine and contains lots of features that get the job done, but... I would never use C++ if I was guiding missiles, or coding for self-driving cars, or logging a heart monitor. It is too massive and cluttered to ever know that you have no bugs. In fact, in any C++ program beyond tiny "Hello World" apps there WILL be bugs. Most will never appear, but they are there.

There was a joke, and only partly funny, that in the halls of my old employer, all of the fat guys (and gals) were C++ coders who were forcibly locked in a room for weeks and fed pizzas that were slid under the door until they found that damn last bug, then THAT last one, then...
 

Members online


Top