Variable scope

PERL

    Next

  • 1. RE : Forcing array context
    Try this: echo hello: world | perl -lne '$aref = [ split(/:/, $_) ]; print $aref' [] synthax gives a ref to the array returned by split() HTH, Jos -----Message d'origine----- De : Binish A R [mailto: XXXX@XXXXX.COM ] Envoy: samedi 20 ao 2005 17:15 : Perl Beginners Objet : Forcing array context How can I force array context ... like # echo hello: world | perl -lne '$aref = split(/:/, $_); print $aref' but thatz giving the length of the array ... I want $aref to be a reference ... How is that possible ?? A workaround is to use the following # echo hello: world | perl -lne '@ar = split(/:/, $_); $aref = \@ar;' But I'm not satisfied with that as I've to use an extra variable @ar ... :( Any help ... --
  • 2. perl 5.6.1debug version 1.07
    All, I am having some weird system call return codes and I am hoping for some help. when I run in debug mode a system command I get this error: here is my debugger code perl -dT foo use strict; use warnings; $ENV{"PATH"} = qq (/blah blah/); system ("import -v H02046 900") or die $!; END DEBUG MODE syntax error at (eval 22) [/usr/perl5/5.6.1/lib/perl5db.pl:1521 ] line 2 , at EOF Any ideas? The command works outside of debug mode, but not in debug mode. In production I am running the snippet of code in a CGI program, and the error I get from CGI::Carp is "Interrupted system call" and Can't locate object method "header" via package "Interrupted system call" (perhaps you forgot to load "Interrupted system call"?) at /var/apache/cgi-bin/ASM_monitor.pl line 30. at line 30 is sub error { my ($q, $error_messg) = @_; print $q->header( -type=> "text/html" ), ## Line 30 here $q->start_html("ASM CGI Page Error"), $q->h1("ASM CGI Page Error"), $q->p("The following Error has occurred when retrieving ASM data via CGI to your browser:"), $q->p($q->i($error_messg) ), $q->end_html; exit; } Derek B. Smith OhioHealth IT UNIX / TSM / EDM Teams

Variable scope

Postby dermot » Wed, 03 Oct 2007 23:10:34 GMT

Hi,

I thought that the code snip below should work but I am not getting 
my text into the the file. The file is created but it's empty.

Ideally I want to create the log file outside any sub routines so 
that $log to be available to any sub I have created and hence not 
scoped to any one sub routine.  

Is my understanding not correct here? Should I be doing this in 
another way?

Thanx,
Dp.



#!/bin/perl

package Images;

 use strict;
 use warnings;
 use vars qw($log);

 $| = 1;
 our $log;
 my $logpath = 'mylog.txt';
 open($log,">>$logpath") or die "Can't append to $logpath: $!\n";


sub mysub {

my $var = 'Hello';
print $log "Starting mysub with val\n";

}

Re: Variable scope

Postby rob.dixon » Wed, 03 Oct 2007 23:23:12 GMT



Hi Dermot. You're a little confused, but you're not far out!


No need to autoflush.


There is no need to declare the $log variable with 'our' here (or to
'use vars' above): a 'my' variable will do the job fine.


open my $log, '>>', $logpath or die "Can't append to $logpath: $!";

mysub();


This variable is never used after it is declared.


This looks fine, except that you haven't called the subroutine you've
declared!

This is what it looks like after those changes:

use strict;
use warnings;

my $logpath = 'mylog.txt';

open my $log, '>>', $logpath or die "Can't append to $logpath: $!";

mysub();

sub mysub {
  print $log "Starting mysub with val\n";
}



HTH,

Rob



Re: Variable scope

Postby tom » Wed, 03 Oct 2007 23:24:24 GMT




In other words, $log needs to be a global variable, so that it's
available everywhere in your program.


So, you're declaring the global variable $Images::log.


Are you meaning to affect STDOUT with this statement? It doesn't
affect your log.


And here, you seem to be declaring $Images::log again. You shouldn't
need 'use vars' if you use 'our', mostly.


