Beefy Boxes and Bandwidth Generously Provided by pair Networks
Do you know where your variables are?
 
PerlMonks  

Re: slurping __DATA__

by jeffa (Bishop)
on Aug 08, 2003 at 14:17 UTC ( #282187=note: print w/replies, xml ) Need Help??


in reply to slurping __DATA__

do is "not really a function". It is a block that has a seperate scope from wherever it was called from. This allows you to localize $/ so that when execution leaves our "temporary" scope, $/ will be what it was before it was changed.

If you look up $/ in perlvar, you will see

   You may set it to a multi-character
   string to match a multi-character terminator, or
   to "undef" to read through the end of file.
Finally, <DATA> returns the next line from the filehandle DATA, and since $/ is undefined, the entire contents of the file are returned and "caught" in a scalar.

UPDATE: i should add that this is one of those few cases where local is a good choice. For example,
local $/;
Is essentially the same as
local $/ = undef;
Which is not the same as
$/ = undef;
Even though that last snippet will be contained inside a do block ... $/ will still be undef afterwards. Try this - make two files foo.txt and bar.txt and run this:
# print entire file open FH,'foo.txt'; my $foo = do {$/ = undef;<FH>}; print $foo; # print only first line open FH,'bar.txt'; my $bar = <FH>; print $bar;
Then go back and add local to the do block.

jeffa

L-LL-L--L-LL-L--L-LL-L--
-R--R-RR-R--R-RR-R--R-RR
B--B--B--B--B--B--B--B--
H---H---H---H---H---H---
(the triplet paradiddle with high-hat)

Replies are listed 'Best First'.
Re: Re: slurping __DATA__
by LanceDeeply (Chaplain) on Aug 08, 2003 at 14:44 UTC
    Thanks!

    I did read perldoc perlvar but I didnt know that local $/; was undef-ing it for the do's block context. I'll have to go brush up on local, 'cause I never use it.

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://282187]
help
Chatterbox?
[stevieb]: nice! I just finished a GPS "take me home" device last week, and did a bunch of software updates to it yesterday. I also created a pseudo chip with an Arduino to simulate an IC, where it responds to register read/writes over the I2C bus...
[stevieb]: ...from an I2C master. It's ugly and there are many changes I'm going to make, but I had not done anything like it before. It's designed for my RPi:: automated test platform; a system that does CI on *all* my RPi modules.
[shmem]: pseudo chip?
[stevieb]: well, what happens is the Arduino 'listens' for requests r/w, and does the appropriate thing when it's interrupted based on the 'register' address sent in. It's ugly as it was my first attempt, but I've got great new ideas I'm just sitting.
[stevieb]: ...down to implement now. Here's the sketch as it currently sits
[shmem]: well I use I2C and SPI and stuff, but creating a pseudo chip looks to me like lot of indirection and memory clutter... not?
[choroba]: Are you going to use the device soon? Related to your comment about "not having much time to do a lot of coding"...
[stevieb]: sure, but I'm just learning ;) I consider it practice to get a good understanding of what goes on *after* an I2C/SPI request is made
[shmem]: ah ok. Gonna read that. but now....
shmem compiles himself into his template

How do I use this? | Other CB clients
Other Users?
Others chilling in the Monastery: (9)
As of 2017-06-25 22:42 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    How many monitors do you use while coding?















    Results (572 votes). Check out past polls.