Mod perl with "local $/" 
Author Message
 Mod perl with "local $/"

Hi.  I have a program which needs to slurp data from a filehandle and
encode the data to base 64.  I use the following code to ensure that
windows newline characters dont mess this up.

binmode F; local $/ = undef;
$encoded = encode_base64(<F>);
close F;

The question I have, is does this local version of $/ only affect this
scope ?
I am aware that any functions I call for example within this function,
will inherit the undefined version of $/, but will this not affect the
value of $/ for other instances of the program running ?

If multiple users hit the server at the same time, and someone happens
to be executing this code, am I correct in assuming that the version
of $/ used by other users sessions will be the correct global version
and not the local one defined in the above code ?

Any input welcomed
Richard Curtis



Sat, 15 May 2004 10:24:00 GMT  
 Mod perl with "local $/"


Quote:
> Hi.  I have a program which needs to slurp data from a filehandle and
> encode the data to base 64.  I use the following code to ensure that
> windows newline characters dont mess this up.

> binmode F; local $/ = undef;
> $encoded = encode_base64(<F>);
> close F;

> The question I have, is does this local version of $/ only affect this
> scope ?
> I am aware that any functions I call for example within this function,
> will inherit the undefined version of $/, but will this not affect the
> value of $/ for other instances of the program running ?

> If multiple users hit the server at the same time, and someone happens
> to be executing this code, am I correct in assuming that the version
> of $/ used by other users sessions will be the correct global version
> and not the local one defined in the above code ?

It's easy to test this. For example:

------------------------------
#!/usr/bin/perl -w
use strict;

aaa();
print "[$/]";

sub aaa{
  local $/ = 'bla';

  print "In aaa \$/ = [$/]\n";
  bbb();

Quote:
}

sub bbb{
  print "In bbb \$/ = [$/]\n";
Quote:
}

------------------------------

As you see the global value of $/ is untouched by the subs,
which is actually the point of using local in the first place.

Cheers,
Bernard



Sat, 15 May 2004 10:38:13 GMT  
 Mod perl with "local $/"

Quote:

>binmode F; local $/ = undef;
>$encoded = encode_base64(<F>);
>close F;

>The question I have, is does this local version of $/ only affect this
>scope ?

You're talking at the root level? It should be. When in doubt, wrap it
in a bare block.

        {
            binmode F;
            local $/;   # no need to assign undef
            $encoded = encode_base64(<F>);
            close F;
        }

--
        Bart.



Sat, 15 May 2004 15:36:33 GMT  
 
 [ 3 post ] 

 Relevant Pages 

1. Using "local" filehandles in Perl 5

2. "local" install of perl

3. cgi- perl local..what in action="???"

4. local() results in "Out of Memory!"

5. Strange behavior with "local"

6. local($_) - why not "my"?

7. BEGIN { local $/="\n" } clobbers global $/

8. "perldoc -f local" fails

9. select and "local" filehandle

10. "character class ""bug""

11. system(("cp", "-Rf", "/tmp/a/*", "/tmp/b")); doesnt wrk

12. how to parse a "this", "that", "and the ", "other" file

 

 
Powered by phpBB® Forum Software