Did you ever use $var? I don't think it's needed. Did you ever call
that subroutine? If you do, I think you'll find data in the logfile.

Hope this helps!

--Tom Phoenix

Re: Variable scope

Postby dermot » Wed, 03 Oct 2007 23:38:58 GMT




Your being kind. 


I wasn't aware this only applied to STDOUT. My log messages all 
appear at once, when the script has exited and I was hoping for some 
'realtime' messages.


Noted. I can have a package variable.



The killer.

 

Sure did. Sorry for not spending a bit longer experimenting before I 
sent the mail.
Dp.


RE: Variable scope

Postby rvm » Wed, 03 Oct 2007 23:48:19 GMT

> -----Original Message-----


It used to be possible to turn off buffering on specific streams using
SELECT FH prior to this statement. I have used it on both serial and
network connections. Is this still the case?

i.e.
   select(S); $| = 1;		# don't cache outputs

Where S is an open TCP/IP socket.

Bob McConnell

Re: Variable scope

Postby chas.owens » Thu, 04 Oct 2007 00:33:51 GMT


snip
snip

Yes, this is still the case.  $| turns off buffering* for the
currently selected filehandle.

* or more accurately automatically flushes

Re: Variable scope

Postby chas.owens » Thu, 04 Oct 2007 00:39:20 GMT


snip
snip

It applies to stdout because it is the currently selected filehandle.
If you wish for $log to have autoflushing behavior you can either
select it

my $old = select $log;
$| = 0;
select $old;

or you can use IO::Handle and then use the autoflush method:

$log->autoflush;

variable scope

Postby ericcire » Sat, 06 Feb 2010 23:27:36 GMT

I have a program that requests many config values from an ini file
using Config::IniFiles. I import all of these values into my main::
script but it's pretty ugly. Is there a way to import my value
definitions int variables in a sub or another module and have them
visible in the main:: scope as local variables.

Thanks,
Eric


Re: variable scope

Postby ericcire » Mon, 08 Feb 2010 14:36:25 GMT


> 2010-02-05 06:27 -0800ricgt;
>>
> > I have a program that requests many config values from an ini file>
> > using Config::IniFiles. I import all of these values into my main::>
> > script but it's pretty ugly. Is there a way to import my value>
> > definitions int variables in a sub or another module and have them>
> > visible in the main:: scope as local variables.>
>>
> I once wrote a simple document for it, see: http://www.**--****.com/ ;
>>
> HTH.>
>>
> -->
> Jeff Peng>
> Email:  XXXX@XXXXX.COM >
> Skype: compuperson

Hi Jeff,

Thank you very much for the pointing me to the doc you wrote. All very
good solutions and possibilities. It's exactly what I was looking
for.

Thanks again,
Eric


Re: variable scope

Postby rvtol+usenet » Mon, 08 Feb 2010 21:41:37 GMT




Use a hash.

-- 
Ruud

Similar Threads:

1.Variable scope in wanted function

Greetings All -

I am having some difficulty with a module that is using File::Find.  The
method is below.

The idea is to enter this method feeding it a file name and beginning
directory and then looking for all occasions of $file_name and push those
addresses into @a_files.  This works fine until I need to use FindPath again
during the same session.  What I'm finding is that while @a_files looses
scope within FindPath itself, it does not in ProcessFile.  In other words,
when I exit FindPath and come back into it later, @a_files is an uninitiated
array.  However when ProcessFile is called, @a_files has retained the values
it had from the last call to FindPath.

Am I making sense?


sub FindPath
    {
    #- Var Declaration And Initialization
    my ($hr_self, $file_name, $file_path) = @_;
    # Array to fill with file paths
    my @a_files = ();

    # Search file_path for the file
    find(\&ProcessFile, $file_path);

    #- The Subroutine To Process Files And Directories
    sub ProcessFile
        {if ($_ eq $file_name){push (@a_files, $File::Find::name);}}

    # Return the paths found
    return @a_files;
    }   # end FindPath

Peace -
Ron Goral


