Beefy Boxes and Bandwidth Generously Provided by pair Networks
Syntactic Confectionery Delight

(ichimunki) Re: Need some help with a dodgy variable

by ichimunki (Priest)
on Jul 30, 2001 at 23:30 UTC ( #100933=note: print w/replies, xml ) Need Help??

in reply to Variable Scope

If you are using multiple files in your script and you assume that "use strict" from the main script will automatically affect all the other scripts, you will run afoul of this scoping issue. The scripts below will exhibit different behavior with respect to globals when strict and my are used than when strict and my are not used. If we really want to use $z in our main scope as a global, we should either pass a reference to it in our sub call (and modify it in place), or set its value based on the return value of our sub call. Or we could not use strict, but we should use strict and figure out a better way to handle our data than indiscriminate use of globals.
#!/usr/bin/perl -w #this is use strict; #comment out require ''; my $z = 0; #comment out, we could also leave this # undef, but that generates a bunch of warnings for my $x (1..5) { my $y = multiply( $x ); print "$x * 3 : $y : $z\n"; } _______________________________________ #this is sub multiply { $this = shift; $z = $this * 3; } 1;

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://100933]
[james28909]: ok maybe i need to ask this question in sopw and not CB but here goes
[james28909]: i am manually parsing html, and am trying to wrap my head around keeping up with tag counts
[james28909]: i can extract a certain tree manually by regexing the html file for a starting anchor, then i send the position of the match to a sub and then seek to that position in the file and keep up with div tags (for now)
[james28909]: how can i manage multiple tags? other than div?

How do I use this? | Other CB clients
Other Users?
Others about the Monastery: (6)
As of 2017-04-25 03:52 GMT
Find Nodes?
    Voting Booth?
    I'm a fool:

    Results (448 votes). Check out past polls.