http://www.perlmonks.org?node_id=31763

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

What can one print to in perl? can you, say,

open(PRINTER,'/dev/lp0');$stuff="stuff";print PRINTER "$stuff";

and use that to talk to devices? also, can you use print to a memory location? I'm not too knowledgable in memory, but would something like

print 0x384A "AF83";

work? the memory address was of course made up and so was the hex i sent there and i'm not sure if i need to make a file handle or something. Of course that wouldn't work cause there is no buffer, and i don't feel like getting into all of that, but if i did print to a real address like that, keeping a buffer full of course with real information, would it work? If that is not how one does it, how does one speak to devices and memory locations in perl? There must be some way, and I'd like to know it. It would bring me one step closer to writing TNT2 opengl drivers in perl :)

@:::::::((==========Rydor====>

Replies are listed 'Best First'.
Re: print to memory?
by athomason (Curate) on Sep 10, 2000 at 00:06 UTC
    ...writing TNT2 opengl drivers in perl.

    Whoa! That's a strange undertaking. I hope it's only an academic exercise. In any case, accessing raw memory is largely what Perl is designed to shield you from. So as far as core Perl is concerned, you can't do that (and shouldn't be able to). A quick search didn't turn up an existing module to do this, but you could write a small XS extension (a Perl module written in C) which had routines to read to and write from specific memory locations, and then just call those in your program. Keep in mind this would most likely be insanely slow. But perhaps you could accelerate Commander Keen :-).

    As far as the printing goes, you might not be far off. Provided your /dev/lp0 is configured to correctly handle raw text dumped to it, you should be able to use what you have if you make the handle writable (open(PRINTER,'>>/dev/lp0')) before printing to it. You may need to figure out how to send a page-feed, though, or just press the button yourself on the printer.

      Generally, \x0c (that's ctrl-L or ^L) is a page-feed to most printers.

      I've written ofs in perl for the lpd before, it's not a difficult task :) However, I'd strongly recommend using the lpd (or at least, one of the new non-spooling print programs, such as pdq) rather than trying to write raw text to the hardware device.

      Spud Zeppelin * spud@spudzeppelin.com

        In Perl (and usually "C") string handling you can use "\f" for the formfeeds. As long as you are playing with teletype commands, you should remember that one.

        • \t ab
        • \n ewline
        • \r eturn
        • \f ormfeed
        • \b ackspace
        • \a larm
        • \e scape (also handy but a perl only idiom AFAIK)

        --
        $you = new YOU;
        honk() if $you->love(perl)

      i was hoping something like the printer thing would work, and i think i couldn't spend my time more productively than accelerating commander keen. That game rocks!! seriously, it was the game that got me interested in computer games. I owe it my life (or perhaps it owes me my life back... i'll have to think about that) I wonder if there is commander keen for linux.......

      @:::::::((==========Rydor====>
Re: print to memory?
by chromatic (Archbishop) on Sep 10, 2000 at 03:29 UTC
    You might have more luck with Loki's SDL, especially as there are Perl bindings available from the CPAN.

    (Now under Unix, writing to devices is a lot easier as they appear to be files in the filesystem, but writing actual drivers for speed-intensive hardware like a 3d card in Perl strikes me as a sign of too much free time. :)

    Update: merlyn has just pointed out the shiny new Inline module, which may get you closer to writing drivers in Perl.

      This inline module looks <stong>way cool, has anybody used it?