2.comparing and contrasing two approaches to variable scope

Assuming you had a script configuration variable that was used numerous 
places in your script, further assume that you have "use strict;" in 
your perl script.

You could declare the variable "my" and pass that variable to any 
subroutines that needed it with @_.

Or you could declare that variable with "local" or some other "scope 
defining declaration" so that it would be available automatically in any 
and all subroutines called from the level you declared the var as "local"

My question concerns a little of form and a little of function

On the function front:

which approach is faster?  With one you need a

my ($arg1, $arg2...) = @_;

inside each subroutine which should take some time to execute.

(I apologize if the syntax isn't correct for the above line, I don't 
have my notes available and I'm questioning the use of the parenthesis, 
but you should be able to get the idea, which is all I'm after)

On the form front, Ive noticed that the scripts are not as readable for 
me, and by extension probably any other people who use my script.

What are your thoughts on choosing which approach to take?  I'm sure 
both are technically correct, but I'm sure there are places where one 
approach should be preferred over the other.

-- 
Rance Hall
System Administrator
Nebraska Turkey Growers
1-308-468-5711, ext. 106
 XXXX@XXXXX.COM 


3.problem with variable scope


4.Help with Variable Scope, Regular Expressions

Hello All.

Thank you for reading my email :)
I am trying to get this code to write the variables in a specific order,
even if they are undefined or blank.  I would like each row seperated by
newline.  I am running into a problem with the scope of the variable being
limited to the block of code it is in.

Can anyone help.

Here is the code
[code]
#!/perl -w
use strict;

my $directory = "C:/documents and settings/administrator/my documents/";
my $file = "rentals April 6 2004 mesquite";
my $ext = ".txt";
my $outfile = "$file.parse";

open(FILE, "<$directory$file$ext") or die "Can't open $file \n\n$!";
#open(OUT, ">$directory$outfile") or die "Can't open $outfile \n\n$!";

while(<FILE>){

  if(m/(\$+\d+)/g) {
 my $price = $1;
 $price =~ s/^\s+//; #remove leading spaces
 $price =~ s/\s+$//; #remove trailing spaces
 $price =~ s/(\d)\W(\d\d\d)/$1$2/;
 print   "\"\nprice: $price\",";

  }

  if(m/BD:*\s+(\d+)/g) {
 my $bd = $1;
 $bd =~ s/^\s+//; #remove leading spaces
 $bd =~ s/\s+$//; #remove trailing spaces
 print   "\"beds: $bd\",";
  }


  if(m/BA:*\s+(\d+)/g) {
 my $ba = $1;
 $ba =~ s/^\s+//; #remove leading spaces
 $ba =~ s/\s+$//; #remove trailing spaces
 print   "\"baths: $ba\",";
  }


  if(m/LA:*\s+(\d+)/g) {
 my $la = $1;
 $la =~ s/^\s+//; #remove leading spaces
 $la =~ s/\s+$//; #remove trailing spaces
 print   "\"la: $la\",";
  }

  if(m/(\d\d\d-\d\d\d-\d\d\d\d)/g){
   my $phone = $1;
   print   "\"phone: $phone\",";

  }


  if(m/(\d\w+\W\d\w+\W\d\s+)/g){
    my $arrangement = $1;
    print   "\"arrangement: $arrangement\",";
#    print  ("\n");
  }



if(m/(\w+[\W\s]?\w+[\W\s]?)\s+\W\s+(\w+\s\w+\s\w+\s\w+)\s+\W\s+(\d+\W\d+\W\d
+)/g){
    my $location = $1;
    my $paper = $2;
    my $date = $3;
    print   "\"location: $location\",";
    print   "\"paper: $paper\",";
    print   "\"date: $date\",";
  }


  if(m/(\d\d\d+\s\w+[\W\s]?\w+)/g){
  my $address = $1;

print  "\"address: $address\",";
#    print   "\n";


  }


}#end while

[/code]


Here is the result

