comparing and contrasing two approaches to variable scope

PERL

    Next

  • 1. Can't figure out why File::Path::rmtree says "Operation not permitted"
    Hi folks, I have a script that calls rmtree. The operation works; my directory and its contents are removed. However, I get this error: Can't make directory /u01/mydocs/mytest001 read+writeable: Operation not permitted at /u02/home/myacct_rem.pl line 41 If the directory has been deleted, why then is rmtree trying to make it read/writeable??! The permissions on the directories are as follows: drwxrwxr-x 3 nobody nobody 4096 Jul 18 13:35 /u01/mydocs drwxr-xr-x 2 nobody nobody 4096 Jan 1 2006 /u01/mydocs/mytest001 I'm not running as user "nobody", but I'm in group "nobody". I know this must be easy to fix... Thanks for any insights! richf
  • 2. search option in perl
    Hi all, I need to search a file for a specific string thru perl script. for example i want to put following condition in the script. --- > first search the string string1 ----> and if that string found then search for another string string2 ----> if both the string found then print both the string on shell prompt can anybody plz help regards irfan.
  • 3. How do I get a full array row from array of arrays?
    I'm sure this is an easy question for some of you. I've slurped up some data from a relational database using fetchall_arrayref and passed it in to a subroutine. I know how to get at individual elements after dereferencing the array: (e.g., go get the second element of the first array returned) my @data = @$data_ref; $output = $data[0][1]; But I'd like to slurp all elements of the first array returned as an array. Is it possible to assign a full row from the array of arrays without iterating through each element? (i.e., I want assign all elements in the $data[0] portion of the above array of arrays as an new array.)
  • 4. regarding rsh
    Hi I have two perl scripts second one i execute it remotely using rsh.The problem is after the scripts is succesful i exit 0 or if its a failure exit 1 .How can i handle it in the first script as rsh will not return the exit code of the script rather it jus return the exit code of its one . the exit code of rsh is whether the script has been run succesfully or not that it but not wat script returns. any help pls. thanks
  • 5. Modification of table -limited by programming constraint
    Hi, In my current program I am not sure where to use rowSpan so that I create table like below. In otherwords, I want switch 1 to appear once only for the two rows. My main constraint is I dont want to change the program style to create such a table . Any help will be greatly appreciated. |--------------|------------| | | | |--------------|------------| | | Cell B | |switch1 |------------| | | Cell C | |--------------|------------| print $cgi->table({ -border=>undef, -width=>'100%', -height=>'20%', -colSpan => 10, }, $cgi->Tr({ -align=>'center', -valign=>'top' }, [ $cgi->th([ "Network Switch", "Slot 1", "Slot 2", "Network Switch"]), ]), $cgi->Tr({ -align=>'center', -valign=>'middle', }, [ $cgi->td([ '{-rowSpan=> 2},Switch 1', 'CayenneE-1_B-1 ', 'CayenneE-1_B-2', "Switch 2" ]), ]), $cgi->Tr({ -align=>'center', -valign=>'middle', }, [ $cgi->td([ 'Switch 1', 'CayenneE-1 ', 'No Blade', "Switch 2" ]), ]), ) ;

comparing and contrasing two approaches to variable scope

Postby rhall » Thu, 04 May 2006 23:17:21 GMT

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 



Re: comparing and contrasing two approaches to variable scope

Postby paul » Thu, 04 May 2006 23:40:19 GMT




First, forget about speed.  Why do you care whether one approach is
faster than another?  If your script is "fast enough" don't waste time
and effort even thinking about it.  And if it's not fast enough it's
unlikely that tweaking a few variables is going to make it fast enough.

So, we're left with style.  Choose the style which makes your program
most understandable.  If you only have a few parameters then passing
them around may be fine.  If there are lots you might want to put them
into global variables, by which I mean package scoped lexical variables.
I always start global variables with an uppercase letter so they are
immediately recognisable.

An approach I frequently use is to have a global hash %Options in which
I keep all the  ... wait for it ... global options.  I also pass this
hash into GetOptions from Getopt::Long to get the command line options.
I find this approach works well.

Another option, if you are using OO, is to make the options part of the
object in which case they get passed around for free.

-- 
Paul Johnson -  XXXX@XXXXX.COM 
 http://www.**--****.com/ 

Similar Threads:

1.Problem in comparing two files using compare.pm

Hi,

Please find below my code to compare 2 files
"source.txt,destination.txt" in folder "seek"

#!perl

use File::Compare;

if(Compare("source.txt","destination.txt")==0)
	{
	    print "They're equal\n";
	}

Please find below the error I am getting while running the code in
command prompt

D:\seek>perl filematch.pl

syntax error at C:\Program Files\Perl\lib/File/Compare.pm line 3, near
"use 5.00
5_64"
BEGIN failed--compilation aborted at filematch.pl line 3.

Please help me to rectify this problem..

