Beefy Boxes and Bandwidth Generously Provided by pair Networks
Your skill will accomplish
what the force of many cannot
 
PerlMonks  

Re: Scope of $/

by NetWallah (Canon)
on May 26, 2016 at 01:12 UTC ( [id://1164153]=note: print w/replies, xml ) Need Help??


in reply to Scope of $/

Limit the scope of the changed value of $/, by "local"izing it, and enclosing it in the smallest block possible:
{ # Add an enclosing block local $/= "\n\n\n"; while(<>){ local $/="\n"; # Reset to normal behaviour inside loop only my ($filename)=/^(\S+)/; my $text=reader::firstline($filename); } } # End enclosing block.
This code avoids "Action at a distance", by making wierd settings in as limited a scope as possible.

        This is not an optical illusion, it just looks like one.

Replies are listed 'Best First'.
Re^2: Scope of $/
by BillKSmith (Monsignor) on May 26, 2016 at 13:29 UTC
    A truly minimal scope solves the problem.
    use strict; use reader; #$/= "\n\n\n"; while(do{local $/="\n\n\n"; $_=<>}){ my ($filename)=/^(\S+)/; my $text=reader::firstline($filename); }

    Update: Commented out original definition of $/.

    Bill
      Applause, for a truly elegant solution !

      I wish I had thought of that.

              This is not an optical illusion, it just looks like one.

Re^2: Scope of $/
by Anonymous Monk on May 26, 2016 at 01:23 UTC

    The enclosing block is just noise :) unless you give it a name

      That is not true. No name is required. The {} set the scope of the local. For my demo below, I used an "our" variable, package scope. A Perl Global like $/ acts similar. You cannot localize a "my" variable.

      I guess you can think of a local var as sort of like creating a stack of that variable. The local statement is like a "push" and when you exit the scope of the local, that is like a "pop". Rough analogy, but that is descriptive of the behavior.

      #!usr/bin/perl use warnings; use strict; our $x = 1; #a package variable { print "$x\n"; #prints 1 local $x = 99; print "$x\n"; #prints 99 } print "$x\n"; #prints 1, back to before the "local"

        :) in the OPs program, having  { local $/ ... } on the outside doesn't change the behavior of the program,

        therefore no benefit is realized ... so the extra {local} is just extra typing (noise),

        unless you add a name, like  sub StdinTriElReader{ local $/ = "\n\n\n"; ... } then its serves a purpose

        :D:D:)

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://1164153]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others romping around the Monastery: (5)
As of 2024-04-24 19:52 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found