price: $725","beds: 3","baths: 1","
price: $725","phone: 972-289-2098","location: Mesquite ","paper: The Dallas
Morning News","date: 04/08/2004","
price: $950","beds: 3","baths: 1","
price: $950","phone: 469-525-0204","address: 1620 Summitt","location:
Mesquite ","paper: The Dallas Morning News","date: 04/08/2004","
price: $1100","beds: 4","baths: 2","la: 2","
price: $1100","phone: 214-566-6106","location: Mesquite ","paper: The Dallas
Morning News","date: 04/08/2004","
price: $995","beds: 3","baths: 2","address: 995 mo","
price: $995","arrangement: 11/2ba/1 ","address: 995 mo","location: Mesquite
","paper: The Dallas Morning News","date: 04/08/2004","
price: $850","beds: 3","baths: 1","
price: $850","phone: 972-216-0999","address: 3621 Edgebrook-3bd","location:
Mesquite ","paper: The Dallas Morning News","date: 04/08/2004","
price: $995","beds: 4","baths: 1","
price: $995","phone: 972-303-5134","address: 3528 Statler","location:
Mesquite ","paper: The Dallas Morning News","date: 04/08/2004","beds:
4","baths: 2","phone: 214-356-2120","location: Mesquite ","paper: The Dallas
Morning News","date: 04/08/2004","beds: 3","baths: 1","phone:
214-417-6453","address: 2428 Vickie 214","location: Mesquite ","paper: The
Dallas Morning News","date: 04/08/2004","
price: $1050","beds: 3","baths: 2","arrangement: 3bd/2ba/2 ","
price: $1050","phone: 972-754-0214","location: Mesquite ","paper: The Dallas
Morning News","date: 04/05/2004","
price: $1250","beds: 3","baths: 2","
price: $1250","address: 1617 Colborne","phone: 214-236-8717","location:
Mesquite ","paper: The Dallas Morning News","date: 04/05/2004","beds:
3","baths: 2","phone: 214-478-9139","location: Mesquite ","paper: The Dallas
Morning News","date: 04/05/2004","
price: $895","beds: 3","baths: 2","
price: $895","phone: 972-240-7503","address: 7503 972-226","location:
Mesquite ","paper: The Dallas Morning News","date: 04/04/2004","
price: $750","beds: 3","baths: 1","arrangement: 3bd/1ba/1 ","
price: $750","location: Mesquite ","paper: The Dallas Morning News","date:
04/04/2004","
price: $795","beds: 3","baths: 2","
price: $795","phone: 972-216-4988","location: Mesquite ","paper: The Dallas
Morning News","date: 04/04/2004","
price: $1025","beds: 3","baths: 2","
price: $1025","address: 224 Overland Trail","location: Mesquite ","paper:
The Dallas Morning News","date: 04/04/2004","
price: $995","beds: 3","baths: 2","
price: $995","phone: 972-681-4502","address: 800 dep","location: Mesquite
","paper: The Dallas Morning News","date: 04/04/2004","
price: $975","beds: 3","baths: 2","la: 2","
price: $975","phone: 214-343-4618","address: 1509 Liberty Ln","location:
Mesquite ","paper: The Dallas Morning News","date: 04/04/2004","
price: $925","beds: 3","baths: 2","
price: $925","phone: 972-288-1495","address: 2309 Ithaca","location:
Mesquite ","paper: The Dallas Morning News","date: 04/04/2004","
price: $1150","beds: 3","baths: 2","
price: $1150","phone: 214-827-7777","address: 4109 Aralia","location:
Mesquite ","paper: The Dallas Morning News","date: 04/04/2004","
price: $1150","beds: 3","baths: 1","
price: $1150","phone: 214-755-3922","address: 800 dep sect","location:
Mesquite ","paper: The Dallas Morning News","date: 04/04/2004","
price: $1025","beds: 3","baths: 2","
price: $1025","address: 224 Overland Trail","location: Mesquite ","paper:
The Dallas Morning News","date: 04/02/2004",



Here is the data file I am working on



Click here to go back to Residential Real Estate for Rent.

Your search for Mesquite returned the following ads:

*If your search did not return as many ads as you'd like, simply go back and
search
again using fewer search criteria.



----------------------------------------------------------------------------
----
 Ads basket. Check to add. Uncheck to remove.

Price: $725     BD: 3     BA: 1     LA:
Home 3bd-1ba-1,ch/a new carpet & paint fence stove $725+dep. 972-289-2098

Mesquite  -  The Dallas Morning News  -  04/08/2004


----------------------------------------------------------------------------
----
 Ads basket. Check to add. Uncheck to remove.

Price: $950     BD: 3     BA: 1.5     LA:
Home 3bd/1.5ba/2 1620 Summitt $950+dep Sec 8ok 469-525-0204; 214-957-8176

Mesquite  -  The Dallas Morning News  -  04/08/2004


----------------------------------------------------------------------------
----
 Ads basket. Check to add. Uncheck to remove.

Price: $1100/mo     BD: 4     BA: 2     LA: 2
Home 4bd/2ba/2/2la close to schl/mall avail now $1100/mo+$1000/dep Sec. 8 ok
welcome 214-566-6106

Mesquite  -  The Dallas Morning News  -  04/08/2004


----------------------------------------------------------------------------
----
 Ads basket. Check to add. Uncheck to remove.

Price: $995 mo     BD: 3     BA: 2     LA:
Home-$995 mo/$600 dep. Beautiful 3br/11/2ba/1 gar near schl (214) 755-2861

Mesquite  -  The Dallas Morning News  -  04/08/2004


----------------------------------------------------------------------------
----
 Ads basket. Check to add. Uncheck to remove.

Price: $850mo     BD: 3     BA: 1.5     LA:
Home-3621 Edgebrook-3bd-1.5ba-1gar-$850mo-$850dp-no pets-tel.972-216-0999

Mesquite  -  The Dallas Morning News  -  04/08/2004


----------------------------------------------------------------------------
----
 Ads basket. Check to add. Uncheck to remove.

Price: $995     BD: 4     BA: 1.5     LA:
Home-4bd/1.5ba/den: 3528 Statler, $995+$1000dep I-30/Eastfield area.
972-303-5134

Mesquite  -  The Dallas Morning News  -  04/08/2004


----------------------------------------------------------------------------
----
 Ads basket. Check to add. Uncheck to remove.

Price:      BD: 4     BA: 2.5     LA:
Home-4bd/2.5ba/2cp, remodeled, strg bldg, rent to own. 2100sf. 214-356-2120

Mesquite  -  The Dallas Morning News  -  04/08/2004


----------------------------------------------------------------------------
----
 Ads basket. Check to add. Uncheck to remove.

Price:      BD: 3     BA: 1     LA:
Home-Sec 8. 3bd/1ba remodeled. Open Sat & Sun, 2-4. 2428 Vickie 214-417-6453

Mesquite  -  The Dallas Morning News  -  04/08/2004


----------------------------------------------------------------------------
----
 Ads basket. Check to add. Uncheck to remove.

Price: $1050/mo     BD: 3     BA: 2     LA:
Home 3bd/2ba/2 brick
ch/a fp $1050/mo $900/dep
972-754-0214

Mesquite  -  The Dallas Morning News  -  04/05/2004


----------------------------------------------------------------------------
----
 Ads basket. Check to add. Uncheck to remove.

Price: $1250/mo     BD: 3     BA: 2     LA:
Home Rent To Own
3bd/2ba/2gar 1617 Colborne $1250/mo. Bad Credit OK
214-236-8717

HappyHouseSellers.com

Mesquite  -  The Dallas Morning News  -  04/05/2004


----------------------------------------------------------------------------
----
 Ads basket. Check to add. Uncheck to remove.

Price:      BD: 3     BA: 2     LA:
Home-gorgeous 4-3bd/2ba/gar, all new, must see. Sec 8 ok. Avail 214-478-9139

Mesquite  -  The Dallas Morning News  -  04/05/2004


----------------------------------------------------------------------------
----
 Ads basket. Check to add. Uncheck to remove.