Note: In my other computer with perl tjis code works properly..

Regards,
Prabhu

2.newbie question about scope, variables, declarations of variables and option strict (as in perl)


Hello,

I am trying to understand the syntax error I receive in a code similar to
this.

1 require 'logger'
2
3 log = logger.new  #some other logger settings are ignored.

4 def func
5   log.debug "a statement" # error is reported here when func is called
below
6  # some code
7 end
8
9 #some code continues
10 func


When func is called, an error is reported on line-5 saying that undefine
local variable log etc. I understand that functions create scopes and log is
seen as local variable  which is not defined in that scope. As it is
qualified with no scope operator, interpreter thinks that it is local but
can not find definition of the log before it's usage but also in the
parameter list and I understand that. On the other hand, I can use log
without qualifying it with a scope symbol anywhere in the same file if it is
not in a function. I know that loops, if statements etc are built into the
language and do not create scope. Code blocks inherit the locals. So it is
meaningful that I can use it anywhere else. When I qualify log with $ as
$log, it becomes global and I no longer receive error. I have tried it
qualifying with @ etc. but the received the same error. What I am asking is,
what is scope of log?. What kind of variable is it? It is the local or
instance variable of what, Object? I know that func is private to the
Object. But what about log?  How  can  I  access it in a function without
making it global?

Is there a way to make variables local to a file as perl does with "my".

Is there a strict option that prevents unintended variable creation because
of typos. Is there a way force predeclaration of variables?

Thanks.

3.(fwd) How to make a megawidget with two entrys and two variables

> From: "Pit" < XXXX@XXXXX.COM >
> Newsgroups: comp.lang.perl.tk
> Subject: How to make a megawidget with two entrys and two variables
> Date: Thu, 10 Mar 2005 13:24:38 -0500
>


>
> Hi Folks
>
> a question to designing a mega-widget.
>
> I would like to create a mega-widget that contains two simple entry's 
> and
> accordingly two text-variables.
>
> the mega-widget should be created as follows:
>
> my $megawidget->MegaWidet(-var1 => \$var1,
>                           -var2 => \$var2);
>
> How to process this into the mega-widget ?
>
> Has somebody any examples or other ways in order to check this ?
>

With a tiny bit of room for improvement ...

#!/usr/local/bin/perl -w
$Tk::MyMega::VERSION = '0.1';

package Tk::MyMega;

use base qw/Tk::Frame/;
use strict;

Construct Tk::Widget 'MyMega';

sub Populate {

     my($self, $args) = @_;

     $self->SUPER::Populate($args);

     $self->{entry1} = $self->Entry->pack;
     $self->{entry2} = $self->Entry->pack;

     $self->ConfigSpecs(
         -variable1   => [qw/PASSIVE  variable1   Variable1/, undef ],
         -variable2   => [qw/PASSIVE  variable2   Variable2/, undef ],
     );

} # end Populate

sub ConfigChanged {

     my( $self, $args ) = @_;

     foreach my $k (keys %$args) {
	if( my( $n ) = $k =~ /^-variable(\d+)$/ ) {
	    $self->{"entry${n}"}->configure( -textvariable => $args->{$k} );
	}
     }

} # end ConfigChanged

package main;

use Tk;
#use Tk::MyMega;
use strict;

my $mw = MainWindow->new;
my $frog = 1;
my $toad = 2;
my $mm = $mw->MyMega(
		     -background => 'white',
		     -foreground => 'blue',
		     -variable1  => \$frog,
		     -variable2  => \$toad,
)->pack;

$mw->update;
$mw->after( 2000, sub{ $frog++; $toad++ } );

MainLoop;

-++**==--++**==--++**==--++**==--++**==--++**==--++**==
This message was posted through the Stanford campus mailing list
server.  If you wish to unsubscribe from this mailing list, send the
message body of "unsubscribe ptk" to  XXXX@XXXXX.COM 

4.How to make a megawidget with two entrys and two variables

Hi Folks

a question to designing a mega-widget.

I would like to create a mega-widget that contains two simple entry's and
accordingly two text-variables.

the mega-widget should be created as follows:

my $megawidget->MegaWidet(-var1 => \$var1,
                          -var2 => \$var2);

How to process this into the mega-widget ?

Has somebody any examples or other ways in order to check this ?

Thanks
Pit

5.Mega-Widget with two entry's and two variables

Hi Folks

a question to designing a mega-widget.

I would like to create a mega-widget that contains two simple entry's and
accordingly two text-variables.

the mega-widget should be created as follows:

my $megawidget->MegaWidet(-var1 => \$var1,
                          -var2 => \$var2);

How to process this into the mega-widget ?

Has somebody any examples or other ways in order to check this ?

Thanks
Pit

6. scope of the variable?

7. Variable scope in wanted function

8. problem with variable scope



Return to PERL

 

Who is online

Users browsing this forum: No registered users and 19 guest