Price: $895/mo     BD: 3     BA: 2     LA:
Dup. 3bd/2ba, $895/mo. Owner/Agent. Section 8! 972-240-7503 972-226-4446

Mesquite  -  The Dallas Morning News  -  04/04/2004


----------------------------------------------------------------------------
----
 Ads basket. Check to add. Uncheck to remove.

Price: $750/mo     BD: 3     BA: 1     LA:
Home 3bd/1ba/1 ch/a
$750/mo + $600/dep
(972) 285-2090

Mesquite  -  The Dallas Morning News  -  04/04/2004


----------------------------------------------------------------------------
----
 Ads basket. Check to add. Uncheck to remove.

Price: $795/mo     BD: 3     BA: 2     LA:
Home 3bd/2ba/den, brick, CH/A, new crpt, appls, $795/mo+dep. 972-216-4988

Mesquite  -  The Dallas Morning News  -  04/04/2004


----------------------------------------------------------------------------
----
 Ads basket. Check to add. Uncheck to remove.

Price: $1025/mo     BD: 3     BA: 2     LA:
Home-224 Overland Trail 3bd/2ba/2gar $1025/mo, Sec 8 ok. (972) 554-8833

Mesquite  -  The Dallas Morning News  -  04/04/2004


----------------------------------------------------------------------------
----
 Ads basket. Check to add. Uncheck to remove.

Price: $995/mo     BD: 3     BA: 2     LA:
Home-3bd/2ba/2, fp, $995/mo, $800 dep, 2426 Beverly Hills Lane, exc cond,
credit check. 972-681-4502

Mesquite  -  The Dallas Morning News  -  04/04/2004


----------------------------------------------------------------------------
----
 Ads basket. Check to add. Uncheck to remove.

Price: $975.     BD: 3     BA: 2     LA: 2
Home-3bd/2ba/2cp/2la, sec & sprnklr, no pets, no Sec 8. 1509 Liberty Ln. Hwy
80/Beltline. $975. 214-343-4618

Mesquite  -  The Dallas Morning News  -  04/04/2004


----------------------------------------------------------------------------
----
 Ads basket. Check to add. Uncheck to remove.

Price: $925/mo     BD: 3     BA: 2     LA:
Home-3bd/2ba/2ga, $925/mo + dep, very clean, 2309 Ithaca, 972-288-1495

Mesquite  -  The Dallas Morning News  -  04/04/2004


----------------------------------------------------------------------------
----
 Ads basket. Check to add. Uncheck to remove.

Price: $1150/mo     BD: 3     BA: 2     LA:
Home-Poteet Schools! 3bd/2ba/2ga, 1900sf, WBFP, very nice, 4109 Aralia, no
pets. $1150/mo. 214-827-7777

Mesquite  -  The Dallas Morning News  -  04/04/2004


----------------------------------------------------------------------------
----
 Ads basket. Check to add. Uncheck to remove.

Price: $1150/mo     BD: 3     BA: 1.5     LA:
Home: 3bd/1.5ba near school $1150/mo + $800 dep sect. 8 ok 214-755-3922.

Mesquite  -  The Dallas Morning News  -  04/04/2004


----------------------------------------------------------------------------
----
 Ads basket. Check to add. Uncheck to remove.

Price: $1025/mo     BD: 3     BA: 2     LA:
Home-224 Overland Trail 3bd/2ba/2gar $1025/mo, Sec 8 ok. (972) 554-8833

Mesquite  -  The Dallas Morning News  -  04/02/2004







5.extra constructor calls, inefficent loop variable scope?

Hi All,

Does the declaration of a variable impact performance?  For exmaple:

Loop A:

  while (<>) {
     my $something = $_;
  }

Loop B:

  my $something;
  while (<>) {
     $something = $_;
  }

Is either one better w.r.t. performance?

Thanks,
itd

6. Complex data structures and variable scope

7. Question about Modules and Variable Scope

8. Question about variable scope



Return to PERL

 

Who is online

Users browsing this forum: No registered users and 41 guest